1 16 17 package org.apache.naming.handler.jndi; 18 19 import java.io.IOException ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 import java.net.URLStreamHandler ; 23 import java.util.Hashtable ; 24 25 import javax.naming.directory.DirContext ; 26 27 33 public class DirContextURLStreamHandler 34 extends URLStreamHandler { 35 36 37 39 40 public DirContextURLStreamHandler() { 41 } 42 43 44 public DirContextURLStreamHandler(DirContext context) { 45 this.context = context; 46 } 47 48 49 51 52 55 private static Hashtable clBindings = new Hashtable (); 56 57 58 61 private static Hashtable threadBindings = new Hashtable (); 62 63 64 66 67 70 protected DirContext context = null; 71 72 73 75 76 78 79 83 protected URLConnection openConnection(URL u) 84 throws IOException { 85 DirContext currentContext = this.context; 86 if (currentContext == null) 87 currentContext = get(); 88 return new DirContextURLConnection(currentContext, u); 89 } 90 91 92 public static final String PROTOCOL_HANDLER_VARIABLE = 94 "java.protocol.handler.pkgs"; 95 96 public static final String PACKAGE = "org.apache.naming.handler.jndi"; 97 98 99 102 public static void setProtocolHandler() { 103 String 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 118 public static boolean isBound() { 119 return (clBindings.containsKey 120 (Thread.currentThread().getContextClassLoader())) 121 || (threadBindings.containsKey(Thread.currentThread())); 122 } 123 124 125 128 public static void bind(DirContext dirContext) { 129 ClassLoader currentCL = 130 Thread.currentThread().getContextClassLoader(); 131 if (currentCL != null) 132 clBindings.put(currentCL, dirContext); 133 } 134 135 136 139 public static void unbind() { 140 ClassLoader currentCL = 141 Thread.currentThread().getContextClassLoader(); 142 if (currentCL != null) 143 clBindings.remove(currentCL); 144 } 145 146 147 150 public static void bindThread(DirContext dirContext) { 151 threadBindings.put(Thread.currentThread(), dirContext); 152 } 153 154 155 158 public static void unbindThread() { 159 threadBindings.remove(Thread.currentThread()); 160 } 161 162 163 166 public static DirContext get() { 167 168 DirContext result = null; 169 170 Thread currentThread = Thread.currentThread(); 171 ClassLoader currentCL = currentThread.getContextClassLoader(); 172 173 result = (DirContext ) clBindings.get(currentCL); 175 if (result != null) 176 return result; 177 178 result = (DirContext ) threadBindings.get(currentThread); 180 181 currentCL = currentCL.getParent(); 183 while (currentCL != null) { 184 result = (DirContext ) clBindings.get(currentCL); 185 if (result != null) 186 return result; 187 currentCL = currentCL.getParent(); 188 } 189 190 if (result == null) 191 throw new IllegalStateException ("Illegal class loader binding"); 192 193 return result; 194 195 } 196 197 198 201 public static void bind(ClassLoader cl, DirContext dirContext) { 202 clBindings.put(cl, dirContext); 203 } 204 205 206 209 public static void unbind(ClassLoader cl) { 210 clBindings.remove(cl); 211 } 212 213 214 217 public static DirContext get(ClassLoader cl) { 218 return (DirContext ) clBindings.get(cl); 219 } 220 221 222 225 public static DirContext get(Thread thread) { 226 return (DirContext ) threadBindings.get(thread); 227 } 228 229 230 } 231 | Popular Tags |