1 17 18 package org.apache.naming.resources; 19 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 import java.net.URLStreamHandler ; 24 import java.util.Hashtable ; 25 26 import javax.naming.directory.DirContext ; 27 28 34 public class DirContextURLStreamHandler 35 extends URLStreamHandler { 36 37 38 40 41 public DirContextURLStreamHandler() { 42 } 43 44 45 public DirContextURLStreamHandler(DirContext context) { 46 this.context = context; 47 } 48 49 50 52 53 56 private static Hashtable clBindings = new Hashtable (); 57 58 59 62 private static Hashtable threadBindings = new Hashtable (); 63 64 65 67 68 71 protected DirContext context = null; 72 73 74 76 77 79 80 84 protected URLConnection openConnection(URL u) 85 throws IOException { 86 DirContext currentContext = this.context; 87 if (currentContext == null) 88 currentContext = get(); 89 return new DirContextURLConnection(currentContext, u); 90 } 91 92 93 95 96 99 protected String toExternalForm(URL u) { 100 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 result = new StringBuffer (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 130 131 134 public static void setProtocolHandler() { 135 String 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 150 public static boolean isBound() { 151 return (clBindings.containsKey 152 (Thread.currentThread().getContextClassLoader())) 153 || (threadBindings.containsKey(Thread.currentThread())); 154 } 155 156 157 160 public static void bind(DirContext dirContext) { 161 ClassLoader currentCL = 162 Thread.currentThread().getContextClassLoader(); 163 if (currentCL != null) 164 clBindings.put(currentCL, dirContext); 165 } 166 167 168 171 public static void unbind() { 172 ClassLoader currentCL = 173 Thread.currentThread().getContextClassLoader(); 174 if (currentCL != null) 175 clBindings.remove(currentCL); 176 } 177 178 179 182 public static void bindThread(DirContext dirContext) { 183 threadBindings.put(Thread.currentThread(), dirContext); 184 } 185 186 187 190 public static void unbindThread() { 191 threadBindings.remove(Thread.currentThread()); 192 } 193 194 195 198 public static DirContext get() { 199 200 DirContext result = null; 201 202 Thread currentThread = Thread.currentThread(); 203 ClassLoader currentCL = currentThread.getContextClassLoader(); 204 205 result = (DirContext ) clBindings.get(currentCL); 207 if (result != null) 208 return result; 209 210 result = (DirContext ) threadBindings.get(currentThread); 212 213 currentCL = currentCL.getParent(); 215 while (currentCL != null) { 216 result = (DirContext ) clBindings.get(currentCL); 217 if (result != null) 218 return result; 219 currentCL = currentCL.getParent(); 220 } 221 222 if (result == null) 223 throw new IllegalStateException ("Illegal class loader binding"); 224 225 return result; 226 227 } 228 229 230 233 public static void bind(ClassLoader cl, DirContext dirContext) { 234 clBindings.put(cl, dirContext); 235 } 236 237 238 241 public static void unbind(ClassLoader cl) { 242 clBindings.remove(cl); 243 } 244 245 246 249 public static DirContext get(ClassLoader cl) { 250 return (DirContext ) clBindings.get(cl); 251 } 252 253 254 257 public static DirContext get(Thread thread) { 258 return (DirContext ) threadBindings.get(thread); 259 } 260 261 262 } 263 | Popular Tags |