001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.resources;
018:
019: import java.io.IOException;
020: import java.net.URL;
021: import java.net.URLConnection;
022: import java.net.URLStreamHandler;
023: import java.util.Hashtable;
024:
025: import javax.naming.directory.DirContext;
026:
027: /**
028: * Stream handler to a JNDI directory context.
029: *
030: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
031: * @version $Revision: 1.3 $
032: */
033: public class DirContextURLStreamHandler extends URLStreamHandler {
034:
035: // ----------------------------------------------------------- Constructors
036:
037: public DirContextURLStreamHandler() {
038: }
039:
040: public DirContextURLStreamHandler(DirContext context) {
041: this .context = context;
042: }
043:
044: // -------------------------------------------------------------- Variables
045:
046: /**
047: * Bindings class loader - directory context. Keyed by CL id.
048: */
049: private static Hashtable clBindings = new Hashtable();
050:
051: /**
052: * Bindings thread - directory context. Keyed by thread id.
053: */
054: private static Hashtable threadBindings = new Hashtable();
055:
056: // ----------------------------------------------------- Instance Variables
057:
058: /**
059: * Directory context.
060: */
061: protected DirContext context = null;
062:
063: // ------------------------------------------------------------- Properties
064:
065: // ----------------------------------------------- URLStreamHandler Methods
066:
067: /**
068: * Opens a connection to the object referenced by the <code>URL</code>
069: * argument.
070: */
071: protected URLConnection openConnection(URL u) throws IOException {
072: DirContext currentContext = this .context;
073: if (currentContext == null)
074: currentContext = get();
075: return new DirContextURLConnection(currentContext, u);
076: }
077:
078: // --------------------------------------------------------- Public Methods
079:
080: /**
081: * Set the java.protocol.handler.pkgs system property.
082: */
083: public static void setProtocolHandler() {
084: String value = System
085: .getProperty(Constants.PROTOCOL_HANDLER_VARIABLE);
086: if (value == null) {
087: value = Constants.Package;
088: System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE,
089: value);
090: } else if (value.indexOf(Constants.Package) == -1) {
091: value += "|" + Constants.Package;
092: System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE,
093: value);
094: }
095: }
096:
097: /**
098: * Returns true if the thread or the context class loader of the current
099: * thread is bound.
100: */
101: public static boolean isBound() {
102: return (clBindings.containsKey(Thread.currentThread()
103: .getContextClassLoader()))
104: || (threadBindings.containsKey(Thread.currentThread()));
105: }
106:
107: /**
108: * Binds a directory context to a class loader.
109: */
110: public static void bind(DirContext dirContext) {
111: ClassLoader currentCL = Thread.currentThread()
112: .getContextClassLoader();
113: if (currentCL != null)
114: clBindings.put(currentCL, dirContext);
115: }
116:
117: /**
118: * Unbinds a directory context to a class loader.
119: */
120: public static void unbind() {
121: ClassLoader currentCL = Thread.currentThread()
122: .getContextClassLoader();
123: if (currentCL != null)
124: clBindings.remove(currentCL);
125: }
126:
127: /**
128: * Binds a directory context to a thread.
129: */
130: public static void bindThread(DirContext dirContext) {
131: threadBindings.put(Thread.currentThread(), dirContext);
132: }
133:
134: /**
135: * Unbinds a directory context to a thread.
136: */
137: public static void unbindThread() {
138: threadBindings.remove(Thread.currentThread());
139: }
140:
141: /**
142: * Get the bound context.
143: */
144: public static DirContext get() {
145:
146: DirContext result = null;
147:
148: Thread currentThread = Thread.currentThread();
149: ClassLoader currentCL = currentThread.getContextClassLoader();
150:
151: // Checking CL binding
152: result = (DirContext) clBindings.get(currentCL);
153: if (result != null)
154: return result;
155:
156: // Checking thread biding
157: result = (DirContext) threadBindings.get(currentThread);
158:
159: // Checking parent CL binding
160: currentCL = currentCL.getParent();
161: while (currentCL != null) {
162: result = (DirContext) clBindings.get(currentCL);
163: if (result != null)
164: return result;
165: currentCL = currentCL.getParent();
166: }
167:
168: if (result == null)
169: throw new IllegalStateException(
170: "Illegal class loader binding");
171:
172: return result;
173:
174: }
175:
176: /**
177: * Binds a directory context to a class loader.
178: */
179: public static void bind(ClassLoader cl, DirContext dirContext) {
180: clBindings.put(cl, dirContext);
181: }
182:
183: /**
184: * Unbinds a directory context to a class loader.
185: */
186: public static void unbind(ClassLoader cl) {
187: clBindings.remove(cl);
188: }
189:
190: /**
191: * Get the bound context.
192: */
193: public static DirContext get(ClassLoader cl) {
194: return (DirContext) clBindings.get(cl);
195: }
196:
197: /**
198: * Get the bound context.
199: */
200: public static DirContext get(Thread thread) {
201: return (DirContext) threadBindings.get(thread);
202: }
203:
204: }
|