1 2 package Jt; 3 4 import java.io.*; 5 import java.util.*; 6 import java.text.*; 7 import java.lang.reflect.*; 8 import java.beans.*; 9 10 11 12 16 17 18 public class JtObject implements Serializable, JtInterface { 19 20 private static String version = "1.5 - 11/01/06"; private Hashtable objTable = null; private static Hashtable resTable = null; private static int initted = 0; 24 private static int log = 0; 25 private static String logFile = null; private static PrintWriter logStream = null; 27 private static String resourceFile = ".Jtrc"; private String objName = null; private int objTrace = 0; private Object objException; private int objStatus = 0; 33 private long objId; private String objStore = ".Jt"; 39 private String objPath = null; 40 41 42 43 44 public JtObject () { 45 46 48 if (initted == 0) { 49 initialize (); 50 initted = 1; 51 } 52 if (log == 1) 53 setObjTrace (1); 54 } 55 56 57 66 67 public Object createObject (Object class_name, Object id) { 68 Object obj, tmp = null; 69 Class jtclass; 70 71 72 handleTrace ("JtObject.createObject:" + class_name + "," + id); 73 74 if (class_name == null || id == null) { 75 handleError ("JtObject.createObject: invalid paramenters"); 76 return (null); 77 } 78 79 80 84 85 obj = lookupObject (id); 86 87 if (obj != null) { 89 if (log == 0) 90 handleWarning 91 ("JtObject.createObject: unable to create a duplicate entry " + id); 92 else 93 handleError 94 ("JtObject.createObject: unable to create a duplicate entry " + id); 95 return (null); 96 } 97 98 try { 99 jtclass = Class.forName ((String ) class_name); 100 } catch (Exception e) { 101 handleException (e); 102 return (null); 103 } 104 105 107 try { 108 109 110 if (JtSingleton.class.isAssignableFrom (jtclass)) { 111 obj = JtSingleton.getInstance (); 112 if (obj == null) { 113 obj = jtclass.newInstance (); 114 JtSingleton.setInstance (obj); 115 tmp = JtSingleton.getInstance (); 116 if (tmp != obj) { 119 handleError 120 ("JtObject.createObject: attempt to create another instance of a Singleton (" + 121 class_name + ")"); 122 return (null); 123 } 124 125 126 } else { 127 handleError 128 ("JtObject.createObject: attempt to create another instance of a Singleton (" + 129 class_name + ")"); 130 return (null); 131 } 132 } else 133 obj = jtclass.newInstance (); 134 135 if (obj instanceof JtObject) { 136 ((JtObject)obj).setObjName (absolute_name ((String ) id)); 137 if (log == 1) 138 ((JtObject)obj).setObjTrace (1); 139 else 140 ((JtObject)obj).setObjTrace (this.getObjTrace ()); 141 } 142 143 add (absolute_name ((String ) id), obj); 146 load_object_resources (obj, class_name); 147 } catch (Exception e) { 148 handleException (e); 149 } 150 151 152 return (obj); 153 } 154 155 156 157 159 String absolute_name (String name) { 160 if (name == null) 161 return (null); 162 163 if (name.indexOf (".") > 0) 164 return (name); 165 166 if (this.getObjName () == null) 167 return (name); 168 169 170 return (this.getObjName () + "." + name); 171 172 } 173 174 175 String calculateAbsoluteName (Object id) { 176 String name; 177 178 if (id == null) 179 return (null); 180 181 if (id instanceof JtObject) { 182 if (((JtObject) id).getObjName () == null) 183 handleWarning ("calculateAbsoluteName:JObject is missing name" + 184 ((JtObject) id).getObjName ()); 185 return (((JtObject) id).getObjName ()); 186 } 187 188 if (id instanceof String ) { 189 name = (String ) id; 190 } else 191 name = id.toString (); 192 193 if (name.indexOf (".") > 0) 194 return (name); 195 196 if (this.getObjName () == null) 197 return (name); 198 199 return (this.getObjName () + "." + name); 200 201 } 202 203 204 209 210 public Object lookupObject (Object id) { 211 String obj_id; 212 Hashtable ht; 213 214 if (!id.getClass().getName().equals ("java.lang.String")) 215 return (id); 217 218 obj_id = absolute_name ((String ) id); 219 220 222 if (objTable == null) 223 return (null); 224 225 return (objTable.get (obj_id)); 226 228 239 } 240 241 242 243 private boolean checkModifiers (Class cl, String prop) { 244 245 Field field; 247 int mod; 248 249 250 if (cl == null || prop == null) 251 return (false); 252 253 254 field = null; 255 try { 256 field = cl.getDeclaredField (prop); 259 } catch (Exception e) { 260 261 263 if (cl.getSuperclass () == null) { 264 handleException (e); 265 return (false); 266 } 267 } 268 269 if (field == null) { 270 cl = cl.getSuperclass (); 271 return (checkModifiers (cl, prop)); 272 } 273 274 mod = field.getModifiers (); 275 276 if (Modifier.isTransient (mod)) { 277 return (false); 278 } 279 if (Modifier.isStatic (mod)) { 280 return (false); 281 } 282 return (true); 283 } 284 285 private Object copyObject (Object obj, Object target) { 286 Object args[]; 287 PropertyDescriptor[] prop; 288 Class p; 289 Method m; 290 BeanInfo info = null; 291 Object value, svalue = null; 292 int i; 293 294 295 if (obj == null || target == null) 296 return (null); 297 298 if (obj.getClass () != target.getClass ()) { 299 handleError 300 ("Jt.copyObject: object classes should be the same (source and target)"); 301 return (null); 302 } 303 304 try { 305 info = Introspector.getBeanInfo( 306 obj.getClass (), java.lang.Object .class); 307 } catch(Exception e) { 308 handleException (e); 309 return (null); } 311 312 prop = info.getPropertyDescriptors(); 313 314 315 for(i = 0; i < prop.length; i++) { 316 317 if (!checkModifiers (obj.getClass (),prop[i].getName())) { 318 continue; 319 } 320 321 value = getValue (obj, prop[i].getName()); 323 svalue = null; 324 if (value instanceof Integer || 325 value instanceof Long || 326 value instanceof Float || 327 value instanceof Byte || 328 value instanceof Boolean || 329 value instanceof Short || 330 value instanceof Double ) { 331 svalue = value.toString (); 332 } else 333 svalue = value; 334 335 336 setValue (target, prop[i].getName(), svalue); 338 339 } 340 341 return (null); } 343 344 345 346 355 356 public void setValue (Object id, Object att, Object value) { 357 358 Object obj; 359 Object args[]; 360 PropertyDescriptor[] prop; 361 int i; 362 Class p; 363 Method m; 364 BeanInfo info = null; 365 366 371 handleTrace ("JtObject.setValue:" + id + "," + att + "," + value); 372 373 if (id == null | att == null) { 374 handleError ("JtObject.setValue: invalid paramenters"); 375 return; 376 } 377 378 obj = lookupObject (id); 379 380 if (obj == null) { 381 handleError ("setValue: unable to find object: " + id); 382 return; 383 } 384 385 391 392 try { 393 394 info = Introspector.getBeanInfo( 395 obj.getClass (), java.lang.Object .class); 396 } catch(Exception e) { 397 handleException (e); 398 return; 399 } 400 401 prop = info.getPropertyDescriptors(); 402 for(i = 0; i < prop.length; i++) { 403 404 if (prop[i].getName().equals ((String ) att)) { 405 406 p = prop[i].getPropertyType(); 407 args = new Object [1]; 408 412 try { 413 if (p.getName().equals ("java.lang.String") ){ 414 args[0] = value; 415 } else if (p.getName().equals ("byte") ){ 416 args[0] = (value instanceof String )?Byte.valueOf ((String ) value):value; 417 } else if (p.getName().equals ("short") ){ 418 args[0] = (value instanceof String )?Short.valueOf ((String ) value):value; 419 } else if (p.getName().equals ("int") ){ 420 args[0] = (value instanceof String )?Integer.valueOf ((String ) value):value; 421 } else if (p.getName().equals ("long") ){ 423 args[0] = (value instanceof String )?Long.valueOf ((String ) value):value; 424 } else if (p.getName().equals ("float") ){ 425 args[0] = (value instanceof String )?Float.valueOf ((String ) value):value; 426 } else if (p.getName().equals ("double") ){ 427 args[0] = (value instanceof String )?Double.valueOf ((String ) value):value; 428 } else if (p.getName().equals ("boolean") ){ 429 args[0] = (value instanceof String )?Boolean.valueOf ((String ) value):value; 430 } else if (p.getName().equals ("java.util.Date") ){ 431 DateFormat df = DateFormat.getDateInstance(); 432 if (value instanceof String ) 434 try { 435 args[0] = df.parse ((String ) value); 437 } catch (Exception e) { 439 handleException (e); 440 return; 441 } 442 else 443 args[0] = value; 444 } else 445 args[0] = value; 446 } catch (Exception e) { 447 handleException (e); 448 return; 449 } 450 451 try { 452 m = prop[i].getWriteMethod (); 453 if (m == null) { 454 handleError 455 ("JtObject.setValue failed:missing setter for attribute " + att); 456 return; 457 } 458 m.invoke (obj, args); 459 } catch (Exception e) { 460 handleException (e); 461 } 462 return; 463 464 } 465 } 466 467 handleError ("JtObject.setValue: invalid attribute:"+ att); 468 469 470 } 471 472 473 483 484 public Object getValue (Object id, Object att) { 485 Object obj; 486 Method m; 487 Class p; 488 BeanInfo info = null; 489 PropertyDescriptor[] prop; 490 int i; 491 492 handleTrace ("JtObject.getValue: " + id + "," + att); 493 494 if (id == null || att == null) { 495 handleError ("JtObject.getValue: invalid paramenters"); 496 return (null); 497 } 498 499 obj = lookupObject (id); 500 501 if (obj == null) { 502 handleError ("getValue: unable to find object " + id); 503 return (null); 504 } 505 510 511 try { 512 513 info = Introspector.getBeanInfo(obj.getClass (), 514 java.lang.Object .class); 515 516 } catch(Exception e) { 517 handleException (e); 518 return (null); 519 } 520 521 prop = info.getPropertyDescriptors(); 522 523 for(i = 0; i < prop.length; i++) { 524 525 526 if (prop[i].getName().equals ((String ) att)) { 527 528 try { 529 m = prop[i].getReadMethod (); 530 if (m == null) { 531 handleError 532 ("JtObject.getValue: getReadMethod returned null"); 533 return (null); 534 } 535 return (m.invoke (obj, null)); 536 } catch (Exception e) { 537 handleException(e); 538 return (null); 539 } 540 } 541 } 542 handleError ("JtObject.getValue:invalid attribute:" + att); 543 return (null); 544 545 } 546 547 548 555 556 public void removeObject (Object id) { 557 Object obj; 558 559 handleTrace ("JtObject.removeObject: " + id); 560 561 if (id == null) { 562 handleError 563 ("JtObject.removeObject: invalid paramenters"); 564 return; 565 } 566 567 obj = lookupObject (id); 568 569 if (obj == null) { 570 handleError ("JtObject.removeObject: object not found: "+ 571 id); 572 return; 573 } 574 575 sendMessage (obj, new JtMessage ("JtREMOVE")); 576 577 if (remove (calculateAbsoluteName (id)) == null) 579 handleError 580 ("JtObject.removeObject: unable to remove object " + id); 581 582 } 583 584 588 589 590 public void destroyObject (Object id) { 591 Object obj; 592 handleWarning 593 ("JtObject.destroyObject has been deprecated. Please use removeObject"); 594 removeObject (id); 595 596 619 } 620 621 623 void realizeObject (Object id) { 624 Object obj; 625 626 handleTrace ("JtObject.realizeObject:" + id); 627 628 if (id == null) { 629 handleError 630 ("JtObject.realizeObject: invalid paramenters"); 631 return; 632 } 633 634 obj = lookupObject (id); 635 636 if (obj == null) { 637 handleError ("JtObject.realizeObject: object not found:"+ 638 id); 639 return; 640 } 641 642 ((JtObject) obj).realize (); 643 } 644 645 646 648 void activateObject (Object id) { 649 Object obj; 650 651 handleTrace ("JtObject.activateObject: " + id); 652 653 if (id == null) { 654 handleError 655 ("JtObject.activateObject: invalid parameters"); 656 return; 657 } 658 659 obj = lookupObject (id); 660 661 if (obj == null) { 662 handleError ("JtObject.activateObject: object not found: "+ 663 id); 664 return; 665 } 666 667 ((JtObject) obj).activate (); 668 } 669 670 671 673 void save () { 674 ObjectOutputStream p = null; 675 BufferedOutputStream ostream = null; 676 String fpath = null; 677 678 if (objPath == null) { 679 680 if (this.getObjName () != null) { 681 fpath = ".Jt/" + this.getObjName (); 682 } else { 683 fpath = ".Jt/" + this.getObjId (); 684 } 685 686 } else 687 fpath = objPath; 688 689 create_dir (fpath); 691 692 try { 693 694 ostream = new BufferedOutputStream (new FileOutputStream (fpath)); 695 696 p = new ObjectOutputStream(ostream); 697 } catch (Exception e) { 698 handleException (e); 699 return; 700 } 701 702 703 try { 704 p.writeObject(this); 705 p.flush(); 706 p.close(); 707 } catch (Exception e) { 708 handleException (e); 709 } 710 } 711 712 713 715 Object restore () { 716 ObjectInputStream p = null; 717 BufferedInputStream istream = null; 718 String fpath; 719 Object obj = null; 720 File file = null; 721 722 723 if (objPath == null) 724 if (this.getObjName () != null) { 725 fpath = ".Jt/" + this.getObjName (); 726 } else { 727 fpath = ".Jt/" + this.getObjId (); 728 } 729 else 730 fpath = objPath; 731 732 handleTrace ("Jt.restore: " + fpath); 733 734 if (fpath == null) 735 return (null); 736 737 file = new File (fpath); 738 739 if (!file.exists ()) { 740 handleTrace ("Jt.restore: " + fpath + " does not exists"); 741 return (null); 742 } 743 744 try { 746 istream = new BufferedInputStream (new FileInputStream 747 (fpath)); 748 749 p = new ObjectInputStream(istream); 750 } catch (Exception e) { 751 handleException (e); 752 return (null); 753 } 754 755 try { 756 obj = p.readObject(); 757 p.close(); 758 } catch (Exception e) { 759 handleException (e); 760 } 761 762 return (obj); 763 764 } 765 766 767 768 770 private void handle_message_trace (JtMessage msg) { 771 772 String nl = System.getProperty("line.separator"); 773 String tmp; 774 775 776 777 tmp = "JtMessage.MsgId:" + msg.getMsgId (); 778 if (msg.getMsgSubject () != null) 779 tmp +=nl+ "JtMessage.MsgSubject:"+ msg.getMsgSubject(); 780 if (msg.getMsgContent () != null) 781 tmp += nl + "JtMessage.MsgContent:"+ msg.getMsgContent(); 782 handleTrace (tmp); 783 784 } 785 786 private Object object_name (Object obj) { 787 String name; 788 789 if (!(obj instanceof JtObject)) 790 return (obj); 791 792 name = ((JtObject) obj).getObjName (); 793 if (name == null) 794 return (obj); 795 796 return (name); 797 } 798 799 805 806 public Object sendMessage (Object id, Object msgid) { 807 Object obj; 808 Object msg; 809 Object reply; 810 811 812 818 handleTrace ("JtObject.sendMessage:"+ 819 object_name (id) + ", "+ object_name (msgid) + " ..."); 820 821 if (id == null || msgid == null) { 822 handleError ("JtObject.sendMessage: invalid paramenters"); 823 return (null); 824 } 825 826 obj = lookupObject (id); 827 828 if (obj == null) { 829 handleError ("JtObject.sendMessage: unable to find object " + id); 830 return (null); 831 } 832 833 msg = lookupObject (msgid); 834 835 if (msg == null) { 836 handleError ("JtObject.sendMessage: unable to find object " + msgid); 837 return (null); 838 } 839 840 if (msg instanceof JtMessage) 841 handle_message_trace ((JtMessage) msg); 842 843 846 if (obj instanceof JtThread) 847 reply = ((JtThread) obj).enqueueMessage ((Object ) msg); 848 else 849 reply = ((JtInterface) obj).processMessage ((Object ) msg); 850 return (reply); 851 852 } 853 854 855 private Object encodeObject (Object obj) { 856 857 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 858 XMLEncoder e; 859 Object result = null; 860 861 862 if (obj == null) 863 return (null); 864 865 try { 866 867 e = new XMLEncoder( 868 new BufferedOutputStream(stream)); 869 e.writeObject(obj); 870 e.close(); 871 result = stream.toString (); 872 873 } catch (Exception ex) { 874 handleException (ex); 875 return (null); 876 } 877 return (result); 878 879 } 880 881 private Object decodeObject (Object obj) { 882 883 ByteArrayInputStream stream; 884 Object result = null; 885 XMLDecoder d; 886 887 888 if (obj == null) 889 return (null); 890 891 stream = new ByteArrayInputStream (((String ) obj).getBytes ()); 892 893 try { 894 895 d = new XMLDecoder( 896 new BufferedInputStream(stream)); 897 result = d.readObject(); 898 d.close(); 899 900 } catch (Exception ex) { 901 handleException (ex); 902 } 903 return (result); 904 905 } 906 907 913 914 915 public Object processMessage (Object message) { 916 917 String msgid; 918 JtMessage msg = (JtMessage) message; 919 920 if (msg == null) 922 return (null); 923 924 msgid = (String ) msg.getMsgId (); 925 926 if (msgid == null) 927 return (null); 928 929 if (msgid.equals ("JtREMOVE")) { 931 return (null); 932 } 933 934 if (msgid.equals ("JtSET_VALUE")) { 935 setValue (msg.getMsgSubject (), 936 msg.getMsgContent (), 937 msg.getMsgData()); 938 return (null); 939 } 940 941 if (msgid.equals ("JtGET_VALUE")) { 942 if (msg.getMsgSubject () == null) 943 return (getValue (this, 944 msg.getMsgContent ())); 945 else 946 return (getValue (msg.getMsgSubject (), 947 msg.getMsgContent ())); 948 949 } 950 951 if (msgid.equals ("JtCREATE_OBJECT")) { 952 createObject (msg.getMsgContent (), 953 msg.getMsgData()); 954 return (null); 955 } 956 957 if (msgid.equals ("JtREMOVE_OBJECT")) { 958 removeObject (msg.getMsgContent ()); 959 return (null); 960 } 961 962 if (msgid.equals ("JtSEND_MESSAGE")) { 963 return (sendMessage (msg.getMsgContent (), 964 msg.getMsgData ())); 965 966 } 967 968 if (msgid.equals ("JtCOPY_OBJECT")) { 969 return (copyObject (msg.getMsgContent (), 970 msg.getMsgData ())); 971 972 } 973 974 976 if (msgid.equals ("JtENCODE_OBJECT")) { 977 return (encodeObject (msg.getMsgContent ())); 978 } 979 980 if (msgid.equals ("JtDECODE_OBJECT")) { 981 return (decodeObject (msg.getMsgContent ())); 982 } 983 984 if (msgid.equals ("JtPRINT_OBJECT")) { 985 System.out.println (encodeObject (this)); 986 return (this); 987 } 988 989 handleError ("JtObject.processMessage: invalid msg ID:" 990 + msg.getMsgId ()); 991 return (null); 992 } 993 994 996 private void open_logfile () { 997 FileOutputStream fs; 998 999 if (logFile == null || logFile.equals ("")) 1000 return; 1001 1002 if (logFile.equals ("stderr")) { logStream = null; 1004 return; 1005 } 1006 1007 try { 1008 fs = new FileOutputStream (logFile); 1009 logStream = new PrintWriter (fs); 1010 } catch (Exception e) { 1011 logStream = null; 1012 handleException (e); 1013 return; 1014 } 1015 1016 handleTrace ("JtObject.open_logfile: opened log file ... " + 1017 logFile); 1018 } 1023 1024 1026 void initialize () { 1027 1028 String rfile; 1029 1030 logFile = System.getProperty ("Log"); 1031 rfile = System.getProperty ("Resources"); 1032 1033 if (rfile != null) 1034 setResourceFile (rfile); 1035 1036 if (logFile != null) { 1037 log = 1; this.setObjTrace (1); 1039 open_logfile (); 1040 } 1041 1044 handleTrace ("Initializing Jt " + version + "..."); 1045 load_resources (); 1046 1047 } 1048 1049 1050 void realize () { 1052 1053 } 1054 1055 void activate () { 1057 1058 } 1059 1060 1061 void destroy () { 1063 1064 } 1065 1066 1074 1075 public void handleException (Throwable e) { 1076 1077 objException = e; 1079 e.printStackTrace (); 1080 1081 if (logStream == null) 1082 return; 1083 1084 logStream.println (e); 1085 logStream.flush (); 1086 } 1087 1088 1089 1096 1097 public void handleTrace (String msg) { 1098 1099 if (objTrace <= 0) 1100 return; 1101 1102 if (logStream == null) { 1103 System.err.println (msg); 1104 return; 1105 } 1106 1107 logStream.println (msg); 1108 logStream.flush (); 1109 } 1110 1111 1118 1119 public void handleWarning (String msg) { 1120 1121 1124 1125 if (logStream == null) { 1126 System.err.println (msg); 1127 return; 1128 } 1129 1130 logStream.println (msg); 1131 logStream.flush (); 1132 } 1133 1134 1136 1144 1145 public void handleError (String msg) { 1146 try { 1147 1148 throw new JtException (msg); 1150 1151 } catch (JtException e) { 1152 1153 handleException (e); 1154 } 1155 1156 } 1157 1158 1159 1160 1162 private void add (String id, Object obj) { 1163 1165 Hashtable ht; 1166 1167 if (id == null || obj == null) 1168 return; 1169 1170 1171 1173 if (objTable == null) 1174 objTable = new Hashtable (); 1175 objTable.put (id, obj); 1176 return; 1178 1180 1197 } 1198 1199 1201 private Object remove (Object id) { 1203 1204 Hashtable ht; 1205 1206 if (id == null) 1207 return (null); 1208 1209 1211 if (objTable == null) 1212 return (null); 1213 1214 if (objTable.get (id) == null) 1215 handleError ("JtObject.remove: unable to remove object (not found):" + 1216 id); 1217 1218 return (objTable.remove (id)); 1219 1221 1234 } 1235 1236 1237 1238 1241 1242 1243 public String getLogFile() { 1244 return logFile; 1245 } 1246 1247 1248 1252 1253 public void setLogFile(String newLogFile) { 1254 1255 if (newLogFile != null && logFile != null) 1256 if (newLogFile.equals(logFile)) 1257 return; 1258 1259 logFile = newLogFile; 1260 1261 log = 1; 1262 this.setObjTrace (1); 1263 if (logFile != null) 1264 open_logfile (); 1265 1266 } 1267 1268 1269 1273 1274 public String getResourceFile() { 1275 return resourceFile; 1276 } 1277 1278 1279 1284 1285 public void setResourceFile(String newResourceFile) { 1286 resourceFile = newResourceFile; 1287 1288 if (resourceFile != null) 1289 load_resources (); 1290 1291 } 1292 1293 1294 1297 1298 public String getObjName() { 1299 return objName; 1300 } 1301 1302 1303 1306 1307 public void setObjName(String newObjName) { 1308 objName = newObjName; 1309 } 1310 1311 1313 1314 long getObjId() { 1315 return objId; 1316 } 1317 1318 void setObjId(long newObjId) { 1319 objId = newObjId; 1320 } 1321 1322 1323 1324 1327 1328 public int getObjTrace() { 1329 return objTrace; 1330 } 1331 1332 1335 1336 public void setObjTrace(int newObjTrace) { 1337 objTrace = newObjTrace; 1338 } 1339 1340 1341 1345 1346 public int getObjStatus() { 1347 return objStatus; 1348 } 1349 1350 1351 1355 1356 public void setObjStatus(int newObjStatus) { 1357 objStatus = newObjStatus; 1358 } 1359 1360 1362 1371 1372 1377 1378 public Object getObjException() { 1379 return objException; 1380 } 1381 1382 1387 1388 public void setObjException(Object newObjException) { 1389 objException = newObjException; 1390 } 1391 1392 1393 1395 1400 1401 1408 1409 1411 void create_dir (String name) { 1412 File file; 1413 String parentpath; 1414 1415 if (name == null) 1416 return; 1417 1418 file = new File (name); 1419 if (file == null) { 1420 handleError ("JtFile: unable to create File object:" + name); 1421 return; 1422 } 1423 1424 parentpath = file.getParent (); 1425 if (parentpath == null) 1426 return; 1427 1428 file = new File (parentpath); 1430 if (file == null) { 1431 handleError ("JtFile: unable to get parent File:" + name); 1432 return; 1433 } 1434 1435 if (!file.exists ()) 1436 if (!file.mkdirs ()) 1437 handleError ("JtFile: unable to create directories:" + 1438 parentpath); 1439 } 1440 1441 1442 JtResource parse_resource (String line) { 1443 1444 String resource_class, tmp; 1445 int index; 1446 int length; 1447 JtResource res; 1448 1449 if (line == null) 1450 return (null); 1451 1452 length = line.length (); 1453 1454 if (length == 0) 1455 return (null); 1456 1457 index = line.indexOf (":"); 1458 1459 if (index < 0) 1460 return (null); 1461 1462 if (index == length - 1) 1463 return (null); 1464 1465 res = new JtResource (); 1466 1467 res.value = line.substring (index+1, length); 1468 1469 tmp = line.substring (0, index); 1470 1471 index = tmp.lastIndexOf ("."); 1472 1473 if (index == -1) 1474 return (null); 1475 1476 if (index == tmp.length () - 1) 1477 return (null); 1479 res.rclass = tmp.substring (0, index); 1480 res.name = tmp.substring (index + 1); 1481 1482 handleTrace ("Jt Resource: " + "(" + 1483 res.rclass + "," + res.name + "," + res.value 1484 + ")"); 1485 1486 return (res); 1487 1488 } 1489 1490 private void load_object_resources (Object obj, 1491 Object class_name) { 1492 1493 Hashtable ht; 1494 Enumeration rnames; 1495 Object resource; 1496 1497 1498 if (resTable == null) 1499 resTable = new Hashtable (); 1500 1501 if (obj == null || class_name == null) 1502 return; 1503 1504 ht = (Hashtable) resTable.get (class_name); 1505 1506 if (ht == null) 1507 return; 1509 rnames = ht.keys (); 1510 if (rnames == null) 1511 return; 1512 1513 handleTrace ("Jt: Loading Jt resources into object ..."); 1514 while (rnames.hasMoreElements ()) { 1515 resource = rnames.nextElement (); 1516 setValue (obj, resource, ht.get (resource)); 1517 } 1518 handleTrace ("Jt: Loaded Jt resources"); 1519 } 1520 1521 private void update_resources (JtResource res) { 1522 Hashtable ht; 1523 1524 if (res == null) 1525 return; 1526 1527 if (res.rclass == null) 1528 return; 1529 1530 if (resTable == null) 1531 resTable = new Hashtable (); 1532 1533 if (resTable.get (res.rclass) == null) { 1534 resTable.put (res.rclass, new Hashtable ()); 1535 } 1536 1537 ht = (Hashtable) resTable.get (res.rclass); 1538 1539 if (ht == null) 1540 return; 1542 ht.put (res.name, res.value); 1543 } 1544 1545 void load_resources ( ) { 1546 String line; 1547 JtResource res; 1548 File file; 1549 1550 if (resourceFile == null) 1551 return; 1552 1553 file = new File (resourceFile); 1554 1555 if (!file.exists ()) 1556 return; 1557 1558 handleTrace ("Jt: reading resources from " + 1559 resourceFile + " ..."); 1560 try { 1561 BufferedReader d = new BufferedReader 1562 (new FileReader (resourceFile)); 1563 1564 while ((line = d.readLine ()) != null) { 1565 if (line.startsWith ("!") || 1567 line.startsWith ("//")) 1568 continue; 1569 res = parse_resource (line); 1570 1571 if (res != null) 1572 update_resources (res); 1573 } 1574 1575 } catch (Exception ex) { 1576 handleException (ex); 1577 } 1578 } 1579 1580 1583 1584 public static void main(String [] args) { 1585 1586 JtObject main = new JtObject (); 1587 JtMessage msg; 1588 String tmp; 1589 JtObject main1; 1590 1591 1595 1596 1598 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 1599 1600 1601 1602 if (main.lookupObject ("message") != null) 1603 System.out.println ("Jt.createObject: GO"); 1604 else 1605 System.out.println ("Jt.createObject: FAILED"); 1606 1607 1609 main.setValue ("message", "msgId", "JtTestId"); 1610 1611 if (msg.getMsgId () != null) 1612 if (msg.getMsgId().equals ("JtTestId")) 1613 System.out.println ("Jt.setValue: GO"); 1614 else 1615 System.out.println ("Jt.setValue: FAILED"); 1616 else 1617 System.out.println ("Jt.setValue: FAILED"); 1618 1619 1620 1622 1624 tmp = (String ) main.getValue ("message", "msgId"); 1625 1626 if (tmp != null) 1627 if (tmp.equals ("JtTestId")) 1628 System.out.println ("Jt.getValue: GO"); 1629 else 1630 System.out.println ("Jt.getValue: FAILED"); 1631 else 1632 System.out.println ("Jt.getValue: FAILED"); 1633 1634 main.removeObject ("message"); 1635 1636 if (main.lookupObject ("message") == null) { 1637 System.out.println ("Jt.removeObject: GO"); 1638 } else 1639 System.out.println ("Jt.removeObject: FAILED"); 1640 1641 1643 main.setObjName ("main"); 1644 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 1645 1646 if (main.lookupObject ("main.message") != null) { 1647 System.out.println ("Jt.createObject (main.message): GO"); 1648 } else 1649 System.out.println ("Jt.createObject (main.message): FAILED"); 1650 1651 if (main.lookupObject ("message") != null) { 1652 System.out.println ("Jt.lookupObject (main.message): GO"); 1653 } else 1654 System.out.println ("Jt.lookupObject (main.message): FAILED"); 1655 1656 main.removeObject ("message"); 1657 1658 if (main.lookupObject ("message") == null 1659 && main.lookupObject ("main.message") == null) { 1660 System.out.println ("Jt.removeObject (main.message): GO"); 1661 } else 1662 System.out.println ("Jt.removeObject (main.message): FAILED"); 1663 1664 1665 1678 1679 1681 main.handleTrace ("JtObject: this is a trace"); 1683 1684 1685 1686 } 1687} 1688
| Popular Tags
|