001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/ContextBindings.java,v 1.7 2002/05/31 02:55:21 remm Exp $
003: * $Revision: 1.7 $
004: * $Date: 2002/05/31 02:55:21 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.naming;
065:
066: import java.util.Hashtable;
067: import javax.naming.NamingException;
068: import javax.naming.Context;
069:
070: /**
071: * Handles the associations :
072: * <ul>
073: * <li>Catalina context name with the NamingContext</li>
074: * <li>Calling thread with the NamingContext</li>
075: * </ul>
076: *
077: * @author Remy Maucherat
078: * @version $Revision: 1.7 $ $Date: 2002/05/31 02:55:21 $
079: */
080:
081: public class ContextBindings {
082:
083: // -------------------------------------------------------------- Variables
084:
085: /**
086: * Bindings name - naming context. Keyed by name.
087: */
088: private static Hashtable contextNameBindings = new Hashtable();
089:
090: /**
091: * Bindings thread - naming context. Keyed by thread id.
092: */
093: private static Hashtable threadBindings = new Hashtable();
094:
095: /**
096: * Bindings thread - name. Keyed by thread id.
097: */
098: private static Hashtable threadNameBindings = new Hashtable();
099:
100: /**
101: * Bindings class loader - naming context. Keyed by CL id.
102: */
103: private static Hashtable clBindings = new Hashtable();
104:
105: /**
106: * Bindings class loader - name. Keyed by CL id.
107: */
108: private static Hashtable clNameBindings = new Hashtable();
109:
110: /**
111: * The string manager for this package.
112: */
113: protected static StringManager sm = StringManager
114: .getManager(Constants.Package);
115:
116: // --------------------------------------------------------- Public Methods
117:
118: /**
119: * Binds a context name.
120: *
121: * @param name Name of the context
122: * @param context Associated naming context instance
123: */
124: public static void bindContext(Object name, Context context) {
125: bindContext(name, context, null);
126: }
127:
128: /**
129: * Binds a context name.
130: *
131: * @param name Name of the context
132: * @param context Associated naming context instance
133: * @param token Security token
134: */
135: public static void bindContext(Object name, Context context,
136: Object token) {
137: if (ContextAccessController.checkSecurityToken(name, token))
138: contextNameBindings.put(name, context);
139: }
140:
141: /**
142: * Unbind context name.
143: *
144: * @param name Name of the context
145: */
146: public static void unbindContext(Object name) {
147: unbindContext(name, null);
148: }
149:
150: /**
151: * Unbind context name.
152: *
153: * @param name Name of the context
154: * @param token Security token
155: */
156: public static void unbindContext(Object name, Object token) {
157: if (ContextAccessController.checkSecurityToken(name, token))
158: contextNameBindings.remove(name);
159: }
160:
161: /**
162: * Retrieve a naming context.
163: *
164: * @param name Name of the context
165: */
166: static Context getContext(Object name) {
167: return (Context) contextNameBindings.get(name);
168: }
169:
170: /**
171: * Binds a naming context to a thread.
172: *
173: * @param name Name of the context
174: */
175: public static void bindThread(Object name) throws NamingException {
176: bindThread(name, null);
177: }
178:
179: /**
180: * Binds a naming context to a thread.
181: *
182: * @param name Name of the context
183: * @param token Security token
184: */
185: public static void bindThread(Object name, Object token)
186: throws NamingException {
187: if (ContextAccessController.checkSecurityToken(name, token)) {
188: Context context = (Context) contextNameBindings.get(name);
189: if (context == null)
190: throw new NamingException(sm.getString(
191: "contextBindings.unknownContext", name));
192: threadBindings.put(Thread.currentThread(), context);
193: threadNameBindings.put(Thread.currentThread(), name);
194: }
195: }
196:
197: /**
198: * Unbinds a naming context to a thread.
199: *
200: * @param name Name of the context
201: */
202: public static void unbindThread(Object name) {
203: unbindThread(name, null);
204: }
205:
206: /**
207: * Unbinds a naming context to a thread.
208: *
209: * @param name Name of the context
210: * @param token Security token
211: */
212: public static void unbindThread(Object name, Object token) {
213: if (ContextAccessController.checkSecurityToken(name, token)) {
214: threadBindings.remove(Thread.currentThread());
215: threadNameBindings.remove(Thread.currentThread());
216: }
217: }
218:
219: /**
220: * Retrieves the naming context bound to a thread.
221: */
222: public static Context getThread() throws NamingException {
223: Context context = (Context) threadBindings.get(Thread
224: .currentThread());
225: if (context == null)
226: throw new NamingException(
227: sm
228: .getString("contextBindings.noContextBoundToThread"));
229: return context;
230: }
231:
232: /**
233: * Retrieves the naming context name bound to a thread.
234: */
235: static Object getThreadName() throws NamingException {
236: Object name = threadNameBindings.get(Thread.currentThread());
237: if (name == null)
238: throw new NamingException(
239: sm
240: .getString("contextBindings.noContextBoundToThread"));
241: return name;
242: }
243:
244: /**
245: * Tests if current thread is bound to a context.
246: */
247: public static boolean isThreadBound() {
248: return (threadBindings.containsKey(Thread.currentThread()));
249: }
250:
251: /**
252: * Binds a naming context to a class loader.
253: *
254: * @param name Name of the context
255: */
256: public static void bindClassLoader(Object name)
257: throws NamingException {
258: bindClassLoader(name, null);
259: }
260:
261: /**
262: * Binds a naming context to a thread.
263: *
264: * @param name Name of the context
265: * @param token Security token
266: */
267: public static void bindClassLoader(Object name, Object token)
268: throws NamingException {
269: bindClassLoader(name, token, Thread.currentThread()
270: .getContextClassLoader());
271: }
272:
273: /**
274: * Binds a naming context to a thread.
275: *
276: * @param name Name of the context
277: * @param token Security token
278: */
279: public static void bindClassLoader(Object name, Object token,
280: ClassLoader classLoader) throws NamingException {
281: if (ContextAccessController.checkSecurityToken(name, token)) {
282: Context context = (Context) contextNameBindings.get(name);
283: if (context == null)
284: throw new NamingException(sm.getString(
285: "contextBindings.unknownContext", name));
286: clBindings.put(classLoader, context);
287: clNameBindings.put(classLoader, name);
288: }
289: }
290:
291: /**
292: * Unbinds a naming context to a class loader.
293: *
294: * @param name Name of the context
295: */
296: public static void unbindClassLoader(Object name) {
297: unbindClassLoader(name, null);
298: }
299:
300: /**
301: * Unbinds a naming context to a class loader.
302: *
303: * @param name Name of the context
304: * @param token Security token
305: */
306: public static void unbindClassLoader(Object name, Object token) {
307: unbindClassLoader(name, token, Thread.currentThread()
308: .getContextClassLoader());
309: }
310:
311: /**
312: * Unbinds a naming context to a class loader.
313: *
314: * @param name Name of the context
315: * @param token Security token
316: */
317: public static void unbindClassLoader(Object name, Object token,
318: ClassLoader classLoader) {
319: if (ContextAccessController.checkSecurityToken(name, token)) {
320: Object n = clNameBindings.get(classLoader);
321: if (!(n.equals(name))) {
322: return;
323: }
324: clBindings.remove(classLoader);
325: clNameBindings.remove(classLoader);
326: }
327: }
328:
329: /**
330: * Retrieves the naming context bound to a class loader.
331: */
332: public static Context getClassLoader() throws NamingException {
333: ClassLoader cl = Thread.currentThread().getContextClassLoader();
334: Context context = null;
335: do {
336: context = (Context) clBindings.get(cl);
337: if (context != null) {
338: return context;
339: }
340: } while ((cl = cl.getParent()) != null);
341: throw new NamingException(sm
342: .getString("contextBindings.noContextBoundToCL"));
343: }
344:
345: /**
346: * Retrieves the naming context name bound to a class loader.
347: */
348: static Object getClassLoaderName() throws NamingException {
349: ClassLoader cl = Thread.currentThread().getContextClassLoader();
350: Object name = null;
351: do {
352: name = clNameBindings.get(cl);
353: if (name != null) {
354: return name;
355: }
356: } while ((cl = cl.getParent()) != null);
357: throw new NamingException(sm
358: .getString("contextBindings.noContextBoundToCL"));
359: }
360:
361: /**
362: * Tests if current class loader is bound to a context.
363: */
364: public static boolean isClassLoaderBound() {
365: ClassLoader cl = Thread.currentThread().getContextClassLoader();
366: do {
367: if (clBindings.containsKey(cl)) {
368: return true;
369: }
370: } while ((cl = cl.getParent()) != null);
371: return false;
372: }
373:
374: }
|