1 25 26 package org.objectweb.easybeans.naming; 27 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.transaction.UserTransaction ; 32 33 import org.objectweb.easybeans.log.JLog; 34 import org.objectweb.easybeans.log.JLogFactory; 35 import org.objectweb.easybeans.naming.context.ContextImpl; 36 37 41 public final class NamingManager { 42 43 46 private static JLog logger = JLogFactory.getLog(NamingManager.class); 47 48 51 private static ThreadLocal <Context > threadContext = new ThreadLocal <Context >(); 52 53 56 private InitialContext ictx = null; 57 58 59 62 private static Context clientCtx = null; 63 64 68 private static NamingManager unique = null; 69 70 73 private UserTransaction userTransaction = null; 74 75 79 private NamingManager() throws NamingException { 80 ictx = new InitialContext (); 81 } 82 83 88 public static synchronized NamingManager getInstance() throws NamingException { 89 if (unique == null) { 90 unique = new NamingManager(); 91 } 92 return unique; 93 } 94 95 99 public InitialContext getInitialContext() { 100 return ictx; 101 } 102 103 110 public Context createEnvironmentContext(final String namespace) throws NamingException { 111 112 ContextImpl ctx = new ContextImpl(namespace); 114 115 Context compCtx = ctx.createSubcontext("comp"); 117 118 if (userTransaction == null) { 120 try { 121 userTransaction = (UserTransaction ) ictx.lookup("javax.transaction.UserTransaction"); 122 } catch (NamingException e) { 123 if (logger.isDebugEnabled()) { 124 logger.debug("Cannot lookup UserTransaction.", e); 125 } 126 } 127 } 128 if (userTransaction != null) { 129 compCtx.rebind("UserTransaction", userTransaction); 130 } 131 132 134 return ctx; 135 } 136 137 142 public Context getComponentContext() throws NamingException { 143 144 Context ctx = null; 145 146 ctx = threadContext.get(); 149 if (ctx != null) { 150 return ctx; 151 } 152 153 if (clientCtx != null) { 155 ctx = clientCtx; 156 if (ctx != null) { 157 return ctx; 158 } 159 } 160 161 if (ctx == null) { 164 throw new NamingException ("No java: context for components running outside EasyBeans."); 165 } 166 return ctx; 167 } 168 169 176 public Context setComponentContext(final Context ctx) { 177 Context ret = threadContext.get(); 178 threadContext.set(ctx); 179 return ret; 180 } 181 182 187 public void resetComponentContext(final Context ctx) { 188 threadContext.set(ctx); 189 } 190 191 192 196 public void setClientContainerComponentContext(final Context ctx) { 197 clientCtx = ctx; 198 } 199 200 201 } 202 | Popular Tags |