KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > modules > java > ContextBindings


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.modules.java;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 import org.apache.naming.core.ContextAccessController;
25 import org.apache.tomcat.util.res.StringManager;
26
27 // this can be a nice generic util that binds per thread or CL any object.
28

29 /**
30  * Handles the associations :
31  * <ul>
32  * <li>Catalina context name with the NamingContext</li>
33  * <li>Calling thread with the NamingContext</li>
34  * </ul>
35  *
36  * @author Remy Maucherat
37  */

38 public class ContextBindings {
39     private static org.apache.commons.logging.Log log=
40         org.apache.commons.logging.LogFactory.getLog( ContextBindings.class );
41
42     // -------------------------------------------------------------- Variables
43

44
45     /**
46      * Bindings name - naming context. Keyed by name.
47      */

48     private static Hashtable JavaDoc contextNameBindings = new Hashtable JavaDoc();
49
50
51     /**
52      * Bindings thread - naming context. Keyed by thread id.
53      */

54     private static Hashtable JavaDoc threadBindings = new Hashtable JavaDoc();
55
56
57     /**
58      * Bindings thread - name. Keyed by thread id.
59      */

60     private static Hashtable JavaDoc threadNameBindings = new Hashtable JavaDoc();
61
62
63     /**
64      * Bindings class loader - naming context. Keyed by CL id.
65      */

66     private static Hashtable JavaDoc clBindings = new Hashtable JavaDoc();
67
68
69     /**
70      * Bindings class loader - name. Keyed by CL id.
71      */

72     private static Hashtable JavaDoc clNameBindings = new Hashtable JavaDoc();
73
74
75     /**
76      * The string manager for this package.
77      */

78     protected static StringManager sm =
79         StringManager.getManager("org.apache.naming");
80
81
82     // --------------------------------------------------------- Public Methods
83

84
85     /**
86      * Binds a context name.
87      *
88      * @param name Name of the context
89      * @param context Associated naming context instance
90      */

91     public static void bindContext(Object JavaDoc name, Context JavaDoc context) {
92         bindContext(name, context, null);
93     }
94
95
96     /**
97      * Binds a context name.
98      *
99      * @param name Name of the context
100      * @param context Associated naming context instance
101      * @param token Security token
102      */

103     public static void bindContext(Object JavaDoc name, Context JavaDoc context,
104                                    Object JavaDoc token) {
105         if (ContextAccessController.checkSecurityToken(name, token))
106             contextNameBindings.put(name, context);
107     }
108
109
110     /**
111      * Unbind context name.
112      *
113      * @param name Name of the context
114      */

115     public static void unbindContext(Object JavaDoc name) {
116         unbindContext(name, null);
117     }
118
119
120     /**
121      * Unbind context name.
122      *
123      * @param name Name of the context
124      * @param token Security token
125      */

126     public static void unbindContext(Object JavaDoc name, Object JavaDoc token) {
127         if (ContextAccessController.checkSecurityToken(name, token))
128             contextNameBindings.remove(name);
129     }
130
131
132     /**
133      * Retrieve a naming context.
134      *
135      * @param name Name of the context
136      */

137     static Context JavaDoc getContext(Object JavaDoc name) {
138         return (Context JavaDoc) contextNameBindings.get(name);
139     }
140
141
142     /**
143      * Binds a naming context to a thread.
144      *
145      * @param name Name of the context
146      */

147     public static void bindThread(Object JavaDoc name)
148         throws NamingException JavaDoc {
149         bindThread(name, null);
150     }
151
152
153     /**
154      * Binds a naming context to a thread.
155      *
156      * @param name Name of the context
157      * @param token Security token
158      */

159     public static void bindThread(Object JavaDoc name, Object JavaDoc token)
160         throws NamingException JavaDoc {
161         // log.info( "BIND: " + name + " " + token );
162
if (ContextAccessController.checkSecurityToken(name, token)) {
163             Context JavaDoc context = (Context JavaDoc) contextNameBindings.get(name);
164             if (context == null)
165                 throw new NamingException JavaDoc
166                     (sm.getString("contextBindings.unknownContext", name));
167             threadBindings.put(Thread.currentThread(), context);
168             threadNameBindings.put(Thread.currentThread(), name);
169         }
170     }
171
172
173     /**
174      * Unbinds a naming context to a thread.
175      *
176      * @param name Name of the context
177      */

178     public static void unbindThread(Object JavaDoc name) {
179         unbindThread(name, null);
180     }
181
182
183     /**
184      * Unbinds a naming context to a thread.
185      *
186      * @param name Name of the context
187      * @param token Security token
188      */

189     public static void unbindThread(Object JavaDoc name, Object JavaDoc token) {
190         if (ContextAccessController.checkSecurityToken(name, token)) {
191             threadBindings.remove(Thread.currentThread());
192             threadNameBindings.remove(Thread.currentThread());
193         }
194     }
195
196
197     /**
198      * Retrieves the naming context bound to a thread.
199      */

200     public static Context JavaDoc getThread()
201         throws NamingException JavaDoc {
202         Context JavaDoc context =
203             (Context JavaDoc) threadBindings.get(Thread.currentThread());
204         log.info( "Context=getThread: " + context );
205         if (context == null)
206             throw new NamingException JavaDoc
207                 (sm.getString("contextBindings.noContextBoundToThread"));
208         return context;
209     }
210
211
212     /**
213      * Retrieves the naming context name bound to a thread.
214      */

215     static Object JavaDoc getThreadName()
216         throws NamingException JavaDoc {
217         Object JavaDoc name = threadNameBindings.get(Thread.currentThread());
218         if (name == null)
219             throw new NamingException JavaDoc
220                 (sm.getString("contextBindings.noContextBoundToThread"));
221         return name;
222     }
223
224
225     /**
226      * Tests if current thread is bound to a context.
227      */

228     public static boolean isThreadBound() {
229         return (threadBindings.containsKey(Thread.currentThread()));
230     }
231
232
233     /**
234      * Binds a naming context to a class loader.
235      *
236      * @param name Name of the context
237      */

238     public static void bindClassLoader(Object JavaDoc name)
239         throws NamingException JavaDoc {
240         bindClassLoader(name, null);
241     }
242
243
244     /**
245      * Binds a naming context to a thread.
246      *
247      * @param name Name of the context
248      * @param token Security token
249      */

250     public static void bindClassLoader(Object JavaDoc name, Object JavaDoc token)
251         throws NamingException JavaDoc {
252         bindClassLoader
253             (name, token, Thread.currentThread().getContextClassLoader());
254     }
255
256
257     /**
258      * Binds a naming context to a thread.
259      *
260      * @param name Name of the context
261      * @param token Security token
262      */

263     public static void bindClassLoader(Object JavaDoc name, Object JavaDoc token,
264                                        ClassLoader JavaDoc classLoader)
265         throws NamingException JavaDoc {
266         if (ContextAccessController.checkSecurityToken(name, token)) {
267             Context JavaDoc context = (Context JavaDoc) contextNameBindings.get(name);
268             if (context == null)
269                 throw new NamingException JavaDoc
270                     (sm.getString("contextBindings.unknownContext", name));
271             clBindings.put(classLoader, context);
272             clNameBindings.put(classLoader, name);
273         }
274     }
275
276
277     /**
278      * Unbinds a naming context to a class loader.
279      *
280      * @param name Name of the context
281      */

282     public static void unbindClassLoader(Object JavaDoc name) {
283         unbindClassLoader(name, null);
284     }
285
286
287     /**
288      * Unbinds a naming context to a class loader.
289      *
290      * @param name Name of the context
291      * @param token Security token
292      */

293     public static void unbindClassLoader(Object JavaDoc name, Object JavaDoc token) {
294         unbindClassLoader(name, token,
295                           Thread.currentThread().getContextClassLoader());
296     }
297
298
299     /**
300      * Unbinds a naming context to a class loader.
301      *
302      * @param name Name of the context
303      * @param token Security token
304      */

305     public static void unbindClassLoader(Object JavaDoc name, Object JavaDoc token,
306                                          ClassLoader JavaDoc classLoader) {
307         if (ContextAccessController.checkSecurityToken(name, token)) {
308             Object JavaDoc n = clNameBindings.get(classLoader);
309             if (!(n.equals(name))) {
310                 return;
311             }
312             clBindings.remove(classLoader);
313             clNameBindings.remove(classLoader);
314         }
315     }
316
317
318     /**
319      * Retrieves the naming context bound to a class loader.
320      */

321     public static Context JavaDoc getClassLoader()
322         throws NamingException JavaDoc {
323         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
324         Context JavaDoc context = null;
325         do {
326             context = (Context JavaDoc) clBindings.get(cl);
327             log.info( "Context=getClassLoader: " + context + " " + cl );
328             if (context != null) {
329                 return context;
330             }
331         } while ((cl = cl.getParent()) != null);
332         throw new NamingException JavaDoc
333             (sm.getString("contextBindings.noContextBoundToCL"));
334     }
335
336
337     /**
338      * Retrieves the naming context name bound to a class loader.
339      */

340     static Object JavaDoc getClassLoaderName()
341         throws NamingException JavaDoc {
342         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
343         Object JavaDoc name = null;
344         do {
345             name = clNameBindings.get(cl);
346             if (name != null) {
347                 return name;
348             }
349         } while ((cl = cl.getParent()) != null);
350         throw new NamingException JavaDoc
351             (sm.getString("contextBindings.noContextBoundToCL"));
352     }
353
354
355     /**
356      * Tests if current class loader is bound to a context.
357      */

358     public static boolean isClassLoaderBound() {
359         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
360         do {
361             if (clBindings.containsKey(cl)) {
362                 return true;
363             }
364         } while ((cl = cl.getParent()) != null);
365         return false;
366     }
367
368
369 }
370
371
Popular Tags