KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > naming > NamingManager


1 /**
2  *
3  * JOnAS: Java(TM) Open Application Server
4  * Copyright (C) 1999-2005 Bull S.A.
5  * Contact: jonas-team@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  *
22  * --------------------------------------------------------------------------
23  * $Id: NamingManager.java,v 1.38 2005/06/06 11:20:35 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.naming;
28
29 import java.util.Hashtable JavaDoc;
30
31 import javax.ejb.spi.HandleDelegate JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.omg.CORBA.ORB JavaDoc;
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 /**
50  * Naming Manager for an EJB Server. this singleton class must exist in each
51  * jonas server.
52  * @author Philippe Durieux
53  * @author Philippe Coq : Monolog
54  * @author Florent Benoit & Ludovic Bert : Context for web container
55  */

56 public class NamingManager implements ContainerNaming {
57
58     /**
59      * Logger used for traces
60      */

61     private static Logger logger = Log.getLogger(Log.JONAS_NAMING_PREFIX);
62
63     /**
64      * Naming Context associated with the thread
65      */

66     private ThreadLocal JavaDoc threadContext = new ThreadLocal JavaDoc();
67
68     /**
69      * Initial Context
70      */

71     private InitialContext JavaDoc ictx = null;
72
73     /**
74      * Context of the server
75      */

76     private static Context JavaDoc serverContext = null;
77
78     /**
79      * Environment
80      */

81     private Hashtable JavaDoc myEnv = null;
82
83     /**
84      * Associate a context to a class loader
85      */

86     private Hashtable JavaDoc clBindings = null;
87
88     /**
89      * Static context used by client container. One context for all the JVM.
90      */

91     private static Context JavaDoc clientCtx = null;
92
93     /**
94      * Singleton management: - the constructor is private. - use static method
95      * getInstance to retrieve/create the instance.
96      */

97     private static NamingManager unique = null;
98
99     /**
100      * UserTransaction object, to be shared by all components.
101      */

102     private Object JavaDoc userTransaction = null;
103
104     /**
105      * HandleDelegate implementation is shared by all component contexts
106      */

107     private HandleDelegate JavaDoc handleDelegate = null;
108
109     /**
110      * Create the naming manager.
111      * @throws NamingException if no initial context is built
112      */

113     private NamingManager() throws NamingException JavaDoc {
114         try {
115             ictx = new InitialContext JavaDoc();
116             // there is a bug in carol that makes this not work.
117
//myEnv = ictx.getEnvironment();
118
clBindings = new Hashtable JavaDoc();
119
120         } catch (NamingException JavaDoc n) {
121             logger.log(BasicLevel.ERROR, "NamingManager: " + n.getExplanation());
122             Throwable JavaDoc 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     /**
138      * Return the unique instance of a NamingManager.
139      * @return NamingManager the unique instance.
140      * @throws NamingException if it failed.
141      */

142     public static NamingManager getInstance() throws NamingException JavaDoc {
143         if (unique == null) {
144             unique = new NamingManager();
145         }
146         return unique;
147     }
148
149     // ------------------------------------------------------------------
150
// ContainerNaming implementation
151
// ------------------------------------------------------------------
152

153     /**
154      * Get the initialContext used in this jonas server.
155      * @return InitialContext the initial context.
156      */

157     public InitialContext JavaDoc getInitialContext() {
158         return ictx;
159     }
160
161     /**
162      * @return The HandleDelegate implementation for this server
163      */

164     public HandleDelegate JavaDoc getHandleDelegate() {
165         if (handleDelegate == null) {
166             handleDelegate = new JHandleDelegate();
167         }
168         return handleDelegate;
169     }
170
171     /**
172      * Create Context for application and component environments. (formally
173      * known as createComponentContext)
174      * @param namespace namespace to used for the Context
175      * @return a java: context with comp/ subcontext
176      * @throws NamingException if the creation of the java: context failed.
177      */

178     public Context JavaDoc createEnvironmentContext(String JavaDoc namespace) throws NamingException JavaDoc {
179
180         if (Log.isDebugNaming()) {
181             logger.log(BasicLevel.DEBUG, namespace);
182         }
183
184         // Create a new environment
185
CompNamingContext ctx = new CompNamingContext(namespace);
186
187         // Create subContext
188
Context JavaDoc compCtx = ctx.createSubcontext("comp");
189
190         // Bind java:comp/UserTransaction
191
if (userTransaction == null) {
192             try {
193                 userTransaction = ictx.lookup("javax.transaction.UserTransaction");
194             } catch (NamingException JavaDoc 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 JavaDoc orb = JacORBCosNaming.getOrb();
205         if (orb != null) {
206             try {
207                 // Bind the java:comp/ORB object instance
208
compCtx.rebind("ORB", orb);
209             } catch (NamingException JavaDoc e) {
210                 String JavaDoc err = "Cannot create URL for java:comp/ORB object : " + e;
211                 logger.log(BasicLevel.ERROR, err);
212                 throw new NamingException JavaDoc(err);
213             }
214         }
215
216         // Bind java:comp/HandleDelegate
217
compCtx.rebind("HandleDelegate", getHandleDelegate());
218
219         return ctx;
220     }
221
222     /**
223      * Get the Context associated with the current thread or to a class loader
224      * @return Context the component context.
225      * @throws NamingException When operation is not allowed
226      */

227     public Context JavaDoc getComponentContext() throws NamingException JavaDoc {
228
229         Context JavaDoc ctx = null;
230
231         // Check if there is a context to the local thread
232
// For ejbs
233
ctx = (Context JavaDoc) 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         // Check if there is a context which match the currentThread
242
// classLoader
243
// For webapps
244
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
245         if ((cl != null) && (cl.getParent() != null)) {
246             ctx = (Context JavaDoc) 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         // Check static context. use in client. One context per JVM.
256
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         // No context found. This is outside of a j2ee component or server
267
// component.
268
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     /**
278      * Associate this CompNamingContext with the current thread. This method
279      * should be called in preinvoke/postinvoke and when we build the bean
280      * environment or web environment.
281      * @param ctx the context to associate to the current thread.
282      * @return Context the context of the thread
283      */

284     public Context JavaDoc setComponentContext(Context JavaDoc ctx) {
285         Context JavaDoc ret = (Context JavaDoc) threadContext.get();
286         threadContext.set(ctx);
287         return ret;
288     }
289
290     /**
291      * Set back the context with the given value.
292      * Don't return the previous context, use setComponentContext() method for this.
293      * @param ctx the context to associate to the current thread.
294      */

295     public void resetComponentContext(Context JavaDoc ctx) {
296         threadContext.set(ctx);
297     }
298
299
300     /**
301      * Associate the specified CompNamingContext with the given classloader.
302      * @param ctx the context to associate to the classloader.
303      * @param cl the classloader which is bind to the context.
304      */

305     public void setComponentContext(Context JavaDoc ctx, ClassLoader JavaDoc cl) {
306         if (Log.isDebugNaming()) {
307             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
308         }
309         clBindings.put(cl, ctx);
310     }
311
312     /**
313      * Set the context used by client container (per JVM instead of per thread)
314      * @param ctx the context to set
315      */

316     public void setClientContainerComponentContext(Context JavaDoc ctx) {
317         if (Log.isDebugNaming()) {
318             logger.log(BasicLevel.DEBUG, "");
319         }
320         clientCtx = ctx;
321     }
322
323     /**
324      * Return the CompNamingContext associated with the given classloader.
325      * @param cl the classloader which is bind to the context.
326      * @return the CompNamingContext associated with the given classloader.
327      */

328     public Context JavaDoc getComponentContext(ClassLoader JavaDoc cl) {
329         if (Log.isDebugNaming()) {
330             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
331         }
332         return (Context JavaDoc) clBindings.get(cl);
333     }
334
335     /**
336      * Remove the CompNamingContext associated with the given classloader.
337      * @param cl the classloader which is bind to the context.
338      */

339     public void unSetComponentContext(ClassLoader JavaDoc cl) {
340         if (Log.isDebugNaming()) {
341             logger.log(BasicLevel.DEBUG, "class loader = " + cl);
342         }
343         clBindings.remove(cl);
344     }
345
346     /**
347      * Return the environment for JNDI This is used only for handles today.
348      * @return Hashtable the environment.
349      */

350     public Hashtable JavaDoc getEnv() {
351         return myEnv;
352     }
353
354     /**
355      * Create Immutable Context. Not implemented !
356      * @param namespace namespace to used for the Context
357      * @return Context an immutable context
358      * @throws NamingException if the creation of the java: context failed.
359      */

360     public Context JavaDoc createImmutableEnvironmentContext(String JavaDoc namespace) throws NamingException JavaDoc {
361         logger.log(BasicLevel.ERROR, "Not Implemented");
362         return null;
363     }
364
365     // ------------------------------------------------------------------
366
// other proprietary methods
367
// ------------------------------------------------------------------
368

369     /**
370      * Get the server component context. This is used only internally in the
371      * jonas NamingManager.
372      * @return Context the server component context.
373      */

374     public Context JavaDoc getServerContext() {
375         if (serverContext == null) {
376             try {
377                 serverContext = createEnvironmentContext("server");
378             } catch (NamingException JavaDoc e) {
379                 logger.log(BasicLevel.ERROR, "cannot create serverContext:" + e);
380              }
381         }
382         return serverContext;
383     }
384
385 }
Popular Tags