jtobject.java

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

JAVA
2,226
字号

        if (length == 0)
            return (null);

        index = line.indexOf (":");

        if (index < 0)
            return (null);

        if (index == length - 1)
            return (null);

        res = new JtResource ();

        res.value = line.substring (index+1, length);

        tmp = line.substring (0, index);

        index = tmp.lastIndexOf (".");

        if (index == -1)
            return (null);

        if (index == tmp.length () - 1)
            return (null);  // ends with a dot ?

        res.robj = tmp.substring (1, index);
        res.name = tmp.substring (index + 1);  

        handleTrace ("Jt Resource: " + "(" +
                res.robj + "," + res.name + "," + res.value
                + ")");

        return (res);

    }
    
    
    private void loadClassResources (Object obj, Class cl) {
        String className;
        Hashtable ht;
        Enumeration rnames;
        Object resource;

        //handleTrace ("Jt: Loading Class ..." + cl);
        if (obj == null || cl == null)
            return;
        
        className = cl.getName();

        ht = (Hashtable) resTable.get (className);

        if (ht == null) {
            loadClassResources (obj, cl.getSuperclass()); // check
            return;
        }
        
        rnames = ht.keys ();
        if (rnames == null)
            return;


        while (rnames.hasMoreElements ()) {
            resource = rnames.nextElement ();
            setValue (obj, resource, ht.get (resource));
        } 
        
        loadClassResources (obj, cl.getSuperclass());
        
    }
    
    private void loadObjectResources (Object obj) {

        Hashtable ht;
        Enumeration rnames;
        Object resource;
        Object objId;
        //String className;

        handleTrace ("Jt: Loading Jt resource values into object ...");
        if (resTable != null) {


            // Load class resources

            if (obj == null)
                return;



            loadClassResources (obj, obj.getClass());

        }
        
        // Load object resources
        

        if (resObjTable == null)
            return; 
        
        if (!(obj instanceof JtObject))
            return;
                           
        objId = ((JtObject) obj).getObjName();

        if (objId == null)
            return;
        
        
        ht = (Hashtable) resObjTable.get (objId);
        
        if (ht == null)
            return; 

        rnames = ht.keys ();
        if (rnames == null)
            return;

        while (rnames.hasMoreElements ()) {
            resource = rnames.nextElement ();
            setValue (obj, resource, ht.get (resource));
        }        
        
        //handleTrace ("Jt: Loaded Jt resources");
    }


    private void updateObjResources (JtResource res) {
        Hashtable ht;

        if (res == null)
            return;

        if (res.robj == null)
            return;

        if (resObjTable == null)
            resObjTable = new Hashtable ();

        if (resObjTable.get (res.robj) == null) {
            resObjTable.put (res.robj, new Hashtable ());
        }

        ht = (Hashtable) resObjTable.get (res.robj);

        if (ht == null)
            return; // check

        ht.put (res.name, res.value);
    }
    
    
    private void updateResources (JtResource res) {
        Hashtable ht;

        if (res == null)
            return;

        if (res.rclass == null)
            return;

        if (resTable == null)
            resTable = new Hashtable ();

        if (resTable.get (res.rclass) == null) {
            resTable.put (res.rclass, new Hashtable ());
        }

        ht = (Hashtable) resTable.get (res.rclass);

        if (ht == null)
            return; // check

        ht.put (res.name, res.value);
    }

    // Load resources from a stream

    private void loadResourcesFromStream (InputStream resourceStream) {
        String line;
        JtResource res;

        if (resourceStream == null)
            return;


        handleTrace ("Jt: reading resources from stream" + " ...");

        try {
            BufferedReader d = new BufferedReader 
            (new InputStreamReader (resourceStream));

            while ((line  = d.readLine ()) != null) {

                if (line.startsWith ("!") || 
                        line.startsWith ("//"))
                    continue;
                
                if (line.startsWith ("#")) {
                    res = parseObjResource (line);
                    
                    if (res != null)
                        updateObjResources (res);
                    
                    continue;
                }
                
                res = parseResource (line);

                if (res != null)
                    updateResources (res);
            }

        } catch (Exception ex) {
            handleException (ex);
        }
    } 

    // Load resources from a file

    private void loadResourceFile ( ) {
        String line;
        JtResource res;
        File file;

        if (resourceFile == null)
            return;

        file = new File (resourceFile);

        if (!file.exists ()) {
            handleTrace ("Resource file not found:" + resourceFile);
            return;
        }

        handleTrace ("Jt: reading resources from " +
                resourceFile + " ...");
        try {
            BufferedReader d = new BufferedReader 
            (new FileReader (resourceFile));

            while ((line  = d.readLine ()) != null) {
                //System.out.println ("JtFile.read_lines:" + line);
                if (line.startsWith ("!") || 
                        line.startsWith ("//"))
                    continue;
                
                if (line.startsWith ("#")) {
                    res = parseObjResource (line);
                    
                    if (res != null)
                        updateObjResources (res);
                    
                    continue;
                }
                
                res = parseResource (line);

                if (res != null)
                    updateResources (res);
            }

        } catch (Exception ex) {
            handleException (ex);
        }
    }

    /*
     * Jt hook for the JBPM api. Defines a jBPM variable that points to the Jt object.
     * It uses the name of the action (jBPM process diagram) as the name of the variable. 
     * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
     */
    public void execute(ExecutionContext context) throws Exception {
        //String reply;
        //JtMessage msg;
        String name, nodeName;

        try {
            nodeName = context.getContextInstance().getProcessInstance().getRootToken().getNode().getName();
            handleTrace ("JtObject.execute:entering node ... " + nodeName);

            name = context.getAction().getName ();
            handleTrace ("JtObject.execute:creating a Jbpm variable " + name);
            context.getContextInstance().setVariable (name, this);	
        } catch (Exception ex) {
            handleException (ex);
            return;
        } 	
        loadObjectResources (this);
    }



    public String toString () {
        int index;

        if (this.objName != null) {
            index = this.objName.lastIndexOf('.');
            if (index < 0)
                return (this.objName);
            return (this.objName.substring(index+1));
        }    
        return (super.toString());

    }

    /**
     * Demonstrates the messages processed by JtObject
     */

    public static void main(String[] args) {

        JtObject main = new JtObject ();
        JtMessage msg;
        String tmp;
        //JtObject main1;

        //main.setObjTrace (1);
        //main.setLogging (true);


        // Create the object

        msg = (JtMessage) main.createObject (JtMessage.JtCLASS_NAME, "message");



        if (main.lookupObject ("message") != null)
            System.out.println ("Jt.createObject: GO");
        else
            System.out.println ("Jt.createObject: FAILED");

        // Set Value

        main.setValue ("message", "msgId", "JtTestId");

        if (msg.getMsgId () != null)
            if (msg.getMsgId().equals ("JtTestId"))
                System.out.println  ("Jt.setValue: GO");
            else
                System.out.println  ("Jt.setValue: FAILED");
        else
            System.out.println  ("Jt.setValue: FAILED");


        //main.sendMessage (main, "message");

        // Get value

        tmp = (String) main.getValue ("message", "msgId");

        if (tmp != null)
            if (tmp.equals ("JtTestId"))
                System.out.println  ("Jt.getValue: GO");
            else
                System.out.println  ("Jt.getValue: FAILED");
        else
            System.out.println  ("Jt.getValue: FAILED");

        main.removeObject ("message");

        if (main.lookupObject ("message") == null) {
            System.out.println  ("Jt.removeObject: GO"); 
        } else
            System.out.println  ("Jt.removeObject: FAILED"); 

        // qualified names

        /*
    main.setObjName ("main");
    msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");

    if (main.lookupObject ("main.message") != null) {
	System.out.println  ("Jt.createObject (main.message): GO"); 
    } else
	System.out.println  ("Jt.createObject (main.message): FAILED"); 

    if (main.lookupObject ("message") != null) {
	System.out.println  ("Jt.lookupObject (main.message): GO"); 
    } else
	System.out.println  ("Jt.lookupObject (main.message): FAILED"); 

    main.removeObject ("message");


    if (main.lookupObject ("message") == null 
            && main.lookupObject ("main.message") == null) {
        System.out.println  ("Jt.removeObject (main.message): GO"); 
    } else
        System.out.println  ("Jt.removeObject (main.message): FAILED"); 
         */

        /*
    msg = (JtMessage) main.createObject ("Jt.JtMessage", "main.message");

    main.setValue ("message", "msgContent", "hello");
    main.setValue ("message", "objPath", "kk/message");
    msg.save ();
    JtMessage obj = new JtMessage ();
    obj.setObjPath ("kk/message");

    obj = (JtMessage) obj.restore ();

    System.out.println  ("JtObject after restore (name):" + obj.getMsgContent ());
         */

        // handleWarning

        //main.handleError ("JtObject: this is a warning");
        main.setLogLevel(1);
        main.handleTrace ("JtObject: this is a trace");
        //main.handleException (new Exception ("JtObject: this is a ex"));


        // JtPrint message

        JtObject obj = (JtObject) main.createObject (JtObject.JtCLASS_NAME, null); 
        //main.setValue(obj, "objName", "newName");


        JtValueObject valueObject = new JtValueObject ();
        msg = new JtMessage (JtValueObject.JtPUT);
        msg.setMsgData("objName");   
        msg.setMsgContent("New Name");
        main.sendMessage(valueObject, msg);   

        msg = new JtMessage (JtObject.JtSET_VALUES);
        msg.setMsgContent(valueObject);  

        main.sendMessage(obj, msg);

        main.sendMessage(obj, new JtMessage (JtObject.JtPRINT));

        //System.out.println (main.getValue (obj, "resTable"));
        System.out.println (main.getValue (obj, "logging"));   


        main.sendMessage(obj, new JtMessage (JtObject.JtRESET_FRAMEWORK));

    }
}

⌨️ 快捷键说明

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