KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > ejb > JtSessionFacadeEJB


1
2
3
4
5 package Jt.ejb;
6
7 import javax.ejb.*;
8 import java.io.Serializable JavaDoc;
9 import java.util.*;
10 import java.rmi.*;
11 import Jt.*;
12
13
14 /**
15  * Jt implementation of the J2EE Session Facade pattern.
16  * Provides an EJB interface to the Jt API. This bean is invoked by
17  * JtEJBAdapter and JtBusinessDelegate. By using JtEJBAdapter, J2EE
18  * clients are able to transparently manipulate remote Jt objects.
19  *
20  * @ejb:bean type="Stateful"
21  * name="JtSessionFacade"
22  * jndi-name="JtSessionFacade"
23  * display-name="Jt Session Facade"
24  */

25
26 public class JtSessionFacadeEJB implements SessionBean {
27
28
29   Hashtable sessionTable = new Hashtable ();
30
31
32   // Implement the methods in the SessionBean interface
33

34   
35   public void ejbActivate() {
36   }
37
38   public void ejbRemove() {
39
40   }
41
42   public void ejbPassivate() {
43
44   }
45
46   /**
47    * Sets the session context.
48    *
49    * @param ctx session context
50    */

51
52   public void setSessionContext(SessionContext ctx)
53         throws javax.ejb.EJBException JavaDoc, java.rmi.RemoteException JavaDoc
54   {
55   }
56
57
58
59   public void ejbCreate () {
60
61   }
62
63
64   private String JavaDoc calculateSessionID (Object JavaDoc shadow) {
65
66     String JavaDoc sessionID;
67     Object JavaDoc oldShadow;
68
69
70     // The following should provide an unique session ID.
71
// It is highly unlikely that two clients will have the same sessionid
72
// If needed, A stronger mechanism may be used
73

74     for (;;) {
75      sessionID = "JtSESSION" + shadow + (new Date()).getTime();
76
77      if (sessionTable.get (sessionID) == null)
78         break;
79
80     }
81      
82         
83     return (sessionID);
84
85   }
86
87     /**
88      * Creates an independent session. Each client session is able to manipulate
89      * Jt objects without interfering with each other. Each session is assigned an
90      * unique session ID.
91      *
92      *
93      * @ejb.interface-method
94      */

95  
96
97   public Object JavaDoc createSession ()
98     throws JtException
99   {
100
101     JtObject shadow;
102     Object JavaDoc session;
103
104
105       shadow = new JtObject ();
106       session = calculateSessionID (shadow);
107       sessionTable.put (session, shadow);
108
109       shadow.handleTrace ("JtSessionFacadeEJB.createSession:session:" + session);
110       return (session);
111   }
112
113
114     /**
115      * Removes a session
116      *
117      * @ejb.interface-method
118      */

119  
120
121   public void removeSession (Object JavaDoc session)
122     throws JtException
123   {
124     JtObject shadow;
125
126     if (session == null)
127       throw new JtException ("JtSessionFacadeEJB.removeSession:invalid session:" + session);
128
129
130     shadow = (JtObject) sessionTable.get (session);
131
132     if (shadow == null) {
133       throw new JtException ("JtSessionFacadeEJB.removeSession:invalid session:" + session);
134     }
135
136     shadow.handleTrace ("JtSessionFacadeEJB.removeSession:session:" + session);
137     sessionTable.remove (session);
138   }
139
140     /**
141      * Creates a Jt object within the specified session
142      *
143      * @ejb.interface-method
144      */

145  
146
147   public Object JavaDoc createObject (Object JavaDoc session, Object JavaDoc class_name, Object JavaDoc obj_id)
148     throws JtException
149   {
150     JtObject shadow;
151     Object JavaDoc output;
152     Exception JavaDoc ex;
153
154
155         if (session == null)
156       throw new JtException ("JtSessionFacadeEJB.createObject:invalid session:" + session);
157
158         shadow = (JtObject) sessionTable.get (session);
159
160         if (shadow == null) {
161
162       throw new JtException ("JtSessionFacadeEJB.createObject:invalid session:" + session);
163
164         }
165
166         shadow.handleTrace ("JtSessionFacadeEJB.createObject:session:" + session);
167
168         shadow.setObjException (null);
169
170         output = shadow.createObject (class_name, obj_id);
171
172         // Check for exceptions
173

174         ex = (Exception JavaDoc) shadow.getObjException ();
175
176         if (ex != null)
177           throw new JtException (ex.getMessage ());
178
179
180         return (output);
181   }
182    
183     /**
184      * Removes an object
185      *
186      * @ejb.interface-method
187      */

188
189   public void removeObject (Object JavaDoc session, Object JavaDoc obj_id)
190     throws JtException
191   {
192
193     JtObject shadow;
194     Exception JavaDoc ex;
195
196         if (session == null)
197       throw new JtException ("JtSessionFacadeEJB.removeObject:invalid session:" + session);
198
199         shadow = (JtObject) sessionTable.get (session);
200
201     if (shadow == null) {
202       throw new JtException ("JtSessionFacadeEJB.removeObject:invalid session:" + session);
203         }
204
205         shadow.handleTrace ("JtSessionFacadeEJB.removeObject:session:" + session);
206
207         shadow.setObjException (null);
208         shadow.removeObject (obj_id);
209
210         // Check for exceptions
211

212         ex = (Exception JavaDoc) shadow.getObjException ();
213
214         if (ex != null)
215           throw new JtException (ex.getMessage ());
216   }
217
218
219     /**
220      * Sends a message to an object
221      *
222      * @ejb.interface-method
223      */

224
225
226   public Object JavaDoc sendMessage (Object JavaDoc session, Object JavaDoc obj_id, Object JavaDoc msg_id)
227     throws JtException
228
229   {
230
231     JtObject shadow;
232     Object JavaDoc output;
233     Exception JavaDoc ex;
234
235
236
237         if (session == null)
238       throw new JtException ("JtSessionFacadeEJB.sendMessage:invalid session:" + session);
239
240         shadow = (JtObject) sessionTable.get (session);
241
242     if (shadow == null)
243       throw new JtException ("JtSessionFacadeEJB.sendMessage:invalid session:" + session);
244 /*
245     if (msg_id == null || obj_id == null)
246        return;
247 */

248         shadow.handleTrace ("JtSessionFacadeEJB.sendMessage:session:" + session);
249
250
251         shadow.setObjException (null);
252         output = shadow.sendMessage (obj_id, msg_id);
253
254         // Check for exceptions
255

256 /*
257         ex = (Exception) shadow.getValue (obj_id, "objException");
258
259         if (ex != null)
260           throw new JtException (ex.getMessage ());
261
262         ex = (Exception) shadow.getObjException ();
263
264         if (ex != null)
265           throw new JtException (ex.getMessage ());
266 */

267
268
269     return (output);
270   }
271
272
273     /**
274      * Gets the value of an attribute
275      *
276      * @ejb.interface-method
277      */

278
279   public Object JavaDoc getValue (Object JavaDoc session, Object JavaDoc obj_id, Object JavaDoc att)
280     throws JtException
281   {
282
283     JtObject shadow;
284     Object JavaDoc output;
285     Exception JavaDoc ex;
286
287
288
289         if (session == null)
290        throw new JtException ("JtSessionFacadeEJB.getValue:invalid session:" + session);
291
292         shadow = (JtObject) sessionTable.get (session);
293
294     if (shadow == null)
295        throw new JtException ("JtSessionFacadeEJB.getValue:invalid session:" + session);
296 /*
297     if (att == null || obj_id == null)
298        return null;
299 */

300         shadow.handleTrace ("JtSessionFacadeEJB.getValue:session:" + session);
301
302         shadow.setObjException (null);
303
304         output = shadow.getValue (obj_id, att);
305
306         ex = (Exception JavaDoc) shadow.getObjException ();
307
308         if (ex != null)
309           throw new JtException (ex.getMessage ());
310
311  
312     return (output);
313   }
314
315
316     /**
317      * Sets the value of an attribute
318      *
319      * @ejb.interface-method
320      */

321  
322
323   public void setValue (Object JavaDoc session, Object JavaDoc obj_id, Object JavaDoc att, Object JavaDoc value)
324     throws JtException
325   {
326
327     JtObject shadow;
328     Exception JavaDoc ex;
329
330         if (session == null)
331        throw new JtException ("JtSessionFacadeEJB.setValue:invalid session:" + session);
332
333         shadow = (JtObject) sessionTable.get (session);
334
335     if (shadow == null)
336        throw new JtException ("JtSessionFacadeEJB.setValue:invalid session:" + session);
337
338 /*
339     if (att == null || obj_id == null)
340        return;
341 */

342         shadow.handleTrace ("JtSessionFacadeEJB.setValue:session:" + session);
343
344         shadow.setObjException (null);
345     shadow.setValue (obj_id, att, value);
346
347         ex = (Exception JavaDoc) shadow.getObjException ();
348
349         if (ex != null)
350           throw new JtException (ex.getMessage ());
351
352   }
353
354     /**
355      * Defines the log file (debugging purposes only)
356      * @ejb.interface-method
357      */

358     
359    public void setLogFile(Object JavaDoc session, String JavaDoc newLogFile)
360     throws JtException
361
362    {
363
364      JtObject shadow;
365
366
367      if (session == null)
368        throw new JtException ("JtSessionFacadeEJB.setLogFile:invalid session:" + session);
369
370      shadow = (JtObject) sessionTable.get (session);
371
372      if (shadow == null) {
373         shadow = new JtObject ();
374
375         sessionTable.put (session, shadow);
376      }
377
378       //shadow.handleTrace ("JtEJB.setLogFile:session:" + session);
379
shadow.setLogFile (newLogFile);
380
381    }
382
383
384 }
385
Popular Tags