KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > SessionUtil


1 package org.sapia.regis;
2
3 public class SessionUtil {
4   
5   private static ThreadLocal JavaDoc _session = new ThreadLocal JavaDoc();
6
7   /**
8    * @param obj an <code>Object</code> implementing the <code>RegistryProvider</code>
9    * interface.
10    *
11    * @return the <code>RegisSession</code> that was internally created and associated with
12    * the current thread.
13    */

14   public static RegisSession createSessionFor(Object JavaDoc obj){
15     if(obj instanceof RegistryProvider){
16       RegistryProvider provider = (RegistryProvider)obj;
17       RegisSession session = provider.getRegistry().open();
18       _session.set(session);
19       return session;
20     }
21     else{
22       throw new IllegalArgumentException JavaDoc("Object must be instance of " +
23           RegistryProvider.class.getName());
24     }
25   }
26   
27   /**
28    * @return the <code>RegisSession</code> that is registered witht the current thread.
29    */

30   public static RegisSession current(){
31     RegisSession session = (RegisSession)_session.get();
32     if(session == null){
33       throw new IllegalStateException JavaDoc("Thread not registered with registry session");
34     }
35     return session;
36   }
37   
38   /**
39    * This method "joins" the current thread to the given session .
40    * @param session a <code>RegisSession</code> to associate with the current thread. If
41    * the current thread is already associated session, no exception is thrown and the
42    * given session becomes the associated session.
43    */

44   public static void join(RegisSession session){
45     _session.set(session);
46   }
47   
48   /**
49    * This method "unjoins" the current thread from the given session.
50    */

51   public static void unjoin(){
52     _session.set(null);
53   }
54   
55   /**
56    * This method closes the session with which the current thread is
57    * associated and unjoins the current thread.
58    *
59    * @see #unjoin()
60    */

61   public static void close(){
62     if(isJoined()){
63       current().close();
64       unjoin();
65     }
66   }
67   
68   /**
69    * @return <code>true</code> if the current thread is joined with a session.
70    *
71    * @see #join(RegisSession)
72    */

73   public static boolean isJoined(){
74     return _session.get() != null;
75   }
76
77 }
78
Popular Tags