1 2 package Jt.ejb; 3 4 5 import javax.ejb.*; 6 import javax.naming.*; 7 import java.rmi.*; 8 9 import java.beans.*; 10 import java.io.*; 11 import java.util.*; 12 import Jt.*; 13 import Jt.xml.*; 14 15 16 19 20 public class JtEJBAdapter extends JtAdapter { 21 22 JtXMLConverter xmlConverter = null; 23 private String url = null; 24 private String remoteLogFile; 25 JtSessionFacadeHome dhome = null; 26 JtSessionFacade sejb = null; 27 Object session = null; 29 30 public JtEJBAdapter () { 31 } 32 33 34 35 36 39 40 public String getRemoteLogFile () { 41 return (remoteLogFile); 42 } 43 44 45 49 50 public void setRemoteLogFile (String remoteLogFile) { 51 this.remoteLogFile = remoteLogFile; 52 53 if (session == null) 54 initializeEJB (); 55 if (session == null) 56 return; 57 try { 58 sejb.setLogFile(session, remoteLogFile); 59 } catch (Exception ex) { 60 handleException (ex); 61 } 62 63 64 } 65 66 67 68 69 70 72 89 90 91 private void getEJBHandle () { 92 93 if (sejb == null) { 94 95 dhome = (JtSessionFacadeHome) locateJtService (); 96 if (dhome == null) 97 return; 98 try { 99 sejb = dhome.create(); 100 } catch (Exception ex) { 101 handleException (ex); 102 } 103 } 104 } 105 106 107 private Object getSessionHandle () { 108 109 if (sejb == null) 110 return (null); 111 try { 112 return (sejb.createSession ()); 113 } catch (Exception ex) { 114 handleException (ex); 115 } 116 return (null); 117 118 } 119 120 122 private void initializeEJB () { 123 124 125 if (sejb == null) 126 getEJBHandle (); 127 128 if (sejb == null) 129 return; 130 131 if (session == null) 132 session = getSessionHandle (); 133 134 return; 135 136 137 } 138 139 140 146 147 public Object createObject (Object class_name, Object id) { 148 String xmlMsg; 149 Object output = null; 150 151 if (class_name == null || id == null) { handleError ("JtEJBAdapter.createObject: invalid parameters"); 153 return (null); 154 } 155 156 handleTrace ("JtEJBAdapter.createObject:" + class_name + "," + id); 157 158 setObjException (null); 160 161 162 177 if (session == null) 178 initializeEJB (); 179 if (session == null) 180 return (null); 181 try { 182 sejb.createObject (session, class_name, id); 184 } catch (Exception ex) { 185 handleException (ex); 186 } 187 188 189 return (output); 190 } 191 192 196 197 private Object locateJtService () { 198 Object jtservice; 199 JtServiceLocator locator; 200 JtObject tmp; 201 202 tmp = new JtObject (); 203 204 206 locator = (JtServiceLocator) tmp.createObject ("Jt.ejb.JtServiceLocator", 207 "locator"); 208 209 210 212 jtservice = (Object ) tmp.sendMessage ("locator", new JtMessage ("JtACTIVATE")); 213 tmp.removeObject ("locator"); 214 return (jtservice); 215 } 216 217 243 244 248 249 public void removeObject (Object id) { 250 String xmlMsg; 251 Object output; 252 253 if (id == null) { handleError ("JtEJBAdapter.removeObject: invalid parameters"); 255 return; 256 } 257 258 handleTrace ("JtEJBAdapter.removeObject: " + id); 259 260 setObjException (null); 262 263 270 271 if (session == null) 272 initializeEJB (); 273 if (session == null) 274 return; 275 276 try { 277 sejb.removeObject (session, id); } catch (Exception ex) { 279 handleException (ex); 280 } 281 } 282 283 293 294 public Object getValue (Object id, Object att) { 295 Object obj; 296 297 298 if (id == null || att == null) { 299 handleError ("JtEJBAdapter.getValue: invalid paramenters"); 300 return (null); 301 } 302 303 handleTrace ("JtEJBAdapter.getValue: " + id + "," + att); 304 305 setObjException (null); 307 308 if (session == null) 309 initializeEJB (); 310 if (session == null) 311 return (null); 312 try { 313 return (sejb.getValue (session, id, att)); 314 } catch (Exception ex) { 315 handleException (ex); 316 } 317 return (null); 318 } 319 320 321 322 328 329 public Object sendMessage (Object id, Object msgid) { 330 331 String xmlMsg; 332 JtMessage msg; 333 Object output = null; 334 335 336 if (id == null || msgid == null) { 337 handleError ("JtEJBAdapter.sendMessage: invalid parameters"); 338 return (null); 339 } 340 341 handleTrace ("JtEJBAdapter.sendMessage:" + id + ", "+ msgid); 342 349 350 setObjException (null); 352 353 if (session == null) 354 initializeEJB (); 355 if (session == null) 356 return (null); 357 try { 358 output = sejb.sendMessage (session, id, msgid); 359 } catch (Exception ex) { 360 handleException (ex); 361 } 362 363 364 365 return (output); 366 367 } 368 369 375 376 public void setValue (Object id, Object att, 377 Object value) { 378 380 381 if (id == null || att == null) { handleError ("JtEJBAdapter.sendMessage: invalid parameters"); 383 return; 384 } 385 386 handleTrace ("JtEJBAdapter.setValue:" + id + "," + att + "," + value); 387 388 setObjException (null); 390 391 398 399 if (session == null) 400 initializeEJB (); 401 if (session == null) 402 return; 403 404 try { 405 sejb.setValue (session, id, att, value); 406 } catch (Exception ex) { 407 handleException (ex); 408 } 409 410 411 } 412 413 414 421 422 public Object processMessage (Object event) { 423 424 String msgid = null; 425 JtMessage e = (JtMessage) event; 426 Object content; 427 428 if (e == null) 429 return null; 430 431 msgid = (String ) e.getMsgId (); 432 433 if (msgid == null) 434 return null; 435 436 content = e.getMsgContent(); 437 438 if (msgid.equals ("JtTEST")) { 439 return (test ()); 440 } 441 442 443 if (msgid.equals ("JtREMOVE")) { 444 445 if (sejb != null && session != null) 447 448 try { 449 sejb.removeSession (session); 450 } catch (Exception ex) { 451 handleException (ex); 452 } 453 return (null); 454 } 455 456 handleError ("processMessage: invalid message ID:" + msgid); 457 return (null); 458 459 } 460 461 462 private Object test () { 463 464 JtEJBAdapter myService; 465 String tmp; 466 String reply = null; 467 Exception ex; 468 469 470 474 createObject ("Jt.examples.HelloWorld", "helloWorld"); 475 476 477 478 480 setValue ("helloWorld", "greetingMessage", "Hello there..."); 481 482 483 484 tmp = (String ) getValue ("helloWorld", "greetingMessage"); 485 486 487 if (tmp == null || !tmp.equals ("Hello there...")) { 488 handleError ("test: getValue failed"); 489 } 490 491 492 494 createObject ("Jt.JtMessage", "message"); 495 setValue ("message", "msgId", "JtHello"); 496 497 498 500 reply = (String ) sendMessage ("helloWorld", "message"); 501 502 503 505 removeObject ("helloWorld"); 506 507 return (reply); 508 509 } 510 511 514 515 public static void main(String [] args) { 516 517 JtObject main = new JtObject (); 518 JtEJBAdapter ejbAdapter; 519 String str; 520 String reply = null; 521 Exception ex; 522 523 526 527 529 ejbAdapter = (JtEJBAdapter) main.createObject ("Jt.ejb.JtEJBAdapter", 530 "ejbAdapter"); 531 532 533 535 536 reply = (String ) main.sendMessage (ejbAdapter, new JtMessage ("JtTEST")); 537 538 539 ex = (Exception ) main.getValue (ejbAdapter, "objException"); 540 541 542 544 if (ex == null) 545 System.out.println (reply); 546 547 549 main.removeObject (ejbAdapter); 550 551 } 552 553 } 554 555 556 | Popular Tags |