1 package Jt; 2 import java.util.*; 3 import java.lang.reflect.*; 4 import java.beans.*; 5 import java.io.*; 6 7 8 12 13 14 public class JtValueObject extends JtHashTable { 15 16 17 private transient Object subject = null; 18 private HashMap attributes = null; 19 20 public JtValueObject() { 21 } 22 23 28 29 public void setSubject (Object subject) { 30 this.subject = subject; 31 32 } 33 34 37 38 public Object getSubject () { 39 return (subject); 40 } 41 42 43 48 49 50 public void setAttributes (HashMap attributes) { 51 this.attributes = attributes; 52 53 } 54 55 56 59 60 public HashMap getAttributes () { 61 return (attributes); 62 } 63 64 65 private boolean checkModifiers (Class cl, String prop) { 66 67 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); 81 } catch (Exception e) { 82 83 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 args[]; 111 PropertyDescriptor[] prop; 112 int i; 113 Class p; 114 Method m; 115 BeanInfo info = null; 116 Object 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 .class); 129 } catch(Exception 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 continue; 141 } 142 143 p = prop[i].getPropertyType(); 146 147 if (!(p.isPrimitive () || Serializable.class.isAssignableFrom (p))) { 148 continue; 151 } 152 153 try { 154 m = prop[i].getReadMethod (); 155 if (m == null) { 156 handleWarning 157 ("JtValueObject: getReadMethod returned null"); 158 continue; 159 } 161 162 value = m.invoke (subject, null); 163 164 174 attr.put (prop[i].getName(), value); 175 176 177 } catch (Exception e) { 178 handleException(e); 179 return (null); 180 } 181 } 182 183 return (attr); 184 } 185 186 187 private Object 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 toString () { 206 207 JtIterator jit = (JtIterator) getKeys (); 208 Object key, value; 209 JtMessage msg = new JtMessage ("JtNEXT"); 210 JtMessage msg1 = new JtMessage ("JtGET"); 211 StringBuffer 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 (); 225 buffer.append (key + ":" + value + "\n"); 226 } 227 228 return ((buffer == null)?null:buffer.toString ()); 229 230 } 231 232 239 240 public Object processMessage (Object event) { 241 242 String msgid = null; 243 JtMessage e = (JtMessage) event; 244 Object content; 245 246 247 if (e == null) 248 return null; 249 250 msgid = (String ) e.getMsgId (); 251 252 if (msgid == null) 253 return null; 254 255 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 282 283 return (super.processMessage (event)); 284 285 286 } 287 288 289 290 293 294 public static void main(String [] args) { 295 296 JtObject main = new JtFactory (); 297 JtMessage msg; 298 Jt.examples.HelloWorld helloWorld; 299 JtValueObject valueObj; 300 301 302 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 318 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
|