KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > modules > java > javaURLContextFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.Name JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.spi.InitialContextFactory JavaDoc;
25 import javax.naming.spi.ObjectFactory JavaDoc;
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 JavaDoc, InitialContextFactory JavaDoc
47 {
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     // -------------------------------------------------------------- Constants
54

55
56     public static final String JavaDoc MAIN = "initialContext";
57
58
59     // ----------------------------------------------------- Instance Variables
60

61
62     /**
63      * Initial context.
64      */

65     protected static Context JavaDoc initialContext = null;
66
67
68     // --------------------------------------------------------- Public Methods
69

70
71     // -------------------------------------------------- ObjectFactory Methods
72

73
74     /**
75      * Crete a new Context's instance.
76      */

77     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
78                                     Hashtable JavaDoc environment)
79         throws NamingException JavaDoc {
80         if ((ContextBindings.isThreadBound()) ||
81             (ContextBindings.isClassLoaderBound())) {
82             return new SelectorContext(environment);
83         } else {
84             return null;
85         }
86     }
87
88
89     /**
90      * Get a new (writable) initial context.
91      */

92     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment)
93         throws NamingException JavaDoc {
94         if (ContextBindings.isThreadBound() ||
95             (ContextBindings.isClassLoaderBound())) {
96             // Redirect the request to the bound initial context
97
return new SelectorContext(environment, true);
98         } else {
99             // If the thread is not bound, return a shared writable context
100
if (initialContext == null)
101                 initialContext = new MemoryNamingContext(environment);
102             return initialContext;
103         }
104     }
105
106
107 }
108
109
Popular Tags