KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > handler > jndi > DirContextURLStreamHandler


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.handler.jndi;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import java.net.URLStreamHandler JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import javax.naming.directory.DirContext JavaDoc;
26
27 /**
28  * Stream handler to a JNDI directory context.
29  *
30  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
31  * @version $Revision: 1.3 $
32  */

33 public class DirContextURLStreamHandler
34     extends URLStreamHandler JavaDoc {
35     
36     
37     // ----------------------------------------------------------- Constructors
38

39     
40     public DirContextURLStreamHandler() {
41     }
42     
43     
44     public DirContextURLStreamHandler(DirContext JavaDoc context) {
45         this.context = context;
46     }
47     
48     
49     // -------------------------------------------------------------- Variables
50

51     
52     /**
53      * Bindings class loader - directory context. Keyed by CL id.
54      */

55     private static Hashtable JavaDoc clBindings = new Hashtable JavaDoc();
56     
57     
58     /**
59      * Bindings thread - directory context. Keyed by thread id.
60      */

61     private static Hashtable JavaDoc threadBindings = new Hashtable JavaDoc();
62     
63     
64     // ----------------------------------------------------- Instance Variables
65

66     
67     /**
68      * Directory context.
69      */

70     protected DirContext JavaDoc context = null;
71     
72     
73     // ------------------------------------------------------------- Properties
74

75     
76     // ----------------------------------------------- URLStreamHandler Methods
77

78     
79     /**
80      * Opens a connection to the object referenced by the <code>URL</code>
81      * argument.
82      */

83     protected URLConnection JavaDoc openConnection(URL JavaDoc u)
84         throws IOException JavaDoc {
85         DirContext JavaDoc currentContext = this.context;
86         if (currentContext == null)
87             currentContext = get();
88         return new DirContextURLConnection(currentContext, u);
89     }
90     
91     
92     // --------------------------------------------------------- Public Methods
93
public static final String JavaDoc PROTOCOL_HANDLER_VARIABLE =
94         "java.protocol.handler.pkgs";
95
96     public static final String JavaDoc PACKAGE = "org.apache.naming.handler.jndi";
97
98     
99     /**
100      * Set the java.protocol.handler.pkgs system property.
101      */

102     public static void setProtocolHandler() {
103         String JavaDoc value = System.getProperty(PROTOCOL_HANDLER_VARIABLE);
104         if (value == null) {
105             value = PACKAGE;
106             System.setProperty(PROTOCOL_HANDLER_VARIABLE, value);
107         } else if (value.indexOf( PACKAGE ) == -1) {
108             value += "|" + PACKAGE;
109             System.setProperty(PROTOCOL_HANDLER_VARIABLE, value);
110         }
111     }
112     
113     
114     /**
115      * Returns true if the thread or the context class loader of the current
116      * thread is bound.
117      */

118     public static boolean isBound() {
119         return (clBindings.containsKey
120                 (Thread.currentThread().getContextClassLoader()))
121             || (threadBindings.containsKey(Thread.currentThread()));
122     }
123     
124     
125     /**
126      * Binds a directory context to a class loader.
127      */

128     public static void bind(DirContext JavaDoc dirContext) {
129         ClassLoader JavaDoc currentCL =
130             Thread.currentThread().getContextClassLoader();
131         if (currentCL != null)
132             clBindings.put(currentCL, dirContext);
133     }
134     
135     
136     /**
137      * Unbinds a directory context to a class loader.
138      */

139     public static void unbind() {
140         ClassLoader JavaDoc currentCL =
141             Thread.currentThread().getContextClassLoader();
142         if (currentCL != null)
143             clBindings.remove(currentCL);
144     }
145     
146     
147     /**
148      * Binds a directory context to a thread.
149      */

150     public static void bindThread(DirContext JavaDoc dirContext) {
151         threadBindings.put(Thread.currentThread(), dirContext);
152     }
153     
154     
155     /**
156      * Unbinds a directory context to a thread.
157      */

158     public static void unbindThread() {
159         threadBindings.remove(Thread.currentThread());
160     }
161     
162     
163     /**
164      * Get the bound context.
165      */

166     public static DirContext JavaDoc get() {
167
168         DirContext JavaDoc result = null;
169
170         Thread JavaDoc currentThread = Thread.currentThread();
171         ClassLoader JavaDoc currentCL = currentThread.getContextClassLoader();
172
173         // Checking CL binding
174
result = (DirContext JavaDoc) clBindings.get(currentCL);
175         if (result != null)
176             return result;
177
178         // Checking thread biding
179
result = (DirContext JavaDoc) threadBindings.get(currentThread);
180
181         // Checking parent CL binding
182
currentCL = currentCL.getParent();
183         while (currentCL != null) {
184             result = (DirContext JavaDoc) clBindings.get(currentCL);
185             if (result != null)
186                 return result;
187             currentCL = currentCL.getParent();
188         }
189
190         if (result == null)
191             throw new IllegalStateException JavaDoc("Illegal class loader binding");
192
193         return result;
194
195     }
196     
197     
198     /**
199      * Binds a directory context to a class loader.
200      */

201     public static void bind(ClassLoader JavaDoc cl, DirContext JavaDoc dirContext) {
202         clBindings.put(cl, dirContext);
203     }
204     
205     
206     /**
207      * Unbinds a directory context to a class loader.
208      */

209     public static void unbind(ClassLoader JavaDoc cl) {
210         clBindings.remove(cl);
211     }
212     
213     
214     /**
215      * Get the bound context.
216      */

217     public static DirContext JavaDoc get(ClassLoader JavaDoc cl) {
218         return (DirContext JavaDoc) clBindings.get(cl);
219     }
220     
221     
222     /**
223      * Get the bound context.
224      */

225     public static DirContext JavaDoc get(Thread JavaDoc thread) {
226         return (DirContext JavaDoc) threadBindings.get(thread);
227     }
228     
229     
230 }
231
Popular Tags