jtobject.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 2,226 行 · 第 1/5 页

JAVA
2,226
字号

package Jt;

import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.reflect.*;
import java.beans.*;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;



/**
 * Root class of the Jt Class hierarchy; it implements the functionality 
 * required by the Jt Messaging pattern.
 */


public class JtObject implements Serializable, JtInterface, ActionHandler {

    private static final long serialVersionUID = 1L;
    public static final String JtCLASS_NAME = JtObject.class.getName();
    public static final String RESOURCE_FILE = "Jt.properties";  // Resource file
    //public static final String WEB_RESOURCE_PATH = "/WEB-INF/.Jtrc";  // Resource path (web application)
    //public static final String DATE_FORMAT = "HH:mm:ss MM/dd/yyyy z"; // Date format
    public static final String DATE_FORMAT = "HH:mm:ss MM/dd/yyyy"; // Date format

    // Messages

    public static final String JtPRINT = "JtPRINT";
    public static final String JtBROADCAST = "JtBROADCAST";  // Broadcast a message
    public static final String JtTEST = "JtTEST";  
    public static final String JtREMOVE = "JtREMOVE"; 
    public static final String JtCLONE = "JtCLONE";         
    public static final String JtCOPY = "JtCOPY"; 
    public static final String JtACTIVATE = "JtACTIVATE"; 
    public static String JtREALIZE = "JtREALIZE";
    public static String JtINITIALIZE = "JtINITIALIZE";
    public static final String JtXML_ENCODE = "JtXML_ENCODE"; 
    public static final String JtXML_DECODE = "JtXML_DECODE";   

    public static final String JtSTART = "JtSTART";
    public static final String JtSTOP = "JtSTOP";

    // Messages used for the implementation of the visitor
    // design pattern

    public static final String JtVISIT = "JtVISIT";
    public static final String JtACCEPT = "JtACCEPT";

    // Value Object design pattern

    public static final String JtVALUE_OBJECT = "JtVALUE_OBJECT";  

    // Core Jt messages

    public static String JtCREATE_OBJECT = "JtCREATE_OBJECT";  
    public static String JtSEND_MESSAGE = "JtSEND_MESSAGE";  
    public static String JtSET_VALUE = "JtSET_VALUE";
    public static String JtGET_VALUE = "JtGET_VALUE";
    public static String JtREMOVE_OBJECT = "JtREMOVE_OBJECT";
    public static String JtSET_VALUES = "JtSET_VALUES";

    // Internal framework messages 

    public static String JtRESET_FRAMEWORK = "JtRESET_FRAMEWORK";  

    
    // CRUD Messages
    

    public static String JtCREATE = "JtCREATE";
    public static String JtREAD = "JtREAD";
    public static String JtUPDATE = "JtUPDATE";   
    public static String JtDELETE = "JtDELETE";   
    
    public static String JtLOOKUP = "JtLOOKUP"; 
    public static String JtSAVE = "JtSAVE";

    // Logging constants

    public static int JtDEFAULT_LOG_LEVEL = 3;          // Default Logging level
    public static int JtMIN_LOG_LEVEL = 0;              // Minimum Logging level

    private String objName = null;			            // Qualified object name
    //private int objTrace = 0;                         // Enable/disable trace messages
    private Object objException;	                    // Exception

    private Hashtable objTable = null;                   // Children of this object
    private static Hashtable resTable = null;            // Attribute values
    private static Hashtable resObjTable = null;         // Attribute values
    private transient Object objErrors = null;

    // Context attributes

    private static String version = "3.0 - 11/01/08";    // Framework version
    private static boolean initted = false;              // The framework has been initialized?
    private static boolean logging = false;              // is logging enabled?
    private static String logFile = null;                // Log file
    private static int logLevel = JtObject.JtDEFAULT_LOG_LEVEL;  // Log level
    private static PrintWriter logStream = null;
    private static String resourceFile = RESOURCE_FILE;  // Jt Resource file
    private static InputStream resourceStream = null;
    //public static JtContext objContext = new JtContext (); // Jt Context

    public static final String OLD_RESOURCE_FILE = ".Jtrc";  // old Resource file


    public JtObject () {

        // Initialize the framework

        if (!initted) {
            initialize ();
            initted = true;
        }
        if (this instanceof JtFactory)
            loadObjectResources (this);
        //    if (logging)
        //       setObjTrace (1);
    } 


    /**
     * Creates a Jt object of the specified class. The object is assigned the ID passed as parameter. 
     * The new object is a child of this object. 
     * If another child with the same ID already exists, an error is produced.
     * After creating the object, it sets its attributes using the Jt resource file. 
     * @param class_name class name
     * @param id  object id
     * @return object created or  null
     */

    public Object createObject (Object class_name, Object id) {
        Object obj = null, tmp = null;
        Class jtclass;
        String objId;

        if (id == null || id.equals ("")) {
            objId = null;
            handleTrace ("JtObject.createObject:" + class_name);
        } else {
            handleTrace ("JtObject.createObject:" + class_name + "," + id);
            objId = (String) id;
        }
        if (class_name == null) {
            handleError ("JtObject.createObject: invalid paramenters");
            return (null);
        }

        obj = lookupObject (objId);

        // check for duplicates
        if (obj != null) {
            if (!logging)
                handleWarning 
                ("JtObject.createObject: unable to create a duplicate entry " + id);
            else
                handleError 
                ("JtObject.createObject: unable to create a duplicate entry " + id);
            return (null);
        }

        try {
            jtclass = Class.forName ((String) class_name);
        } catch (Exception e) {
            handleException (e);
            return (null);
        }

        // Create a new instance of the object

        try {


            if (JtSingleton.class.isAssignableFrom (jtclass)) {
                obj = JtSingleton.getInstance ();
                if (obj == null) {
                    obj = jtclass.newInstance ();
                    JtSingleton.setInstance (obj);
                    tmp = JtSingleton.getInstance ();        
                    // Although unlikely, another thread may create the Singleton instance
                    // first. In this case, a message should be produced. 
                    if (tmp != obj) {
                        handleTrace 
                        ("JtObject.createObject: attempt to create another instance of a Singleton (" +
                                class_name + ")", 0); 
                        return (tmp); 
                    }


                } else {
                    handleTrace 
                    ("JtObject.createObject: attempt to create another instance of a Singleton (" +
                            class_name + ")", 0); 
                    return (obj); 
                }
            } else
                obj = jtclass.newInstance ();

            if (obj instanceof JtObject) {
                if (objId != null)
                    //((JtObject)obj).setObjName (absoluteName ((String) objId));
                    ((JtObject)obj).setObjName (objId);
                /*
               if (logging)
                   ((JtObject)obj).setObjTrace (1);
               else
                   ((JtObject)obj).setObjTrace (this.getObjTrace ());
                 */    
            }


            // add the new object to the object table
            if (obj != null) {
                if (objId != null)
                    //add (absoluteName ((String) objId), obj);
                    add (objId, obj);
                else
                    add (obj, obj);
            }
            loadObjectResources (obj);
        } catch (Exception e) {
            handleException (e);
        }


        return (obj);
    }

    /** Creates a Jt object of the specified class. 
     * @param className class name
     * @return object created or  null
     */
    public Object createObject (Object className) {

        return (createObject (className, null));

    }

    // absolute_name: determine the absolute name of the object

    String absoluteName (String name) {
        if (name == null)
            return (null);

        if (name.indexOf (".") > 0)
            return (name);

        if (this.getObjName () == null)
            return (name);


        return (this.getObjName () + "." + name);

    }


    String calculateAbsoluteName (Object id) {
        String name;

        if (id == null)
            return (null);

        if (id instanceof JtObject) {
            if (((JtObject) id).getObjName () == null)
                handleWarning ("calculateAbsoluteName:JObject is missing name (null)");
            return (((JtObject) id).getObjName ());
        }

        if (id instanceof String) {
            name = (String) id;
        } else
            name = id.toString ();

        if (name.indexOf (".") > 0)
            return (name);

        if (this.getObjName () == null)
            return (name);

        return (this.getObjName () + "." + name);

    }


    /**
     * Looks for an object based on its ID. 
     * @param id object ID
     * @return object found or null
     */

    public Object lookupObject (Object id) {
        String obj_id;


        if (id == null) 
            return (null);

        if (!(id instanceof String))
            //if (!id.getClass().getName().equals ("java.lang.String"))
            return (id); // check

        if (id.equals(""))
            return (null);

        //obj_id = absoluteName ((String) id);
        obj_id = (String) id;


        if (objTable == null)
            return (null);

        return (objTable.get (obj_id));


    }



    private boolean checkModifiers (Class cl, String prop) {

        //Class cl;
        Field field;
        int mod;


        if (cl == null || prop == null)
            return (false);


        field = null;
        try {
            field = cl.getDeclaredField (prop); // property dup property names
            //System.out.println ("class:" + cl.getName ());

        } catch (Exception e) {

            //handleException (e);

            if (cl.getSuperclass () == null) {
                handleException (e);
                return (false);
            }
        }

        if (field == null) {
            cl = cl.getSuperclass ();
            return (checkModifiers (cl, prop));
        }

        mod = field.getModifiers ();

        if (Modifier.isTransient (mod)) {
            return (false);
        }
        if (Modifier.isStatic (mod)) {
            return (false);
        } 
        return (true);       
    }

    private Object copyObject (Object obj, Object target) {
        //Object args[];
        PropertyDescriptor[] prop;
        //Class p;
        //Method m;
        BeanInfo info = null;
        Object value, svalue = null;
        int i;


        if (obj == null || target == null)
            return (null);

        if (obj.getClass () != target.getClass ()) {
            handleError 
            ("Jt.copyObject: object classes should be the same (source and target)");
            return (null);
        }

        try {
            info = Introspector.getBeanInfo(
                    obj.getClass (), java.lang.Object.class);
        } catch(Exception e) {
            handleException (e);
            return (null); //check
        }

        prop = info.getPropertyDescriptors();


        for(i = 0; i < prop.length; i++) {

            if (!checkModifiers (obj.getClass (),prop[i].getName())) {
                continue;
            }

            value = getValue (obj, prop[i].getName()); // check

            svalue = null;
            if (value instanceof Integer ||
                    value instanceof Long || 
                    value instanceof Float ||
                    value instanceof Byte ||
                    value instanceof Boolean ||
                    value instanceof Short ||
                    value instanceof Double ||
                    value instanceof Character) {
                svalue =  value.toString (); 
            } else
                svalue = value;


            //if (svalue != null) 
            setValue (target, prop[i].getName(), svalue);

        }

        return (null); // check
    } 

    // Sets the value of an attribute via a proxy

    private void setProxyValue (Object id, Object att, Object value) {
        JtMessage msg = new JtMessage (JtObject.JtSET_VALUE);

        if (id == null)
            return;
        msg.setMsgContent(att);
        msg.setMsgData (value); 
        ((JtInterface) id).processMessage(msg); //check

    }

    /** 
     * Sets the value of an attribute. 
     * This method is able to convert the value from String to the correct type.
     * Automatic convertion from String is done for the following types:
     * byte, short, int, long, float, double, boolean and java.util.Date.
     * @param id object id
     * @param att attribute name
     * @param value attribute value
     */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?