KickJava   Java API By Example, From Geeks To Geeks.

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


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

25
26 package org.objectweb.easybeans.naming;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
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 /**
38  * Manages the java: context used by components.
39  * @author Florent Benoit
40  */

41 public final class NamingManager {
42
43     /**
44      * Logger.
45      */

46     private static JLog logger = JLogFactory.getLog(NamingManager.class);
47
48     /**
49      * Associate a context to a thread.
50      */

51     private static ThreadLocal JavaDoc<Context JavaDoc> threadContext = new ThreadLocal JavaDoc<Context JavaDoc>();
52
53     /**
54      * Initial Context.
55      */

56     private InitialContext JavaDoc ictx = null;
57
58
59     /**
60      * Static context used by client container. One context for all the JVM.
61      */

62     private static Context JavaDoc clientCtx = null;
63
64     /**
65      * Singleton management: - the constructor is private. - use static method
66      * getInstance to retrieve/create the instance.
67      */

68     private static NamingManager unique = null;
69
70     /**
71      * UserTransaction object, to be shared by all components.
72      */

73     private UserTransaction JavaDoc userTransaction = null;
74
75     /**
76      * Create the naming manager.
77      * @throws NamingException if no initial context is built
78      */

79     private NamingManager() throws NamingException JavaDoc {
80             ictx = new InitialContext JavaDoc();
81     }
82
83     /**
84      * Return the unique instance of a NamingManager.
85      * @return NamingManager the unique instance.
86      * @throws NamingException if it failed.
87      */

88     public static synchronized NamingManager getInstance() throws NamingException JavaDoc {
89         if (unique == null) {
90             unique = new NamingManager();
91         }
92         return unique;
93     }
94
95     /**
96      * Get the initialContext used in this jonas server.
97      * @return InitialContext the initial context.
98      */

99     public InitialContext JavaDoc getInitialContext() {
100         return ictx;
101     }
102
103     /**
104      * Create Context for application and component environments. (formally
105      * known as createComponentContext)
106      * @param namespace namespace to used for the Context
107      * @return a java: context with comp/ subcontext
108      * @throws NamingException if the creation of the java: context failed.
109      */

110     public Context JavaDoc createEnvironmentContext(final String JavaDoc namespace) throws NamingException JavaDoc {
111
112         // Create a new environment
113
ContextImpl ctx = new ContextImpl(namespace);
114
115         // Create subContext
116
Context JavaDoc compCtx = ctx.createSubcontext("comp");
117
118         // Bind java:comp/UserTransaction
119
if (userTransaction == null) {
120             try {
121                 userTransaction = (UserTransaction JavaDoc) ictx.lookup("javax.transaction.UserTransaction");
122             } catch (NamingException JavaDoc 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         //TODO : ORB
133

134         return ctx;
135     }
136
137     /**
138      * Get the Context associated with the current thread or to a class loader.
139      * @return Context the component context.
140      * @throws NamingException When operation is not allowed
141      */

142     public Context JavaDoc getComponentContext() throws NamingException JavaDoc {
143
144         Context JavaDoc ctx = null;
145
146         // Check if there is a context to the local thread
147
// For ejbs
148
ctx = threadContext.get();
149         if (ctx != null) {
150             return ctx;
151         }
152
153         // Check static context. use in client. One context per JVM.
154
if (clientCtx != null) {
155             ctx = clientCtx;
156             if (ctx != null) {
157                 return ctx;
158             }
159         }
160
161         // No context found. This is outside of a j2ee component or server
162
// component.
163
if (ctx == null) {
164             throw new NamingException JavaDoc("No java: context for components running outside EasyBeans.");
165         }
166         return ctx;
167     }
168
169     /**
170      * Associate this CompNamingContext with the current thread.
171      * This method should be called before the call to the business method.
172      * After, resetComponentContext should be called to reset the context.
173      * @param ctx the context to associate to the current thread.
174      * @return Context the context of the thread
175      */

176     public Context JavaDoc setComponentContext(final Context JavaDoc ctx) {
177         Context JavaDoc ret = threadContext.get();
178         threadContext.set(ctx);
179         return ret;
180     }
181
182     /**
183      * Set back the context with the given value.
184      * Don't return the previous context, use setComponentContext() method for this.
185      * @param ctx the context to associate to the current thread.
186      */

187     public void resetComponentContext(final Context JavaDoc ctx) {
188         threadContext.set(ctx);
189     }
190
191
192     /**
193      * Set the context used by client container (per JVM instead of per thread).
194      * @param ctx the context to set
195      */

196     public void setClientContainerComponentContext(final Context JavaDoc ctx) {
197         clientCtx = ctx;
198     }
199
200
201 }
202
Popular Tags