001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming;
018:
019: import java.util.Hashtable;
020: import javax.naming.NamingException;
021: import javax.naming.Context;
022:
023: /**
024: * Handles the associations :
025: * <ul>
026: * <li>Catalina context name with the NamingContext</li>
027: * <li>Calling thread with the NamingContext</li>
028: * </ul>
029: *
030: * @author Remy Maucherat
031: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:53 $
032: */
033:
034: public class ContextBindings {
035:
036: // -------------------------------------------------------------- Variables
037:
038: /**
039: * Bindings name - naming context. Keyed by name.
040: */
041: private static Hashtable contextNameBindings = new Hashtable();
042:
043: /**
044: * Bindings thread - naming context. Keyed by thread id.
045: */
046: private static Hashtable threadBindings = new Hashtable();
047:
048: /**
049: * Bindings thread - name. Keyed by thread id.
050: */
051: private static Hashtable threadNameBindings = new Hashtable();
052:
053: /**
054: * Bindings class loader - naming context. Keyed by CL id.
055: */
056: private static Hashtable clBindings = new Hashtable();
057:
058: /**
059: * Bindings class loader - name. Keyed by CL id.
060: */
061: private static Hashtable clNameBindings = new Hashtable();
062:
063: /**
064: * The string manager for this package.
065: */
066: protected static StringManager sm = StringManager
067: .getManager(Constants.Package);
068:
069: // --------------------------------------------------------- Public Methods
070:
071: /**
072: * Binds a context name.
073: *
074: * @param name Name of the context
075: * @param context Associated naming context instance
076: */
077: public static void bindContext(Object name, Context context) {
078: bindContext(name, context, null);
079: }
080:
081: /**
082: * Binds a context name.
083: *
084: * @param name Name of the context
085: * @param context Associated naming context instance
086: * @param token Security token
087: */
088: public static void bindContext(Object name, Context context,
089: Object token) {
090: if (ContextAccessController.checkSecurityToken(name, token))
091: contextNameBindings.put(name, context);
092: }
093:
094: /**
095: * Unbind context name.
096: *
097: * @param name Name of the context
098: */
099: public static void unbindContext(Object name) {
100: unbindContext(name, null);
101: }
102:
103: /**
104: * Unbind context name.
105: *
106: * @param name Name of the context
107: * @param token Security token
108: */
109: public static void unbindContext(Object name, Object token) {
110: if (ContextAccessController.checkSecurityToken(name, token))
111: contextNameBindings.remove(name);
112: }
113:
114: /**
115: * Retrieve a naming context.
116: *
117: * @param name Name of the context
118: */
119: static Context getContext(Object name) {
120: return (Context) contextNameBindings.get(name);
121: }
122:
123: /**
124: * Binds a naming context to a thread.
125: *
126: * @param name Name of the context
127: */
128: public static void bindThread(Object name) throws NamingException {
129: bindThread(name, null);
130: }
131:
132: /**
133: * Binds a naming context to a thread.
134: *
135: * @param name Name of the context
136: * @param token Security token
137: */
138: public static void bindThread(Object name, Object token)
139: throws NamingException {
140: if (ContextAccessController.checkSecurityToken(name, token)) {
141: Context context = (Context) contextNameBindings.get(name);
142: if (context == null)
143: throw new NamingException(sm.getString(
144: "contextBindings.unknownContext", name));
145: threadBindings.put(Thread.currentThread(), context);
146: threadNameBindings.put(Thread.currentThread(), name);
147: }
148: }
149:
150: /**
151: * Unbinds a naming context to a thread.
152: *
153: * @param name Name of the context
154: */
155: public static void unbindThread(Object name) {
156: unbindThread(name, null);
157: }
158:
159: /**
160: * Unbinds a naming context to a thread.
161: *
162: * @param name Name of the context
163: * @param token Security token
164: */
165: public static void unbindThread(Object name, Object token) {
166: if (ContextAccessController.checkSecurityToken(name, token)) {
167: threadBindings.remove(Thread.currentThread());
168: threadNameBindings.remove(Thread.currentThread());
169: }
170: }
171:
172: /**
173: * Retrieves the naming context bound to a thread.
174: */
175: public static Context getThread() throws NamingException {
176: Context context = (Context) threadBindings.get(Thread
177: .currentThread());
178: if (context == null)
179: throw new NamingException(
180: sm
181: .getString("contextBindings.noContextBoundToThread"));
182: return context;
183: }
184:
185: /**
186: * Retrieves the naming context name bound to a thread.
187: */
188: static Object getThreadName() throws NamingException {
189: Object name = threadNameBindings.get(Thread.currentThread());
190: if (name == null)
191: throw new NamingException(
192: sm
193: .getString("contextBindings.noContextBoundToThread"));
194: return name;
195: }
196:
197: /**
198: * Tests if current thread is bound to a context.
199: */
200: public static boolean isThreadBound() {
201: return (threadBindings.containsKey(Thread.currentThread()));
202: }
203:
204: /**
205: * Binds a naming context to a class loader.
206: *
207: * @param name Name of the context
208: */
209: public static void bindClassLoader(Object name)
210: throws NamingException {
211: bindClassLoader(name, null);
212: }
213:
214: /**
215: * Binds a naming context to a thread.
216: *
217: * @param name Name of the context
218: * @param token Security token
219: */
220: public static void bindClassLoader(Object name, Object token)
221: throws NamingException {
222: bindClassLoader(name, token, Thread.currentThread()
223: .getContextClassLoader());
224: }
225:
226: /**
227: * Binds a naming context to a thread.
228: *
229: * @param name Name of the context
230: * @param token Security token
231: */
232: public static void bindClassLoader(Object name, Object token,
233: ClassLoader classLoader) throws NamingException {
234: if (ContextAccessController.checkSecurityToken(name, token)) {
235: Context context = (Context) contextNameBindings.get(name);
236: if (context == null)
237: throw new NamingException(sm.getString(
238: "contextBindings.unknownContext", name));
239: clBindings.put(classLoader, context);
240: clNameBindings.put(classLoader, name);
241: }
242: }
243:
244: /**
245: * Unbinds a naming context to a class loader.
246: *
247: * @param name Name of the context
248: */
249: public static void unbindClassLoader(Object name) {
250: unbindClassLoader(name, null);
251: }
252:
253: /**
254: * Unbinds a naming context to a class loader.
255: *
256: * @param name Name of the context
257: * @param token Security token
258: */
259: public static void unbindClassLoader(Object name, Object token) {
260: unbindClassLoader(name, token, Thread.currentThread()
261: .getContextClassLoader());
262: }
263:
264: /**
265: * Unbinds a naming context to a class loader.
266: *
267: * @param name Name of the context
268: * @param token Security token
269: */
270: public static void unbindClassLoader(Object name, Object token,
271: ClassLoader classLoader) {
272: if (ContextAccessController.checkSecurityToken(name, token)) {
273: Object n = clNameBindings.get(classLoader);
274: if ((n == null) || !(n.equals(name))) {
275: return;
276: }
277: clBindings.remove(classLoader);
278: clNameBindings.remove(classLoader);
279: }
280: }
281:
282: /**
283: * Retrieves the naming context bound to a class loader.
284: */
285: public static Context getClassLoader() throws NamingException {
286: ClassLoader cl = Thread.currentThread().getContextClassLoader();
287: Context context = null;
288: do {
289: context = (Context) clBindings.get(cl);
290: if (context != null) {
291: return context;
292: }
293: } while ((cl = cl.getParent()) != null);
294: throw new NamingException(sm
295: .getString("contextBindings.noContextBoundToCL"));
296: }
297:
298: /**
299: * Retrieves the naming context name bound to a class loader.
300: */
301: static Object getClassLoaderName() throws NamingException {
302: ClassLoader cl = Thread.currentThread().getContextClassLoader();
303: Object name = null;
304: do {
305: name = clNameBindings.get(cl);
306: if (name != null) {
307: return name;
308: }
309: } while ((cl = cl.getParent()) != null);
310: throw new NamingException(sm
311: .getString("contextBindings.noContextBoundToCL"));
312: }
313:
314: /**
315: * Tests if current class loader is bound to a context.
316: */
317: public static boolean isClassLoaderBound() {
318: ClassLoader cl = Thread.currentThread().getContextClassLoader();
319: do {
320: if (clBindings.containsKey(cl)) {
321: return true;
322: }
323: } while ((cl = cl.getParent()) != null);
324: return false;
325: }
326:
327: }
|