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