1 26 27 package org.objectweb.jonas.naming; 28 29 import java.util.Hashtable ; 30 31 import javax.ejb.spi.HandleDelegate ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.omg.CORBA.ORB ; 37 38 import org.objectweb.carol.jndi.ns.JacORBCosNaming; 39 40 import org.objectweb.jonas_ejb.container.JHandleDelegate; 41 42 import org.objectweb.jonas_lib.naming.ContainerNaming; 43 44 import org.objectweb.jonas.common.Log; 45 46 import org.objectweb.util.monolog.api.BasicLevel; 47 import org.objectweb.util.monolog.api.Logger; 48 49 56 public class NamingManager implements ContainerNaming { 57 58 61 private static Logger logger = Log.getLogger(Log.JONAS_NAMING_PREFIX); 62 63 66 private ThreadLocal threadContext = new ThreadLocal (); 67 68 71 private InitialContext ictx = null; 72 73 76 private static Context serverContext = null; 77 78 81 private Hashtable myEnv = null; 82 83 86 private Hashtable clBindings = null; 87 88 91 private static Context clientCtx = null; 92 93 97 private static NamingManager unique = null; 98 99 102 private Object userTransaction = null; 103 104 107 private HandleDelegate handleDelegate = null; 108 109 113 private NamingManager() throws NamingException { 114 try { 115 ictx = new InitialContext (); 116 clBindings = new Hashtable (); 119 120 } catch (NamingException n) { 121 logger.log(BasicLevel.ERROR, "NamingManager: " + n.getExplanation()); 122 Throwable t = n.getRootCause(); 123 if (t != null) { 124 if (t.getMessage().startsWith("Connection refused to host:")) { 125 logger.log(BasicLevel.ERROR, "NamingManager: rmi registry not started ?"); 126 } else if (t.getMessage().startsWith("error during remote invocation")) { 127 logger.log(BasicLevel.ERROR, "NamingManager: jrmi registry not started ?"); 128 } else { 129 logger.log(BasicLevel.ERROR, "NamingManager: " + t.getMessage()); 130 } 131 } 132 throw n; 133 } 134 135 } 136 137 142 public static NamingManager getInstance() throws NamingException { 143 if (unique == null) { 144 unique = new NamingManager(); 145 } 146 return unique; 147 } 148 149 153 157 public InitialContext getInitialContext() { 158 return ictx; 159 } 160 161 164 public HandleDelegate getHandleDelegate() { 165 if (handleDelegate == null) { 166 handleDelegate = new JHandleDelegate(); 167 } 168 return handleDelegate; 169 } 170 171 178 public Context createEnvironmentContext(String namespace) throws NamingException { 179 180 if (Log.isDebugNaming()) { 181 logger.log(BasicLevel.DEBUG, namespace); 182 } 183 184 CompNamingContext ctx = new CompNamingContext(namespace); 186 187 Context compCtx = ctx.createSubcontext("comp"); 189 190 if (userTransaction == null) { 192 try { 193 userTransaction = ictx.lookup("javax.transaction.UserTransaction"); 194 } catch (NamingException ne) { 195 if (Log.isDebugNaming()) { 196 logger.log(BasicLevel.DEBUG, "Cannot lookup UserTransaction."); 197 } 198 } 199 } 200 if (userTransaction != null) { 201 compCtx.rebind("UserTransaction", userTransaction); 202 } 203 204 ORB orb = JacORBCosNaming.getOrb(); 205 if (orb != null) { 206 try { 207 compCtx.rebind("ORB", orb); 209 } catch (NamingException e) { 210 String err = "Cannot create URL for java:comp/ORB object : " + e; 211 logger.log(BasicLevel.ERROR, err); 212 throw new NamingException (err); 213 } 214 } 215 216 compCtx.rebind("HandleDelegate", getHandleDelegate()); 218 219 return ctx; 220 } 221 222 227 public Context getComponentContext() throws NamingException { 228 229 Context ctx = null; 230 231 ctx = (Context ) threadContext.get(); 234 if (ctx != null) { 235 if (Log.isDebugNaming()) { 236 logger.log(BasicLevel.DEBUG, "return Context for ejb"); 237 } 238 return ctx; 239 } 240 241 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 245 if ((cl != null) && (cl.getParent() != null)) { 246 ctx = (Context ) clBindings.get(cl.getParent()); 247 if (ctx != null) { 248 if (Log.isDebugNaming()) { 249 logger.log(BasicLevel.DEBUG, "return Context for webapp"); 250 } 251 return ctx; 252 } 253 } 254 255 if (clientCtx != null) { 257 ctx = clientCtx; 258 if (ctx != null) { 259 if (Log.isDebugNaming()) { 260 logger.log(BasicLevel.DEBUG, "return Context for client"); 261 } 262 return ctx; 263 } 264 } 265 266 if (ctx == null) { 269 ctx = getServerContext(); 270 if (Log.isDebugNaming()) { 271 logger.log(BasicLevel.DEBUG, "return default server Context"); 272 } 273 } 274 return ctx; 275 } 276 277 284 public Context setComponentContext(Context ctx) { 285 Context ret = (Context ) threadContext.get(); 286 threadContext.set(ctx); 287 return ret; 288 } 289 290 295 public void resetComponentContext(Context ctx) { 296 threadContext.set(ctx); 297 } 298 299 300 305 public void setComponentContext(Context ctx, ClassLoader cl) { 306 if (Log.isDebugNaming()) { 307 logger.log(BasicLevel.DEBUG, "class loader = " + cl); 308 } 309 clBindings.put(cl, ctx); 310 } 311 312 316 public void setClientContainerComponentContext(Context ctx) { 317 if (Log.isDebugNaming()) { 318 logger.log(BasicLevel.DEBUG, ""); 319 } 320 clientCtx = ctx; 321 } 322 323 328 public Context getComponentContext(ClassLoader cl) { 329 if (Log.isDebugNaming()) { 330 logger.log(BasicLevel.DEBUG, "class loader = " + cl); 331 } 332 return (Context ) clBindings.get(cl); 333 } 334 335 339 public void unSetComponentContext(ClassLoader cl) { 340 if (Log.isDebugNaming()) { 341 logger.log(BasicLevel.DEBUG, "class loader = " + cl); 342 } 343 clBindings.remove(cl); 344 } 345 346 350 public Hashtable getEnv() { 351 return myEnv; 352 } 353 354 360 public Context createImmutableEnvironmentContext(String namespace) throws NamingException { 361 logger.log(BasicLevel.ERROR, "Not Implemented"); 362 return null; 363 } 364 365 369 374 public Context getServerContext() { 375 if (serverContext == null) { 376 try { 377 serverContext = createEnvironmentContext("server"); 378 } catch (NamingException e) { 379 logger.log(BasicLevel.ERROR, "cannot create serverContext:" + e); 380 } 381 } 382 return serverContext; 383 } 384 385 } | Popular Tags |