NamingManager.java

00001 
00027 package org.objectweb.jonas.naming;
00028 
00029 import java.util.Hashtable;
00030 
00031 import javax.ejb.spi.HandleDelegate;
00032 import javax.naming.Context;
00033 import javax.naming.InitialContext;
00034 import javax.naming.NamingException;
00035 
00036 import org.omg.CORBA.ORB;
00037 
00038 import org.objectweb.carol.jndi.spi.JacORBIIOPContext;
00039 
00040 import org.objectweb.jonas_ejb.container.JHandleDelegate;
00041 
00042 import org.objectweb.jonas_lib.naming.ContainerNaming;
00043 
00044 import org.objectweb.jonas.common.Log;
00045 
00046 import org.objectweb.util.monolog.api.BasicLevel;
00047 import org.objectweb.util.monolog.api.Logger;
00048 
00056 public class NamingManager implements ContainerNaming {
00057 
00061     private static Logger logger = null;
00062 
00066     private ThreadLocal threadContext = new ThreadLocal();
00067 
00071     private InitialContext ictx = null;
00072 
00076     private static Context serverContext = null;
00077 
00081     private Hashtable myEnv = null;
00082 
00086     private Hashtable clBindings = null;
00087 
00091     private static Context clientCtx = null;
00092 
00097     private static NamingManager unique = null;
00098 
00102     private Object userTransaction = null;
00103 
00107     private HandleDelegate handleDelegate = null;
00108 
00113     private NamingManager() throws NamingException {
00114         logger = Log.getLogger(Log.JONAS_NAMING_PREFIX);
00115         try {
00116             ictx = new InitialContext();
00117             // there is a bug in carol that makes this not work.
00118             //myEnv = ictx.getEnvironment();
00119             clBindings = new Hashtable();
00120 
00121         } catch (NamingException n) {
00122             logger.log(BasicLevel.ERROR, "NamingManager: " + n.getExplanation());
00123             Throwable t = n.getRootCause();
00124             if (t != null) {
00125                 if (t.getMessage().startsWith("Connection refused to host:")) {
00126                     logger.log(BasicLevel.ERROR, "NamingManager: rmi registry not started ?");
00127                 } else if (t.getMessage().startsWith("error during remote invocation")) {
00128                     logger.log(BasicLevel.ERROR, "NamingManager: jrmi registry not started ?");
00129                 } else {
00130                     logger.log(BasicLevel.ERROR, "NamingManager: " + t.getMessage());
00131                 }
00132             }
00133             throw n;
00134         }
00135 
00136     }
00137 
00143     public static NamingManager getInstance() throws NamingException {
00144         if (unique == null) {
00145             unique = new NamingManager();
00146         }
00147         return unique;
00148     }
00149 
00150     // ------------------------------------------------------------------
00151     // ContainerNaming implementation
00152     // ------------------------------------------------------------------
00153 
00158     public InitialContext getInitialContext() {
00159         return ictx;
00160     }
00161 
00165     public HandleDelegate getHandleDelegate() {
00166         if (handleDelegate == null) {
00167             handleDelegate = new JHandleDelegate();
00168         }
00169         return handleDelegate;
00170     }
00171 
00179     public Context createEnvironmentContext(String namespace) throws NamingException {
00180 
00181         logger.log(BasicLevel.DEBUG, namespace);
00182         
00183         // Create a new environment
00184         CompNamingContext ctx = new CompNamingContext(namespace);
00185 
00186         // Create subContext
00187         Context compCtx = ctx.createSubcontext("comp");
00188 
00189         // Bind java:comp/UserTransaction
00190         if (userTransaction == null) {
00191             try {
00192                 userTransaction = ictx.lookup("javax.transaction.UserTransaction");
00193             } catch (NamingException ne) {
00194                 if (logger.isLoggable(BasicLevel.DEBUG)) {
00195                     logger.log(BasicLevel.DEBUG, "Cannot lookup UserTransaction.");
00196                 }
00197             }
00198         }
00199         if (userTransaction != null) {
00200             compCtx.rebind("UserTransaction", userTransaction);
00201         }
00202 
00203         ORB orb = JacORBIIOPContext.getOrb();
00204         if (orb != null) {
00205             try {
00206                 // Bind the java:comp/ORB object instance
00207                 compCtx.rebind("ORB", orb);
00208             } catch (NamingException e) {
00209                 String err = "Cannot create URL for java:comp/ORB object : " + e;
00210                 logger.log(BasicLevel.ERROR, err);
00211                 throw new NamingException(err);
00212             }
00213         }
00214 
00215         // Bind java:comp/HandleDelegate
00216         compCtx.rebind("HandleDelegate", getHandleDelegate());
00217 
00218         return ctx;
00219     }
00220 
00226     public Context getComponentContext() throws NamingException {
00227 
00228         Context ctx = null;
00229 
00230         // Check if there is a context to the local thread
00231         // For ejbs
00232         ctx = (Context) threadContext.get();
00233         if (ctx != null) {
00234             logger.log(BasicLevel.DEBUG, "return Context for ejb");
00235             return ctx;
00236         }
00237 
00238         // Check if there is a context which match the currentThread
00239         // classLoader
00240         // For webapps
00241         ClassLoader cl = Thread.currentThread().getContextClassLoader();
00242         if ((cl != null) && (cl.getParent() != null)) {
00243             ctx = (Context) clBindings.get(cl.getParent());
00244             if (ctx != null) {
00245                 logger.log(BasicLevel.DEBUG, "return Context for webapp");
00246                 return ctx;
00247             }
00248         }
00249 
00250         // Check static context. use in client. One context per JVM.
00251         if (clientCtx != null) {
00252             ctx = clientCtx;
00253             if (ctx != null) {
00254                 logger.log(BasicLevel.DEBUG, "return Context for client");
00255                 return ctx;
00256             }
00257         }
00258 
00259         // No context found. This is outside of a j2ee component or server
00260         // component.
00261         if (ctx == null) {
00262             ctx = getServerContext();
00263             logger.log(BasicLevel.DEBUG, "return default server Context");
00264         }
00265         return ctx;
00266     }
00267 
00275     public Context setComponentContext(Context ctx) {
00276         if (logger.isLoggable(BasicLevel.DEBUG)) {
00277             if (ctx == null) {
00278                 logger.log(BasicLevel.DEBUG, "reset to null");
00279             } else {
00280                 try {
00281                     logger.log(BasicLevel.DEBUG, "name = " + ctx.getNameInNamespace());
00282                 } catch (NamingException e) {
00283                     logger.log(BasicLevel.ERROR, "bad name");
00284                 }
00285             }
00286         }
00287         Context ret = (Context) threadContext.get();
00288         threadContext.set(ctx);
00289         return ret;
00290     }
00291 
00297     public void setComponentContext(Context ctx, ClassLoader cl) {
00298         if (logger.isLoggable(BasicLevel.DEBUG)) {
00299             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
00300         }
00301         clBindings.put(cl, ctx);
00302     }
00303 
00308     public void setClientContainerComponentContext(Context ctx) {
00309         logger.log(BasicLevel.DEBUG, "");
00310         clientCtx = ctx;
00311     }
00312 
00318     public Context getComponentContext(ClassLoader cl) {
00319         if (logger.isLoggable(BasicLevel.DEBUG)) {
00320             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
00321         }
00322         return (Context) clBindings.get(cl);
00323     }
00324 
00329     public void unSetComponentContext(ClassLoader cl) {
00330         if (logger.isLoggable(BasicLevel.DEBUG)) {
00331             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
00332         }
00333         clBindings.remove(cl);
00334     }
00335 
00340     public Hashtable getEnv() {
00341         return myEnv;
00342     }
00343 
00350     public Context createImmutableEnvironmentContext(String namespace) throws NamingException {
00351         logger.log(BasicLevel.ERROR, "Not Implemented");
00352         return null;
00353     }
00354 
00355     // ------------------------------------------------------------------
00356     // other proprietary methods
00357     // ------------------------------------------------------------------
00358 
00364     public Context getServerContext() {
00365         if (serverContext == null) {
00366             try {
00367                 serverContext = createEnvironmentContext("server");
00368             } catch (NamingException e) {
00369                 logger.log(BasicLevel.ERROR, "cannot create serverContext:" + e);
00370              }
00371         }
00372         return serverContext;
00373     }
00374 
00375 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1