KickJava   Java API By Example, From Geeks To Geeks.

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


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

49
50 public class javaURLContextFactory
51     implements ObjectFactory JavaDoc, InitialContextFactory JavaDoc {
52
53
54     // ----------------------------------------------------------- Constructors
55

56
57     // -------------------------------------------------------------- Constants
58

59
60     public static final String JavaDoc MAIN = "initialContext";
61
62
63     // ----------------------------------------------------- Instance Variables
64

65
66     /**
67      * Initial context.
68      */

69     protected static Context JavaDoc initialContext = null;
70
71
72     // --------------------------------------------------------- Public Methods
73

74
75     // -------------------------------------------------- ObjectFactory Methods
76

77
78     /**
79      * Crete a new Context's instance.
80      */

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

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