jtvalueobject.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 330 行

JAVA
330
字号
package Jt;
import java.util.*;
import java.lang.reflect.*;
import java.beans.*;
import java.io.*;
import Jt.examples.HelloWorld;


/**
 * Jt Implementation of the J2EE Value Object pattern. This 
 * class relies on introspection to create a hashmap containing the 
 * attribute values.
 */


public class JtValueObject extends JtHashTable {


    private static final long serialVersionUID = 1L;
    private transient Object subject = null;
    private HashMap attributes = null;
    public static final String JtCLASS_NAME = JtValueObject.class.getName();    
    public static final String JtGET_KEYS = "JtGET_KEYS"; 

    public JtValueObject() {
    }

    /**
     * Specifies the subject.
     *
     * @param subject subject
     */

    public void setSubject (Object subject) {
        this.subject = subject; 

    }

    /**
     * Returns the subject (Object whose Value Object needs to be stored).
     */

    public Object getSubject () {
        return (subject);
    }


    /**
     * Specifies the attributes.
     *
     * @param attributes attributes
     */


    public void setAttributes (HashMap attributes) {
        this.attributes = attributes; 

    }


    /**
     * Returns the attributes.
     */

    public HashMap getAttributes () {
        return (attributes);
    }


    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) {
                handleWarning (e.getMessage ());
                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 HashMap calcValueObject () {

        //Object args[];
        PropertyDescriptor[] prop;
        int i;
        Class p;
        Method m;
        BeanInfo info = null;
        Object value;
        HashMap attr;

        if (subject == null) {
            handleError ("getValueObject: the subject attribute needs to be set");
            return (null);
        }

        attr = new HashMap ();

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

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

            if (!checkModifiers (subject.getClass (),prop[i].getName())) {
                //System.out.println ("Skipping (modifiers):" + 
                //prop[i].getName());
                continue;
            }

            //System.out.println ("Attribute:" + 
            //     prop[i].getName());
            p = prop[i].getPropertyType();

            if (!(p.isPrimitive () || Serializable.class.isAssignableFrom (p))) {
                //System.out.println ("Skipping:" + 
                //     prop[i].getName());
                continue;
            }

            try {
                m = prop[i].getReadMethod ();
                if (m == null) {
                    handleWarning
                    ("JtValueObject: getReadMethod returned null");
                    continue;
                    //return (null);
                }

                value = m.invoke (subject, 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 String))
           continue;
                 */
                attr.put (prop[i].getName(), value); 


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

        return (attr);
    }


    private Object getKeys () {
        JtIterator jit = new JtIterator ();
        Collection col;
        HashMap tmp;

        tmp = getHashmap ();
        if (tmp == null)
            return (null);    

        col = tmp.keySet ();
        if (col == null || (col.size () == 0))
            return (null);

        jit.setIterator (col.iterator ());
        return jit;     

    }

    public String toString () {

        JtIterator jit = (JtIterator) getKeys ();
        Object key, value;
        JtMessage msg = new JtMessage (JtIterator.JtNEXT);
        JtMessage msg1 = new JtMessage (JtHashTable.JtGET);
        StringBuffer buffer = null;

        if (jit == null)
            return (null);

        for (;;) {

            key = jit.processMessage (msg);
            if (key == null)
                break; 
            msg1.setMsgData (key);
            value = this.processMessage (msg1);      
            if (buffer == null)
                buffer = new StringBuffer (); 
            buffer.append (key + ":" + value + "\n");
        }    

        return ((buffer == null)?null:buffer.toString ());

    }

    /**
     * Process object messages. 
     * <ul>
     * <li>JtACTIVATE - returns the Value Object associated with the subject.
     * </ul>
     * @param event Jt Message    
     */

    public Object processMessage (Object event) {

        String msgid = null;
        JtMessage e = (JtMessage) event;
        //Object content;


        if (e == null)
            return null;

        msgid = (String) e.getMsgId ();

        if (msgid == null)
            return null;

        // Let the subject process the request


        if (msgid.equals (JtObject.JtREMOVE)) {
            return (null);     
        }

        if (msgid.equals (JtObject.JtACTIVATE)) {

            if (subject == null) {
                handleError ("JtValueObject.process: the subject attribute needs to be set");
                return (null);
            }

            setHashmap (calcValueObject ());
            return (getHashmap ());     
        }

        if (msgid.equals (JtValueObject.JtGET_KEYS)) {
            return (getKeys ());     
        }

        return (super.processMessage (event));


    }



    /**
     * Demonstrates the messages processed by JtValueObject.   
     */

    public static void main(String[] args) {

        JtObject factory = new JtFactory ();
        JtMessage msg;
        HelloWorld helloWorld;
        JtValueObject valueObj;


        // Create an instance of JtValueObject

        valueObj = (JtValueObject)
        factory.createObject (JtValueObject.JtCLASS_NAME, "valueObject");
        helloWorld = (Jt.examples.HelloWorld) factory.createObject (HelloWorld.JtCLASS_NAME, 
        "helloWorld");

        factory.setValue (valueObj, "subject", helloWorld);


        msg = new JtMessage(JtObject.JtACTIVATE);
        factory.sendMessage (valueObj, msg);

        //factory.sendMessage (valueObj, new JtMessage (JtObject.JtPRINT));

        msg = new JtMessage (JtHashTable.JtGET);
        msg.setMsgData ("objName");

        System.out.println (factory.sendMessage (valueObj, msg));

        msg = new JtMessage (JtHashTable.JtGET);
        msg.setMsgData ("greetingMessage");

        System.out.println (factory.sendMessage (valueObj, msg));

        System.out.println (valueObj);
    }

}

⌨️ 快捷键说明

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