1 package org.sapia.regis; 2 3 public class SessionUtil { 4 5 private static ThreadLocal _session = new ThreadLocal (); 6 7 14 public static RegisSession createSessionFor(Object 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 ("Object must be instance of " + 23 RegistryProvider.class.getName()); 24 } 25 } 26 27 30 public static RegisSession current(){ 31 RegisSession session = (RegisSession)_session.get(); 32 if(session == null){ 33 throw new IllegalStateException ("Thread not registered with registry session"); 34 } 35 return session; 36 } 37 38 44 public static void join(RegisSession session){ 45 _session.set(session); 46 } 47 48 51 public static void unjoin(){ 52 _session.set(null); 53 } 54 55 61 public static void close(){ 62 if(isJoined()){ 63 current().close(); 64 unjoin(); 65 } 66 } 67 68 73 public static boolean isJoined(){ 74 return _session.get() != null; 75 } 76 77 } 78 | Popular Tags |