01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.naming.java;
18:
19: import java.util.Hashtable;
20: import javax.naming.Name;
21: import javax.naming.Context;
22: import javax.naming.NamingException;
23: import javax.naming.spi.ObjectFactory;
24: import javax.naming.spi.InitialContextFactory;
25: import org.apache.naming.SelectorContext;
26: import org.apache.naming.NamingContext;
27: import org.apache.naming.ContextBindings;
28:
29: /**
30: * Context factory for the "java:" namespace.
31: * <p>
32: * <b>Important note</b> : This factory MUST be associated with the "java" URL
33: * prefix, which can be done by either :
34: * <ul>
35: * <li>Adding a
36: * java.naming.factory.url.pkgs=org.apache.catalina.util.naming property
37: * to the JNDI properties file</li>
38: * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
39: * its value including the org.apache.catalina.util.naming package name.
40: * More detail about this can be found in the JNDI documentation :
41: * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
42: * </ul>
43: *
44: * @author Remy Maucherat
45: * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:54 $
46: */
47:
48: public class javaURLContextFactory implements ObjectFactory,
49: InitialContextFactory {
50:
51: // ----------------------------------------------------------- Constructors
52:
53: // -------------------------------------------------------------- Constants
54:
55: public static final String MAIN = "initialContext";
56:
57: // ----------------------------------------------------- Instance Variables
58:
59: /**
60: * Initial context.
61: */
62: protected static Context initialContext = null;
63:
64: // --------------------------------------------------------- Public Methods
65:
66: // -------------------------------------------------- ObjectFactory Methods
67:
68: /**
69: * Crete a new Context's instance.
70: */
71: public Object getObjectInstance(Object obj, Name name,
72: Context nameCtx, Hashtable environment)
73: throws NamingException {
74: if ((ContextBindings.isThreadBound())
75: || (ContextBindings.isClassLoaderBound())) {
76: return new SelectorContext(environment);
77: } else {
78: return null;
79: }
80: }
81:
82: /**
83: * Get a new (writable) initial context.
84: */
85: public Context getInitialContext(Hashtable environment)
86: throws NamingException {
87: if (ContextBindings.isThreadBound()
88: || (ContextBindings.isClassLoaderBound())) {
89: // Redirect the request to the bound initial context
90: return new SelectorContext(environment, true);
91: } else {
92: // If the thread is not bound, return a shared writable context
93: if (initialContext == null)
94: initialContext = new NamingContext(environment, MAIN);
95: return initialContext;
96: }
97: }
98:
99: }
|