jtobject.java

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

JAVA
2,226
字号
    public void setValue (Object id, Object att, Object value) {

        Object obj;
        Object args[];
        PropertyDescriptor[] prop;
        int i;
        Class p;
        Method m;
        BeanInfo info = null;

        /*    
        if (objSession != null)
	  handleTrace ("Session(" + objSession + "): JtObject.setValue:" + id + "," + att + "," + value);
        else
         */
        handleTrace ("JtObject.setValue:" + id + "," + att + "," + value);

        if (id == null | att == null) {
            handleError ("JtObject.setValue: invalid parameters");
            return;
        }

        // Use a Proxy
        if (id instanceof JtProxy) {
            setProxyValue (id, att, value);
            return;
        }

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("setValue: unable to find object: " + id);
            return;
        }

        /*
        if (obj instanceof JtValueObject) {
          ((JtValueObject) obj).setValue (id, att, value);
          return;
        }
         */

        try {

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

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

            if (prop[i].getName().equals ((String) att)) {

                p = prop[i].getPropertyType();
                args = new Object[1];
                //System.out.println ("setValue:type" + p);
//              if (value == null) {
//              args[0] = value;

                try {
                    if (p.getName().equals ("java.lang.String") ){
                        args[0] = value;
                    } else if (p.getName().equals ("byte") ){
                        args[0] = (value instanceof String)?Byte.valueOf ((String) value):value;
                    } else if (p.getName().equals ("short") ){
                        args[0] = (value instanceof String)?Short.valueOf ((String) value):value;
                    } else if (p.getName().equals ("int") ){
                        args[0] = (value instanceof String)?Integer.valueOf ((String) value):value;
                        //} else if (p.getName().equals ("java.lang.Long") ){
                    } else if (p.getName().equals ("long") ){
                        args[0] = (value instanceof String)?Long.valueOf ((String) value):value;
                    } else if (p.getName().equals ("float") ){
                        args[0] = (value instanceof String)?Float.valueOf ((String) value):value;
                    } else if (p.getName().equals ("double") ){
                        args[0] = (value instanceof String)?Double.valueOf ((String) value):value;
                    } else if (p.getName().equals ("boolean") ){
                        args[0] = (value instanceof String)?Boolean.valueOf ((String) value):value;
                    } else if (p.getName().equals ("char") ){
                        args[0] = (value instanceof String)?new Character (((String) value).charAt(0)):value;
                    } else if (p.getName().equals ("java.util.Date") ){
                        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
                        //SimpleDateFormat df;
                        if (value instanceof String)
                            try {
                                //df = new SimpleDateFormat ((String) value);
                                if (value != null && value.equals("")) {
                                    handleWarning ("setValue(date): empty string");
                                    return;
                                }    
                                args[0] = df.parse ((String) value);
                                //args[0] = new SimpleDateFormat ((String) value);
                            } catch (Exception e) {
                                handleException (e);
                                return;
                            }
                            else
                                args[0] = value;
                    } else
                        args[0] = value;
                } catch (Exception e) {
                    handleException (e);
                    return;
                }

                try {
                    m = prop[i].getWriteMethod ();
                    if (m == null) {
                        handleError 
                        ("JtObject.setValue failed:missing setter for attribute " + att);  
                        return;
                    }
                    m.invoke (obj, args);
                } catch (Exception e) {
                    handleException (e);
                }
                return;

            }
        }

        handleError ("JtObject.setValue: invalid attribute:"+  att);


    }

    // Gets the value of an attribute via a proxy

    private Object getProxyValue (Object id, Object att) {
        JtMessage msg = new JtMessage (JtObject.JtGET_VALUE);

        if (id == null)
            return (null);
        msg.setMsgContent(att);

        return (((JtInterface) id).processMessage(msg)); //check

    }

    /** 
     * Gets the value of an attribute. An object is always returned.  
     * For primitive types, the corresponding object type is returned.
     * For instance, Integer is returned if
     * the attribute is of type int. 
     *
     * @param id object id
     * @param att attribute name
     * @return attribute value
     */

    public Object getValue (Object id, Object att) {
        Object obj;
        Method m;
        //Class p;
        BeanInfo info = null;
        PropertyDescriptor[] prop;
        int i;

        handleTrace ("JtObject.getValue: " + id + "," + att);



        if (id == null || att == null) {
            handleError ("JtObject.getValue: invalid paramenters");
            return (null);
        }

        if (id instanceof JtProxy) {
            return (getProxyValue (id, att));
        }

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("getValue: unable to find object " + id);
            return (null);
        }
        /*
        if (obj instanceof JtValueObject) {
          return (((JtValueObject) obj).getValue (id, att));
        }
         */

        try {

            info = Introspector.getBeanInfo(obj.getClass (),
                    java.lang.Object.class);

        } catch(Exception e) {
            handleException (e);
            return (null);
        }

        prop = info.getPropertyDescriptors();

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


            if (prop[i].getName().equals ((String) att))    {

                try {
                    m = prop[i].getReadMethod ();
                    if (m == null) {
                        handleError 
                        ("JtObject.getValue: getReadMethod returned null");
                        return (null);
                    }
                    return (m.invoke (obj, null));
                } catch (Exception e) {
                    handleException(e);
                    return (null);
                }
            }
        }
        handleError ("JtObject.getValue:invalid attribute:" + att);
        return (null);

    }


    // Remove a remote object via a proxy

    private Object removeProxy (Object id) {
        JtMessage msg = new JtMessage (JtObject.JtREMOVE_OBJECT);

        if (id == null)
            return (null);
        msg.setMsgContent(id);

        return (((JtInterface) id).processMessage(msg)); //check

    }

    /**
     * Removes an object. The object is removed from the table of children kept by 
     * its parent object. A JtREMOVE message is sent to the object. All Jt objects
     * should process JtREMOVE and release any resources that were allocated 
     * (sockets, Database connections, etc.).  
     * @param id  object id
     */

    public void removeObject (Object id) {
        Object obj;

        handleTrace ("JtObject.removeObject: " + id);

        if (id == null) {
            handleError 
            ("JtObject.removeObject: invalid paramenter (null)");
            return;
        }

        if (id instanceof JtProxy) {
            removeProxy (id);
            return;
        }

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("JtObject.removeObject: object not found: "+
                    id);
            return;
        }

        sendMessage (obj, new JtMessage (JtObject.JtREMOVE));

        //if (id instanceof String)
        //if (remove (calculateAbsoluteName (id)) == null)
        if (remove (id) == null)
            handleError 
            ("JtObject.removeObject: unable to remove object " + id);

    }

    /**
     * Destroys an object (deprecated).  
     * @param id  object id
     */


    public void destroyObject (Object id) {
        //Object obj;
        handleWarning 
        ("JtObject.destroyObject has been deprecated. Please use removeObject");
        removeObject (id);

    }

    // realizeObject: realize an object (being deprecated)

    void realizeObject (Object id) {
        Object obj;

        handleTrace ("JtObject.realizeObject:" + id);

        if (id == null) {
            handleError
            ("JtObject.realizeObject: invalid paramenters");
            return;
        }

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("JtObject.realizeObject: object not found:"+
                    id);
            return;
        }

        ((JtObject) obj).realize ();
    }


    // activateObject: activate an object (being deprecated)

    void activateObject (Object id) {
        Object obj;

        handleTrace ("JtObject.activateObject: " + id);

        if (id == null) {
            handleError 
            ("JtObject.activateObject: invalid parameters");
            return;
        }

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("JtObject.activateObject: object not found: "+
                    id);
            return;
        }

        ((JtObject) obj).activate ();
    }


    // handleMessageTrace 

    private void handleMessageTrace (JtMessage msg) {

        String nl = System.getProperty("line.separator");
        String tmp;



        tmp = "JtMessage.MsgId:" + msg.getMsgId ();
        if (msg.getMsgSubject () != null)
            tmp +=nl+ "JtMessage.MsgSubject:"+ msg.getMsgSubject();
        if (msg.getMsgContent () != null)
            tmp += nl + "JtMessage.MsgContent:"+ msg.getMsgContent();
        handleTrace (tmp);

    }

    private Object object_name (Object obj) {
        String name;

        if (!(obj instanceof JtObject))
            return (obj);

        name = ((JtObject) obj).getObjName (); 
        if (name == null)
            return (obj);

        return (name);
    }

    // Sends a message via a proxy
    /*  
   private Object sendMessageToProxy (Object id, Object msgid) {
     JtMessage msg = new JtMessage (JtObject.JtSEND_MESSAGE);

     if (id == null)
         return (null);
     msg.setMsgContent(id);
     msg.setMsgData (msgid); 
     return (((JtInterface) id).processMessage(msg));

   }
     */   


    /**
     * Sends a Jt Message to another object. 
     *
     * @param id    object ID
     * @param msgid message ID
     */

    public Object sendMessage (Object id, Object msgid) {
        Object obj;
        Object msg;
        Object reply;


        /*
     if (objSession != null)
       handleTrace ("Session(" + objSession + "): JtObject.sendMessage:"+ 
          object_name (id) + ", "+ object_name (msgid) + " ...");
     else
         */
        handleTrace ("JtObject.sendMessage:"+ 
                object_name (id) + ", "+ object_name (msgid) + " ...");

        if (id == null || msgid == null) {
            handleError ("JtObject.sendMessage: invalid paramenters");
            return (null);
        }

        // Use a proxy

        //if (id instanceof JtProxy) {
        //    return (sendMessageToProxy (id, msgid));
        //}

        obj = lookupObject (id);

        if (obj == null) {
            handleError ("JtObject.sendMessage: unable to find object " + id);
            return (null);
        }

        msg = lookupObject (msgid);

        if (msg == null) {
            handleError ("JtObject.sendMessage: unable to find object " + msgid);
            return (null);
        }

        if (msg instanceof JtMessage) 
            handleMessageTrace ((JtMessage) msg);

        // If the recipient object is running in a separate/independent thread,
        // enqueue the message (Asynchronuos processing)

        if (obj instanceof JtThread)
            reply = ((JtThread) obj).enqueueMessage ((Object) msg);
        else 
            reply = ((JtInterface) obj).processMessage ((Object) msg);
        return (reply);

⌨️ 快捷键说明

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