KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > ejb > JtEJBAdapter


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 /**
17   * Jt EJB Adapter.
18   */

19
20 public class JtEJBAdapter extends JtAdapter {
21
22   JtXMLConverter xmlConverter = null;
23   private String JavaDoc url = null;
24   private String JavaDoc remoteLogFile;
25   JtSessionFacadeHome dhome = null;
26   JtSessionFacade sejb = null;
27   Object JavaDoc session = null; //check
28

29
30   public JtEJBAdapter () {
31   }
32
33
34
35
36  /**
37    * Returns the name of the remote log file
38    */

39
40   public String JavaDoc getRemoteLogFile () {
41     return (remoteLogFile);
42   }
43
44
45  /**
46   * Specifies the name of the remote log file
47   * @param remoteLogFile remote log file
48   */

49
50   public void setRemoteLogFile (String JavaDoc 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 JavaDoc ex) {
60       handleException (ex);
61     }
62
63
64   }
65
66
67
68
69
70   // Propagate Exceptions
71

72 /*
73   private Exception propagateException (Object obj)
74   {
75     Exception ex;
76
77     if (obj == null)
78       return null;
79
80     ex = (Exception)
81      super.getValue (obj, "objException");
82
83     if (ex != null)
84       super.setValue (this, "objException", ex);
85
86     return (ex);
87   }
88 */

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 JavaDoc ex) {
101         handleException (ex);
102       }
103     }
104   }
105
106
107   private Object JavaDoc getSessionHandle () {
108
109     if (sejb == null)
110       return (null);
111     try {
112       return (sejb.createSession ());
113     } catch (Exception JavaDoc ex) {
114       handleException (ex);
115     }
116     return (null);
117
118   }
119
120   // Intialialize the Jt EJB
121

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    /**
141     * Creates a remote instance of the specified class.
142     * @param class_name class name
143     * @param id object name
144     * @return object created or null
145     */

146
147   public Object JavaDoc createObject (Object JavaDoc class_name, Object JavaDoc id) {
148     String JavaDoc xmlMsg;
149     Object JavaDoc output = null;
150
151     if (class_name == null || id == null) { // check
152
handleError ("JtEJBAdapter.createObject: invalid parameters");
153       return (null);
154     }
155
156     handleTrace ("JtEJBAdapter.createObject:" + class_name + "," + id);
157
158     // Reset the object exception
159
setObjException (null);
160
161
162 /*
163     if (sejb == null)
164       getEJBHandle ();
165
166     if (sejb == null)
167       return (null);
168
169     if (session == null)
170       session = getSessionHandle ();
171
172     if (session == null) {
173       handleError ("createObject: Invalid session: null");
174       return (null);
175     }
176 */

177     if (session == null)
178       initializeEJB ();
179     if (session == null)
180       return (null);
181     try {
182       //sejb.setLogFile(session, "C:\\log1.txt");
183
sejb.createObject (session, class_name, id);
184     } catch (Exception JavaDoc ex) {
185       handleException (ex);
186     }
187
188
189     return (output);
190   }
191
192  /**
193   * Invokes the service locator
194   *
195   */

196
197   private Object JavaDoc locateJtService () {
198     Object JavaDoc jtservice;
199     JtServiceLocator locator;
200     JtObject tmp;
201
202     tmp = new JtObject ();
203
204    // Create ServiceLocator
205

206     locator = (JtServiceLocator) tmp.createObject ("Jt.ejb.JtServiceLocator",
207       "locator");
208
209
210     // Activate Service Locator
211

212     jtservice = (Object JavaDoc) tmp.sendMessage ("locator", new JtMessage ("JtACTIVATE"));
213     tmp.removeObject ("locator");
214     return (jtservice);
215   }
216
217 /*
218   private void updateRemoteLogFile () {
219
220     String xmlMsg;
221     JtMessage msg;
222     JtXMLConverter xmlconverter;
223
224
225     if (axisAdapter == null)
226       return;
227     
228     xmlConverter = new JtXMLConverter ();
229
230     xmlConverter.setValue ("this", "logFile", remoteLogFile);
231
232     xmlMsg = (String) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
233
234
235     msg = new JtMessage ("JtSCRIPT");
236     msg.setMsgContent (xmlMsg);
237
238     super.sendMessage (axisAdapter, msg);
239
240
241   }
242 */

243
244    /**
245     * Removes an object.
246     * @param id object id
247     */

248
249   public void removeObject (Object JavaDoc id) {
250     String JavaDoc xmlMsg;
251     Object JavaDoc output;
252
253     if (id == null) { // check
254
handleError ("JtEJBAdapter.removeObject: invalid parameters");
255       return;
256     }
257
258     handleTrace ("JtEJBAdapter.removeObject: " + id);
259
260     // Reset the object exception
261
setObjException (null);
262
263 /*
264     if (sejb == null)
265       getEJBHandle ();
266
267     if (sejb == null)
268       return;
269 */

270
271     if (session == null)
272       initializeEJB ();
273     if (session == null)
274       return;
275
276     try {
277       sejb.removeObject (session, id); // check
278
} catch (Exception JavaDoc ex) {
279       handleException (ex);
280     }
281   }
282
283    /**
284      * Gets the value of an attribute. An object is always returned.
285      * For primitive types, the corresponding object type is returned.
286      * For instance, Integer is returned if
287      * the attribute is of type int.
288      *
289      * @param id object id
290      * @param att attribute name
291      * @return attribute value
292      */

293
294   public Object JavaDoc getValue (Object JavaDoc id, Object JavaDoc att) {
295    Object JavaDoc 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     // Reset the object exception
306
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 JavaDoc ex) {
315       handleException (ex);
316     }
317     return (null);
318   }
319
320
321
322    /**
323      * Sends a Jt Message to another object.
324      *
325      * @param id object ID
326      * @param msgid message ID
327      */

328
329   public Object JavaDoc sendMessage (Object JavaDoc id, Object JavaDoc msgid) {
330
331     String JavaDoc xmlMsg;
332     JtMessage msg;
333     Object JavaDoc 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 /*
343     if (sejb == null)
344       getEJBHandle ();
345
346     if (sejb == null)
347       return (null);
348 */

349
350     // Reset the object exception
351
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 JavaDoc ex) {
360       handleException (ex);
361     }
362
363
364
365     return (output);
366
367   }
368
369    /**
370      * Sets the value of an attribute.
371      * @param id object id
372      * @param att attribute name
373      * @param value attribute value
374      */

375  
376   public void setValue (Object JavaDoc id, Object JavaDoc att,
377     Object JavaDoc value) {
378     //String xmlMsg;
379

380
381     if (id == null || att == null) { // check null value
382
handleError ("JtEJBAdapter.sendMessage: invalid parameters");
383       return;
384     }
385
386     handleTrace ("JtEJBAdapter.setValue:" + id + "," + att + "," + value);
387
388     // Reset the object exception
389
setObjException (null);
390
391 /*
392     if (sejb == null)
393       getEJBHandle ();
394
395     if (sejb == null)
396       return;
397 */

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 JavaDoc ex) {
407       handleException (ex);
408     }
409
410
411   }
412
413
414   /**
415     * Process object messages.
416     * <ul>
417     * <li> JtTEST - Tests the messages processes by JtEJBAdapter
418     * </ul>
419     * @param event message
420     */

421
422   public Object JavaDoc processMessage (Object JavaDoc event) {
423
424    String JavaDoc msgid = null;
425    JtMessage e = (JtMessage) event;
426    Object JavaDoc content;
427
428      if (e == null)
429     return null;
430
431      msgid = (String JavaDoc) 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        // Removes the session
446
if (sejb != null && session != null)
447
448          try {
449            sejb.removeSession (session);
450          } catch (Exception JavaDoc 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 JavaDoc test () {
463
464     JtEJBAdapter myService;
465     String JavaDoc tmp;
466     String JavaDoc reply = null;
467     Exception JavaDoc ex;
468
469
470     // Create a remote instance of the HelloWorld class. The Jt EJB adapter
471
// can be used to create remote instances of any Jt Framework class.
472
// HelloWorld is used as an example.
473

474     createObject ("Jt.examples.HelloWorld", "helloWorld");
475
476
477
478     // Set the greetingMessage attribute
479

480     setValue ("helloWorld", "greetingMessage", "Hello there...");
481
482
483
484     tmp = (String JavaDoc) getValue ("helloWorld", "greetingMessage");
485
486
487     if (tmp == null || !tmp.equals ("Hello there...")) {
488       handleError ("test: getValue failed");
489     }
490
491
492     // Create a remote message (JtHello)
493

494     createObject ("Jt.JtMessage", "message");
495     setValue ("message", "msgId", "JtHello");
496
497
498     // Send JtHello to the remote helloWorld object
499

500     reply = (String JavaDoc) sendMessage ("helloWorld", "message");
501
502
503     // Remove the remote instance of HelloWorld
504

505     removeObject ("helloWorld");
506
507     return (reply);
508
509   }
510
511  /**
512    * Unit tests all the messages processed by JtEJBAdapter.
513    */

514
515   public static void main(String JavaDoc[] args) {
516
517     JtObject main = new JtObject ();
518     JtEJBAdapter ejbAdapter;
519     String JavaDoc str;
520     String JavaDoc reply = null;
521     Exception JavaDoc ex;
522
523     //main.setObjTrace (1);
524
//main.setLogFile ("log.txt");
525

526
527     // Create the Jt EJB Adapter
528

529     ejbAdapter = (JtEJBAdapter) main.createObject ("Jt.ejb.JtEJBAdapter",
530      "ejbAdapter");
531
532
533     // send a test message (JtTEST) to the adapter
534

535
536     reply = (String JavaDoc) main.sendMessage (ejbAdapter, new JtMessage ("JtTEST"));
537     
538
539     ex = (Exception JavaDoc) main.getValue (ejbAdapter, "objException");
540     
541
542     // Display the reply unless an exception is detected
543

544     if (ex == null)
545       System.out.println (reply);
546
547     // Remove the EJB adapter
548

549     main.removeObject (ejbAdapter);
550
551   }
552
553 }
554
555
556
Popular Tags