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.modules.java;
018:
019: import java.util.Hashtable;
020:
021: import javax.naming.Context;
022: import javax.naming.NamingException;
023:
024: import org.apache.naming.core.ContextAccessController;
025: import org.apache.tomcat.util.res.StringManager;
026:
027: // this can be a nice generic util that binds per thread or CL any object.
028:
029: /**
030: * Handles the associations :
031: * <ul>
032: * <li>Catalina context name with the NamingContext</li>
033: * <li>Calling thread with the NamingContext</li>
034: * </ul>
035: *
036: * @author Remy Maucherat
037: */
038: public class ContextBindings {
039: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
040: .getLog(ContextBindings.class);
041:
042: // -------------------------------------------------------------- Variables
043:
044: /**
045: * Bindings name - naming context. Keyed by name.
046: */
047: private static Hashtable contextNameBindings = new Hashtable();
048:
049: /**
050: * Bindings thread - naming context. Keyed by thread id.
051: */
052: private static Hashtable threadBindings = new Hashtable();
053:
054: /**
055: * Bindings thread - name. Keyed by thread id.
056: */
057: private static Hashtable threadNameBindings = new Hashtable();
058:
059: /**
060: * Bindings class loader - naming context. Keyed by CL id.
061: */
062: private static Hashtable clBindings = new Hashtable();
063:
064: /**
065: * Bindings class loader - name. Keyed by CL id.
066: */
067: private static Hashtable clNameBindings = new Hashtable();
068:
069: /**
070: * The string manager for this package.
071: */
072: protected static StringManager sm = StringManager
073: .getManager("org.apache.naming");
074:
075: // --------------------------------------------------------- Public Methods
076:
077: /**
078: * Binds a context name.
079: *
080: * @param name Name of the context
081: * @param context Associated naming context instance
082: */
083: public static void bindContext(Object name, Context context) {
084: bindContext(name, context, null);
085: }
086:
087: /**
088: * Binds a context name.
089: *
090: * @param name Name of the context
091: * @param context Associated naming context instance
092: * @param token Security token
093: */
094: public static void bindContext(Object name, Context context,
095: Object token) {
096: if (ContextAccessController.checkSecurityToken(name, token))
097: contextNameBindings.put(name, context);
098: }
099:
100: /**
101: * Unbind context name.
102: *
103: * @param name Name of the context
104: */
105: public static void unbindContext(Object name) {
106: unbindContext(name, null);
107: }
108:
109: /**
110: * Unbind context name.
111: *
112: * @param name Name of the context
113: * @param token Security token
114: */
115: public static void unbindContext(Object name, Object token) {
116: if (ContextAccessController.checkSecurityToken(name, token))
117: contextNameBindings.remove(name);
118: }
119:
120: /**
121: * Retrieve a naming context.
122: *
123: * @param name Name of the context
124: */
125: static Context getContext(Object name) {
126: return (Context) contextNameBindings.get(name);
127: }
128:
129: /**
130: * Binds a naming context to a thread.
131: *
132: * @param name Name of the context
133: */
134: public static void bindThread(Object name) throws NamingException {
135: bindThread(name, null);
136: }
137:
138: /**
139: * Binds a naming context to a thread.
140: *
141: * @param name Name of the context
142: * @param token Security token
143: */
144: public static void bindThread(Object name, Object token)
145: throws NamingException {
146: // log.info( "BIND: " + name + " " + token );
147: if (ContextAccessController.checkSecurityToken(name, token)) {
148: Context context = (Context) contextNameBindings.get(name);
149: if (context == null)
150: throw new NamingException(sm.getString(
151: "contextBindings.unknownContext", name));
152: threadBindings.put(Thread.currentThread(), context);
153: threadNameBindings.put(Thread.currentThread(), name);
154: }
155: }
156:
157: /**
158: * Unbinds a naming context to a thread.
159: *
160: * @param name Name of the context
161: */
162: public static void unbindThread(Object name) {
163: unbindThread(name, null);
164: }
165:
166: /**
167: * Unbinds a naming context to a thread.
168: *
169: * @param name Name of the context
170: * @param token Security token
171: */
172: public static void unbindThread(Object name, Object token) {
173: if (ContextAccessController.checkSecurityToken(name, token)) {
174: threadBindings.remove(Thread.currentThread());
175: threadNameBindings.remove(Thread.currentThread());
176: }
177: }
178:
179: /**
180: * Retrieves the naming context bound to a thread.
181: */
182: public static Context getThread() throws NamingException {
183: Context context = (Context) threadBindings.get(Thread
184: .currentThread());
185: log.info("Context=getThread: " + context);
186: if (context == null)
187: throw new NamingException(
188: sm
189: .getString("contextBindings.noContextBoundToThread"));
190: return context;
191: }
192:
193: /**
194: * Retrieves the naming context name bound to a thread.
195: */
196: static Object getThreadName() throws NamingException {
197: Object name = threadNameBindings.get(Thread.currentThread());
198: if (name == null)
199: throw new NamingException(
200: sm
201: .getString("contextBindings.noContextBoundToThread"));
202: return name;
203: }
204:
205: /**
206: * Tests if current thread is bound to a context.
207: */
208: public static boolean isThreadBound() {
209: return (threadBindings.containsKey(Thread.currentThread()));
210: }
211:
212: /**
213: * Binds a naming context to a class loader.
214: *
215: * @param name Name of the context
216: */
217: public static void bindClassLoader(Object name)
218: throws NamingException {
219: bindClassLoader(name, null);
220: }
221:
222: /**
223: * Binds a naming context to a thread.
224: *
225: * @param name Name of the context
226: * @param token Security token
227: */
228: public static void bindClassLoader(Object name, Object token)
229: throws NamingException {
230: bindClassLoader(name, token, Thread.currentThread()
231: .getContextClassLoader());
232: }
233:
234: /**
235: * Binds a naming context to a thread.
236: *
237: * @param name Name of the context
238: * @param token Security token
239: */
240: public static void bindClassLoader(Object name, Object token,
241: ClassLoader classLoader) throws NamingException {
242: if (ContextAccessController.checkSecurityToken(name, token)) {
243: Context context = (Context) contextNameBindings.get(name);
244: if (context == null)
245: throw new NamingException(sm.getString(
246: "contextBindings.unknownContext", name));
247: clBindings.put(classLoader, context);
248: clNameBindings.put(classLoader, name);
249: }
250: }
251:
252: /**
253: * Unbinds a naming context to a class loader.
254: *
255: * @param name Name of the context
256: */
257: public static void unbindClassLoader(Object name) {
258: unbindClassLoader(name, null);
259: }
260:
261: /**
262: * Unbinds a naming context to a class loader.
263: *
264: * @param name Name of the context
265: * @param token Security token
266: */
267: public static void unbindClassLoader(Object name, Object token) {
268: unbindClassLoader(name, token, Thread.currentThread()
269: .getContextClassLoader());
270: }
271:
272: /**
273: * Unbinds a naming context to a class loader.
274: *
275: * @param name Name of the context
276: * @param token Security token
277: */
278: public static void unbindClassLoader(Object name, Object token,
279: ClassLoader classLoader) {
280: if (ContextAccessController.checkSecurityToken(name, token)) {
281: Object n = clNameBindings.get(classLoader);
282: if (!(n.equals(name))) {
283: return;
284: }
285: clBindings.remove(classLoader);
286: clNameBindings.remove(classLoader);
287: }
288: }
289:
290: /**
291: * Retrieves the naming context bound to a class loader.
292: */
293: public static Context getClassLoader() throws NamingException {
294: ClassLoader cl = Thread.currentThread().getContextClassLoader();
295: Context context = null;
296: do {
297: context = (Context) clBindings.get(cl);
298: log.info("Context=getClassLoader: " + context + " " + cl);
299: if (context != null) {
300: return context;
301: }
302: } while ((cl = cl.getParent()) != null);
303: throw new NamingException(sm
304: .getString("contextBindings.noContextBoundToCL"));
305: }
306:
307: /**
308: * Retrieves the naming context name bound to a class loader.
309: */
310: static Object getClassLoaderName() throws NamingException {
311: ClassLoader cl = Thread.currentThread().getContextClassLoader();
312: Object name = null;
313: do {
314: name = clNameBindings.get(cl);
315: if (name != null) {
316: return name;
317: }
318: } while ((cl = cl.getParent()) != null);
319: throw new NamingException(sm
320: .getString("contextBindings.noContextBoundToCL"));
321: }
322:
323: /**
324: * Tests if current class loader is bound to a context.
325: */
326: public static boolean isClassLoaderBound() {
327: ClassLoader cl = Thread.currentThread().getContextClassLoader();
328: do {
329: if (clBindings.containsKey(cl)) {
330: return true;
331: }
332: } while ((cl = cl.getParent()) != null);
333: return false;
334: }
335:
336: }
|