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.modules.java;
18:
19: import java.util.Hashtable;
20:
21: import javax.naming.Context;
22: import javax.naming.Name;
23: import javax.naming.NamingException;
24: import javax.naming.spi.InitialContextFactory;
25: import javax.naming.spi.ObjectFactory;
26:
27: import org.apache.naming.modules.memory.MemoryNamingContext;
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.naming property to the JNDI properties file</li>
37: * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
38: * its value including the org.apache.naming package name.
39: * More detail about this can be found in the JNDI documentation :
40: * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
41: * </ul>
42: *
43: * @author Remy Maucherat
44: */
45:
46: public class javaURLContextFactory implements ObjectFactory,
47: InitialContextFactory {
48:
49: // ----------------------------------------------------------- Constructors
50:
51: // -------------------------------------------------------------- Constants
52:
53: public static final String MAIN = "initialContext";
54:
55: // ----------------------------------------------------- Instance Variables
56:
57: /**
58: * Initial context.
59: */
60: protected static Context initialContext = null;
61:
62: // --------------------------------------------------------- Public Methods
63:
64: // -------------------------------------------------- ObjectFactory Methods
65:
66: /**
67: * Crete a new Context's instance.
68: */
69: public Object getObjectInstance(Object obj, Name name,
70: Context nameCtx, Hashtable environment)
71: throws NamingException {
72: if ((ContextBindings.isThreadBound())
73: || (ContextBindings.isClassLoaderBound())) {
74: return new SelectorContext(environment);
75: } else {
76: return null;
77: }
78: }
79:
80: /**
81: * Get a new (writable) initial context.
82: */
83: public Context getInitialContext(Hashtable environment)
84: throws NamingException {
85: if (ContextBindings.isThreadBound()
86: || (ContextBindings.isClassLoaderBound())) {
87: // Redirect the request to the bound initial context
88: return new SelectorContext(environment, true);
89: } else {
90: // If the thread is not bound, return a shared writable context
91: if (initialContext == null)
92: initialContext = new MemoryNamingContext(environment);
93: return initialContext;
94: }
95: }
96:
97: }
|