KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtValueObject


1 package Jt;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6
7
8 /**
9  * Jt Implementation of the J2EE Value Object pattern. This class relies
10  * on introspection to create a hashmap containing the attribute values.
11  */

12
13
14 public class JtValueObject extends JtHashTable {
15
16
17   private transient Object JavaDoc subject = null;
18   private HashMap attributes = null;
19
20   public JtValueObject() {
21   }
22
23 /**
24   * Specifies the subject.
25   *
26   * @param subject subject
27   */

28
29   public void setSubject (Object JavaDoc subject) {
30      this.subject = subject;
31
32   }
33
34 /**
35   * Returns the subject (Object whose Value Object needs to be stored).
36   */

37
38   public Object JavaDoc getSubject () {
39      return (subject);
40   }
41
42
43 /**
44   * Specifies the attributes.
45   *
46   * @param attributes attributes
47   */

48
49
50   public void setAttributes (HashMap attributes) {
51      this.attributes = attributes;
52
53   }
54
55
56 /**
57   * Returns the attributes.
58   */

59
60   public HashMap getAttributes () {
61      return (attributes);
62   }
63
64
65  private boolean checkModifiers (Class JavaDoc cl, String JavaDoc prop) {
66
67     //Class cl;
68
Field field;
69     int mod;
70
71
72     if (cl == null || prop == null)
73       return (false);
74
75
76     field = null;
77     try {
78       field = cl.getDeclaredField (prop); // property dup property names
79
//System.out.println ("class:" + cl.getName ());
80

81     } catch (Exception JavaDoc e) {
82       
83       //handleException (e);
84

85       if (cl.getSuperclass () == null) {
86         handleWarning (e.getMessage ());
87         return (false);
88       }
89     }
90  
91     if (field == null) {
92       cl = cl.getSuperclass ();
93       return (checkModifiers (cl, prop));
94     }
95
96     mod = field.getModifiers ();
97
98     if (Modifier.isTransient (mod)) {
99       return (false);
100     }
101     if (Modifier.isStatic (mod)) {
102       return (false);
103     }
104     return (true);
105   }
106
107
108   private HashMap calcValueObject () {
109
110    Object JavaDoc args[];
111    PropertyDescriptor[] prop;
112    int i;
113    Class JavaDoc p;
114    Method m;
115    BeanInfo info = null;
116    Object JavaDoc value;
117    HashMap attr;
118
119      if (subject == null) {
120        handleError ("getValueObject: the subject attribute needs to be set");
121        return (null);
122      }
123
124      attr = new HashMap ();
125
126      try {
127        info = Introspector.getBeanInfo(
128               subject.getClass (), java.lang.Object JavaDoc.class);
129      } catch(Exception JavaDoc e) {
130         handleException (e);
131         return (null);
132      }
133
134      prop = info.getPropertyDescriptors();
135      for(i = 0; i < prop.length; i++) {
136
137        if (!checkModifiers (subject.getClass (),prop[i].getName())) {
138        //System.out.println ("Skipping (modifiers):" +
139
//prop[i].getName());
140
continue;
141        }
142
143        //System.out.println ("Attribute:" +
144
// prop[i].getName());
145
p = prop[i].getPropertyType();
146        
147        if (!(p.isPrimitive () || Serializable.class.isAssignableFrom (p))) {
148        //System.out.println ("Skipping:" +
149
// prop[i].getName());
150
continue;
151        }
152
153        try {
154          m = prop[i].getReadMethod ();
155          if (m == null) {
156            handleWarning
157          ("JtValueObject: getReadMethod returned null");
158              continue;
159              //return (null);
160
}
161
162          value = m.invoke (subject, null);
163
164 /*
165          if (!(value instanceof Integer ||
166              value instanceof Long ||
167              value instanceof Float ||
168              value instanceof Byte ||
169              value instanceof Boolean ||
170              value instanceof Short ||
171              value instanceof Double || value instanceof String))
172            continue;
173 */

174          attr.put (prop[i].getName(), value);
175
176
177         } catch (Exception JavaDoc e) {
178          handleException(e);
179          return (null);
180         }
181       }
182
183       return (attr);
184    }
185
186
187   private Object JavaDoc getKeys () {
188     JtIterator jit = new JtIterator ();
189     Collection col;
190     HashMap tmp;
191
192     tmp = getHashmap ();
193     if (tmp == null)
194       return (null);
195
196     col = tmp.keySet ();
197     if (col == null || (col.size () == 0))
198       return (null);
199
200     jit.setIterator (col.iterator ());
201     return jit;
202
203   }
204
205   public String JavaDoc toString () {
206
207     JtIterator jit = (JtIterator) getKeys ();
208     Object JavaDoc key, value;
209     JtMessage msg = new JtMessage ("JtNEXT");
210     JtMessage msg1 = new JtMessage ("JtGET");
211     StringBuffer JavaDoc buffer = null;
212
213     if (jit == null)
214       return (null);
215
216     for (;;) {
217             
218       key = jit.processMessage (msg);
219       if (key == null)
220         break;
221       msg1.setMsgData (key);
222       value = this.processMessage (msg1);
223       if (buffer == null)
224        buffer = new StringBuffer JavaDoc ();
225       buffer.append (key + ":" + value + "\n");
226     }
227
228     return ((buffer == null)?null:buffer.toString ());
229
230   }
231
232   /**
233     * Process object messages.
234     * <ul>
235     * <li>JtACTIVATE - returns the Value Object associated with the subject.
236     * </ul>
237     * @param event Jt Message
238     */

239
240   public Object JavaDoc processMessage (Object JavaDoc event) {
241
242    String JavaDoc msgid = null;
243    JtMessage e = (JtMessage) event;
244    Object JavaDoc content;
245
246
247      if (e == null)
248     return null;
249
250      msgid = (String JavaDoc) e.getMsgId ();
251
252      if (msgid == null)
253     return null;
254
255      // Let the subject process the request
256

257
258      if (msgid.equals ("JtREMOVE")) {
259        return (null);
260      }
261
262      if (msgid.equals ("JtACTIVATE")) {
263
264        if (subject == null) {
265          handleError ("JtValueObject.process: the subject attribute needs to be set");
266          return (null);
267        }
268
269        setHashmap (calcValueObject ());
270        return (getHashmap ());
271      }
272
273      if (msgid.equals ("JtGET_KEYS")) {
274        return (getKeys ());
275      }
276
277 /*
278      if (msgid.equals ("JtPRINT")) {
279        return (printObject ());
280      }
281 */

282
283      return (super.processMessage (event));
284
285
286   }
287
288
289
290   /**
291     * Unit Tests the messages processed by JtValueObject.
292     */

293
294   public static void main(String JavaDoc[] args) {
295
296     JtObject main = new JtFactory ();
297     JtMessage msg;
298     Jt.examples.HelloWorld helloWorld;
299     JtValueObject valueObj;
300
301
302     // Create an instance of JtValueObject
303

304     valueObj = (JtValueObject)
305       main.createObject ("Jt.JtValueObject",
306       "valueObject");
307     helloWorld = (Jt.examples.HelloWorld) main.createObject ("Jt.examples.HelloWorld",
308       "helloWorld");
309
310     main.setValue (valueObj, "subject", helloWorld);
311
312
313     msg = new JtMessage("JtACTIVATE");
314     main.sendMessage (valueObj, msg);
315          
316     //main.sendMessage (valueObj, new JtMessage ("JtPRINT_OBJECT"));
317

318     //System.out.println (main.getValue (valueObj, "objName"));
319
//System.out.println (main.getValue (valueObj, "greetingMessage"));
320

321     msg = new JtMessage ("JtGET");
322     msg.setMsgData ("objName");
323
324     System.out.println (main.sendMessage (valueObj, msg));
325
326     msg = new JtMessage ("JtGET");
327     msg.setMsgData ("greetingMessage");
328
329     System.out.println (main.sendMessage (valueObj, msg));
330
331     System.out.println (valueObj);
332   }
333
334 }
335
Popular Tags