jtobject.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 2,226 行 · 第 1/5 页
JAVA
2,226 行
}
private Object encodeObject (Object obj) {
ByteArrayOutputStream stream = new ByteArrayOutputStream ();
XMLEncoder e;
Object result = null;
if (obj == null)
return (null);
try {
e = new XMLEncoder(
new BufferedOutputStream(stream));
e.writeObject(obj);
e.close();
result = stream.toString ();
} catch (Exception ex) {
handleException (ex);
return (null);
}
return (result);
}
private Object decodeObject (Object obj) {
ByteArrayInputStream stream;
Object result = null;
XMLDecoder d;
if (obj == null)
return (null);
stream = new ByteArrayInputStream (((String) obj).getBytes ());
try {
d = new XMLDecoder(
new BufferedInputStream(stream));
result = d.readObject();
d.close();
} catch (Exception ex) {
handleException (ex);
}
return (result);
}
// Clone the object
private Object cloneObject () {
JtMessage msg = new JtMessage (JtObject.JtXML_ENCODE);
JtObject tmp = new JtObject();
Object aux;
msg.setMsgContent (this);
aux = tmp.processMessage (msg);
if (aux == null) {
handleError ("cloneObject: Unable to encode the object (XML format");
return (null);
}
msg = new JtMessage (JtObject.JtXML_DECODE);
msg.setMsgContent (aux);
return (tmp.processMessage (msg));
}
private void resetFramework () {
InputStream resStream = this.getClass().getClassLoader().getResourceAsStream (JtObject.RESOURCE_FILE);
if (resStream != null) {
setResourceStream (resStream);
return;
}
}
private void setValues (JtValueObject values) {
JtIterator iterator;
Object obj, value;
JtMessage msg = new JtMessage (JtValueObject.JtGET);
if (values == null)
return;
iterator = (JtIterator) values.processMessage(new JtMessage (JtValueObject.JtGET_KEYS));
if (iterator == null)
return;
for (;;) {
obj = iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
if (obj == null)
break;
msg.setMsgData(obj);
value = values.processMessage(msg);
setValue (this, obj, value);
}
}
/**
* Process a Jt Message. This is the only method required by the Jt interface (Jt Interface).
* <li>JtCLONE - returns a clone of this object.
* <li>JtACCEPT - Accept operation. This message contains a reference to
* the visitor (msgContent). As a result of this operation, a JtVISIT
* message is sent to the visitor. A reference to this object is passed to the visitor.
* <li>JtXML_ENCODE - Encode an object using the XML format.
* <li>JtXML_DECODE - Decodes an object from its XML representation.
* @param message Jt Message
*/
public Object processMessage (Object message) {
String msgid;
JtMessage msg = (JtMessage) message;
Object content;
JtMessage aux;
// handleTrace ("JtObject.processMessage called");
if (msg == null)
return (null);
msgid = (String) msg.getMsgId ();
if (msgid == null)
return (null);
// Remove this object
if (msgid.equals (JtObject.JtREMOVE)) {
return (null);
}
// Clone object
if (msgid.equals (JtObject.JtCLONE)) {
return (cloneObject ());
}
if (msgid.equals (JtObject.JtSET_VALUE)) {
setValue (msg.getMsgSubject (),
msg.getMsgContent (),
msg.getMsgData());
return (null);
}
if (msgid.equals (JtObject.JtSET_VALUES)) {
setValues ((JtValueObject) msg.getMsgContent ());
return (null);
}
if (msgid.equals (JtObject.JtGET_VALUE)) {
if (msg.getMsgSubject () == null)
return (getValue (this,
msg.getMsgContent ()));
else
return (getValue (msg.getMsgSubject (),
msg.getMsgContent ()));
}
if (msgid.equals (JtObject.JtCREATE_OBJECT)) {
createObject (msg.getMsgContent (),
msg.getMsgData());
return (null);
}
if (msgid.equals (JtObject.JtREMOVE_OBJECT)) {
removeObject (msg.getMsgContent ());
return (null);
}
if (msgid.equals (JtObject.JtSEND_MESSAGE)) {
return (sendMessage (msg.getMsgContent (),
msg.getMsgData ()));
}
if (msgid.equals (JtObject.JtCOPY)) {
return (copyObject (msg.getMsgContent (),
msg.getMsgData ()));
}
// Encode Object (XML format)
if (msgid.equals (JtObject.JtXML_ENCODE)) {
return (encodeObject (msg.getMsgContent ()));
}
// Decode Object
if (msgid.equals (JtObject.JtXML_DECODE)) {
return (decodeObject (msg.getMsgContent ()));
}
if (msgid.equals (JtObject.JtPRINT)) {
System.out.println (encodeObject (this));
return (null);
}
if (msgid.equals (JtObject.JtVALUE_OBJECT)) {
return (this);
}
// Internal message. It will cause the framework
// to be initialized. Use with care
if (msgid.equals (JtObject.JtRESET_FRAMEWORK)) {
resetFramework ();
return (null);
}
if (msgid.equals (JtObject.JtACCEPT)) {
content = msg.getMsgContent();
if (content == null) {
handleError ("processMessage: invalid ACCEPT message; visitor reference is null");
return (null);
}
// Send a JtVISIT message to the visitor.
// Include a reference to this object
aux = new JtMessage (JtVisitor.JtVISIT);
aux.setMsgContent (this);
return (sendMessage (content, aux));
}
handleError ("JtObject.processMessage: invalid msg ID:"
+ msg.getMsgId ());
return (null);
}
// Open log file
private void open_logfile () {
FileOutputStream fs;
if (logFile == null || logFile.equals (""))
return;
if (logFile.equals ("stderr")) { // stderr
logStream = null;
return;
}
try {
fs = new FileOutputStream (logFile);
logStream = new PrintWriter (fs);
} catch (Exception e) {
logStream = null;
handleException (e);
return;
}
handleTrace ("JtObject.open_logfile: opened log file ... " +
logFile);
//handleTrace ("JtObject.open_logfile:user.dir:" +
// System.getProperty ("user.dir"));
//handleTrace ("JtObject.open_logfile:user.home:" +
// System.getProperty ("user.home"));
}
// Initialize: initialize Jt
void initialize () {
String rfile = null;
String jtHome = null;
String separator = System.getProperty ("file.separator");
InputStream resStream;
//String tmp;
logFile = System.getProperty ("Log");
// rfile = System.getProperty ("Resources");
//jtHome = System.getenv ("Home");
jtHome = System.getProperty ("Home");
if (jtHome != null) {
rfile = jtHome + separator + OLD_RESOURCE_FILE;
} else
rfile = OLD_RESOURCE_FILE;
if (logFile != null) {
logging = true; // show debugging messages
//this.setObjTrace (1);
open_logfile ();
}
//if (logFile != null)
//open_logfile ();
handleTrace ("Initializing Jt " + version + "...");
if (jtHome != null)
handleTrace ("Home property has been set:" + jtHome);
//else
// handleTrace
// ("Home property hasn't been set. Using the current directory as the default.");
resStream = this.getClass().getClassLoader().getResourceAsStream (JtObject.RESOURCE_FILE);
if (resStream != null) {
setResourceStream (resStream);
return;
}
if (rfile != null)
setResourceFile (rfile);
//loadResourceFile ();
}
// realize
void realize () {
}
// activate
void activate () {
}
// destroy
void destroy () {
}
private String stackTrace (Exception ex ) {
ByteArrayOutputStream bstream;
if (ex == null)
return (null);
bstream = new ByteArrayOutputStream ();
ex.printStackTrace (new PrintWriter (bstream, true));
return (bstream.toString ());
}
/**
* Handles exceptions. Jt provides a consistent way of handling exceptions:
* The exception is stored using the objException attribute.
* The exception message and the stack trace are sent to the screen or a log file.
* This method should be called each time an exception is detected.
* Overide this method if your application requires special exception handling.
* @param e exception
*/
public void handleException (Throwable e) {
Date date;
SimpleDateFormat formatter;
String trace;
// An exception has occured. Update the objException attribute
objException = e;
//e.printStackTrace ();
trace = stackTrace ((Exception) objException);
date = new Date ();
formatter = new SimpleDateFormat(JtObject.DATE_FORMAT);
if (logStream == null) {
System.err.println (formatter.format(date) + ":" + trace);
return;
}
//logStream.println (formatter.format(date) + ":" + e);
logStream.println (formatter.format(date) + ":" + trace);
logStream.flush ();
}
/**
* Handles trace messages. This method should be called each time a
* message needs to be logged. Trace messages are send to the screen or a log file
* depending on the logFile attribute. The logging attribute is used to enable/disable
* logging. Override this method if your application requires special logging capabilities.
* @param msg trace message
* @param level logging level
*/
public void handleTrace (String msg, int level) {
Date date;
SimpleDateFormat formatter;
//if (objTrace <= 0)
// return;
if (!logging)
return;
if (level < logLevel)
return;
date = new Date ();
formatter = new SimpleDateFormat(JtObject.DATE_FORMAT);
if (logStream == null) {
System.err.println (formatter.format(date) + ":" + msg);
return;
}
logStream.println (formatter.format(date) + ":" + msg);
logStream.flush ();
}
public void handleTrace (String msg) {
handleTrace (msg, JtObject.JtDEFAULT_LOG_LEVEL);
}
/**
* Handles warning messages. This method should be called each time a warning
* message needs to be logged. Trace messages are send to the screen or a log file
* depending on the logFile attribute. Warning messages are always logged regardless
* of the value of the objTrace attribute.
* @param msg trace message
*/
public void handleWarning (String msg) {
Date date;
SimpleDateFormat formatter;
/*
if (objTrace > 0)
*/
date = new Date ();
formatter = new SimpleDateFormat(JtObject.DATE_FORMAT);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?