KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > resources > DirContextURLStreamHandler


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

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

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

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

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

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

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

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

76     
77     // ----------------------------------------------- URLStreamHandler Methods
78

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

84     protected URLConnection JavaDoc openConnection(URL JavaDoc u)
85         throws IOException JavaDoc {
86         DirContext JavaDoc currentContext = this.context;
87         if (currentContext == null)
88             currentContext = get();
89         return new DirContextURLConnection(currentContext, u);
90     }
91     
92     
93     // ------------------------------------------------------------ URL Methods
94

95     
96     /**
97      * Override as part of the fix for 36534, to ensure toString is correct.
98      */

99     protected String JavaDoc toExternalForm(URL JavaDoc u) {
100         // pre-compute length of StringBuffer
101
int len = u.getProtocol().length() + 1;
102         if (u.getPath() != null) {
103             len += u.getPath().length();
104         }
105         if (u.getQuery() != null) {
106             len += 1 + u.getQuery().length();
107         }
108         if (u.getRef() != null)
109             len += 1 + u.getRef().length();
110         StringBuffer JavaDoc result = new StringBuffer JavaDoc(len);
111         result.append(u.getProtocol());
112         result.append(":");
113         if (u.getPath() != null) {
114             result.append(u.getPath());
115         }
116         if (u.getQuery() != null) {
117             result.append('?');
118             result.append(u.getQuery());
119         }
120         if (u.getRef() != null) {
121             result.append("#");
122             result.append(u.getRef());
123         }
124         return result.toString();
125     }
126
127
128     // --------------------------------------------------------- Public Methods
129

130     
131     /**
132      * Set the java.protocol.handler.pkgs system property.
133      */

134     public static void setProtocolHandler() {
135         String JavaDoc value = System.getProperty(Constants.PROTOCOL_HANDLER_VARIABLE);
136         if (value == null) {
137             value = Constants.Package;
138             System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE, value);
139         } else if (value.indexOf(Constants.Package) == -1) {
140             value += "|" + Constants.Package;
141             System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE, value);
142         }
143     }
144     
145     
146     /**
147      * Returns true if the thread or the context class loader of the current
148      * thread is bound.
149      */

150     public static boolean isBound() {
151         return (clBindings.containsKey
152                 (Thread.currentThread().getContextClassLoader()))
153             || (threadBindings.containsKey(Thread.currentThread()));
154     }
155     
156     
157     /**
158      * Binds a directory context to a class loader.
159      */

160     public static void bind(DirContext JavaDoc dirContext) {
161         ClassLoader JavaDoc currentCL =
162             Thread.currentThread().getContextClassLoader();
163         if (currentCL != null)
164             clBindings.put(currentCL, dirContext);
165     }
166     
167     
168     /**
169      * Unbinds a directory context to a class loader.
170      */

171     public static void unbind() {
172         ClassLoader JavaDoc currentCL =
173             Thread.currentThread().getContextClassLoader();
174         if (currentCL != null)
175             clBindings.remove(currentCL);
176     }
177     
178     
179     /**
180      * Binds a directory context to a thread.
181      */

182     public static void bindThread(DirContext JavaDoc dirContext) {
183         threadBindings.put(Thread.currentThread(), dirContext);
184     }
185     
186     
187     /**
188      * Unbinds a directory context to a thread.
189      */

190     public static void unbindThread() {
191         threadBindings.remove(Thread.currentThread());
192     }
193     
194     
195     /**
196      * Get the bound context.
197      */

198     public static DirContext JavaDoc get() {
199
200         DirContext JavaDoc result = null;
201
202         Thread JavaDoc currentThread = Thread.currentThread();
203         ClassLoader JavaDoc currentCL = currentThread.getContextClassLoader();
204
205         // Checking CL binding
206
result = (DirContext JavaDoc) clBindings.get(currentCL);
207         if (result != null)
208             return result;
209
210         // Checking thread biding
211
result = (DirContext JavaDoc) threadBindings.get(currentThread);
212
213         // Checking parent CL binding
214
currentCL = currentCL.getParent();
215         while (currentCL != null) {
216             result = (DirContext JavaDoc) clBindings.get(currentCL);
217             if (result != null)
218                 return result;
219             currentCL = currentCL.getParent();
220         }
221
222         if (result == null)
223             throw new IllegalStateException JavaDoc("Illegal class loader binding");
224
225         return result;
226
227     }
228     
229     
230     /**
231      * Binds a directory context to a class loader.
232      */

233     public static void bind(ClassLoader JavaDoc cl, DirContext JavaDoc dirContext) {
234         clBindings.put(cl, dirContext);
235     }
236     
237     
238     /**
239      * Unbinds a directory context to a class loader.
240      */

241     public static void unbind(ClassLoader JavaDoc cl) {
242         clBindings.remove(cl);
243     }
244     
245     
246     /**
247      * Get the bound context.
248      */

249     public static DirContext JavaDoc get(ClassLoader JavaDoc cl) {
250         return (DirContext JavaDoc) clBindings.get(cl);
251     }
252     
253     
254     /**
255      * Get the bound context.
256      */

257     public static DirContext JavaDoc get(Thread JavaDoc thread) {
258         return (DirContext JavaDoc) threadBindings.get(thread);
259     }
260     
261     
262 }
263
Popular Tags