1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.naming.java;
20
21 import java.util.Hashtable;
22
23 import javax.naming.Context;
24 import javax.naming.Name;
25 import javax.naming.NamingException;
26 import javax.naming.spi.InitialContextFactory;
27 import javax.naming.spi.ObjectFactory;
28
29 import org.apache.naming.ContextBindings;
30 import org.apache.naming.NamingContext;
31 import org.apache.naming.SelectorContext;
32
33 /**
34  * Context factory for the "java:" namespace.
35  * <p>
36  * <b>Important note</b> : This factory MUST be associated with the "java" URL
37  * prefix, which can be done by either :
38  * <ul>
39  * <li>Adding a
40  * java.naming.factory.url.pkgs=org.apache.naming property
41  * to the JNDI properties file</li>
42  * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
43  * its value including the org.apache.naming package name.
44  * More detail about this can be found in the JNDI documentation :
45  * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
46  * </ul>
47  *
48  * @author Remy Maucherat
49  */

50 public class javaURLContextFactory
51     implements ObjectFactory, InitialContextFactory {
52
53
54     // ----------------------------------------------------------- Constructors
55
56
57     // -------------------------------------------------------------- Constants
58
59
60     public static final String MAIN = "initialContext";
61
62
63     // ----------------------------------------------------- Instance Variables
64
65
66     /**
67      * Initial context.
68      */

69     protected static volatile Context initialContext = null;
70
71
72     // --------------------------------------------------------- Public Methods
73
74
75     // -------------------------------------------------- ObjectFactory Methods
76
77
78     /**
79      * Crete a new Context's instance.
80      */

81     @SuppressWarnings("unchecked")
82     @Override
83     public Object getObjectInstance(Object obj, Name name, Context nameCtx,
84                                     Hashtable<?,?> environment)
85         throws NamingException {
86         if ((ContextBindings.isThreadBound()) ||
87             (ContextBindings.isClassLoaderBound())) {
88             return new SelectorContext((Hashtable<String,Object>)environment);
89         }
90         return null;
91     }
92
93
94     /**
95      * Get a new (writable) initial context.
96      */

97     @SuppressWarnings("unchecked")
98     @Override
99     public Context getInitialContext(Hashtable<?,?> environment)
100         throws NamingException {
101         if (ContextBindings.isThreadBound() ||
102             (ContextBindings.isClassLoaderBound())) {
103             // Redirect the request to the bound initial context
104             return new SelectorContext(
105                     (Hashtable<String,Object>)environment, true);
106         }
107
108         // If the thread is not bound, return a shared writable context
109         if (initialContext == null) {
110             synchronized(javaURLContextFactory.class) {
111                 if (initialContext == null) {
112                     initialContext = new NamingContext(
113                             (Hashtable<String,Object>)environment, MAIN);
114                 }
115             }
116         }
117         return initialContext;
118     }
119
120
121 }
122
123