KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18
19 package org.apache.naming;
20
21 import java.util.Hashtable JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.Context JavaDoc;
24
25 /**
26  * Handles the associations :
27  * <ul>
28  * <li>Catalina context name with the NamingContext</li>
29  * <li>Calling thread with the NamingContext</li>
30  * </ul>
31  *
32  * @author Remy Maucherat
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class ContextBindings {
37
38
39     // -------------------------------------------------------------- Variables
40

41
42     /**
43      * Bindings name - naming context. Keyed by name.
44      */

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

51     private static Hashtable JavaDoc threadBindings = new Hashtable JavaDoc();
52
53
54     /**
55      * Bindings thread - name. Keyed by thread id.
56      */

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

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

69     private static Hashtable JavaDoc clNameBindings = new Hashtable JavaDoc();
70
71
72     /**
73      * The string manager for this package.
74      */

75     protected static StringManager sm =
76         StringManager.getManager(Constants.Package);
77
78
79     // --------------------------------------------------------- Public Methods
80

81
82     /**
83      * Binds a context name.
84      *
85      * @param name Name of the context
86      * @param context Associated naming context instance
87      */

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

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

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

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

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

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

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

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

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

196     public static Context JavaDoc getThread()
197         throws NamingException JavaDoc {
198         Context JavaDoc context =
199             (Context JavaDoc) threadBindings.get(Thread.currentThread());
200         if (context == null)
201             throw new NamingException JavaDoc
202                 (sm.getString("contextBindings.noContextBoundToThread"));
203         return context;
204     }
205
206
207     /**
208      * Retrieves the naming context name bound to a thread.
209      */

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

223     public static boolean isThreadBound() {
224         return (threadBindings.containsKey(Thread.currentThread()));
225     }
226
227
228     /**
229      * Binds a naming context to a class loader.
230      *
231      * @param name Name of the context
232      */

233     public static void bindClassLoader(Object JavaDoc name)
234         throws NamingException JavaDoc {
235         bindClassLoader(name, null);
236     }
237
238
239     /**
240      * Binds a naming context to a thread.
241      *
242      * @param name Name of the context
243      * @param token Security token
244      */

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

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

277     public static void unbindClassLoader(Object JavaDoc name) {
278         unbindClassLoader(name, null);
279     }
280
281
282     /**
283      * Unbinds a naming context to a class loader.
284      *
285      * @param name Name of the context
286      * @param token Security token
287      */

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

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

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

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

352     public static boolean isClassLoaderBound() {
353         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
354         do {
355             if (clBindings.containsKey(cl)) {
356                 return true;
357             }
358         } while ((cl = cl.getParent()) != null);
359         return false;
360     }
361
362
363 }
364
Popular Tags