1 22 package org.jboss.net.protocol; 23 24 import java.net.URL ; 25 import java.net.URLStreamHandler ; 26 import java.util.Collections ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import org.jboss.logging.Logger; 33 34 55 public class URLStreamHandlerFactory 56 implements java.net.URLStreamHandlerFactory 57 { 58 59 private static final Logger log = Logger.getLogger(URLStreamHandlerFactory.class); 60 61 62 public static final String PACKAGE_PREFIX = "org.jboss.net.protocol"; 63 67 private static Map handlerMap = Collections.synchronizedMap(new HashMap ()); 68 69 98 private static ThreadLocal createURLStreamHandlerProtocol = new ThreadLocal (); 99 100 103 private String [] handlerPkgs = {PACKAGE_PREFIX}; 104 108 private String lastHandlerPkgs = PACKAGE_PREFIX; 109 110 111 public static final String PROTOCOLS[] = { 112 "resource", 113 "file" 114 }; 115 116 120 public static void preload() 121 { 122 for (int i = 0; i < PROTOCOLS.length; i ++) 123 { 124 try 125 { 126 URL url = new URL (PROTOCOLS[i], "", -1, ""); 127 log.trace("Loaded protocol: " + PROTOCOLS[i]); 128 } 129 catch (Exception e) 130 { 131 log.warn("Failed to load protocol: " + PROTOCOLS[i], e); 132 } 133 } 134 } 135 136 139 public static void clear() 140 { 141 handlerMap.clear(); 142 } 143 144 151 public URLStreamHandler createURLStreamHandler(final String protocol) 152 { 153 URLStreamHandler handler = (URLStreamHandler ) handlerMap.get(protocol); 155 if( handler != null ) 156 return handler; 157 158 String prevProtocol = (String ) createURLStreamHandlerProtocol.get(); 160 if( prevProtocol != null && prevProtocol.equals(protocol) ) 161 return null; 162 createURLStreamHandlerProtocol.set(protocol); 163 164 checkHandlerPkgs(); 166 167 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); 169 for(int p = 0; p < handlerPkgs.length; p ++) 170 { 171 try 172 { 173 String classname = handlerPkgs[p] + "." + protocol + ".Handler"; 175 Class type = null; 176 177 try 178 { 179 type = ctxLoader.loadClass(classname); 180 } 181 catch(ClassNotFoundException e) 182 { 183 type = Class.forName(classname); 185 } 186 187 if( type != null ) 188 { 189 handler = (URLStreamHandler ) type.newInstance(); 190 handlerMap.put(protocol, handler); 191 log.trace("Found protocol:"+protocol+" handler:"+handler); 192 } 193 } 194 catch (Throwable ignore) 195 { 196 } 197 } 198 199 createURLStreamHandlerProtocol.set(null); 200 return handler; 201 } 202 203 206 private synchronized void checkHandlerPkgs() 207 { 208 String handlerPkgsProp = System.getProperty("java.protocol.handler.pkgs"); 209 if( handlerPkgsProp != null && handlerPkgsProp.equals(lastHandlerPkgs) == false ) 210 { 211 StringTokenizer tokeninzer = new StringTokenizer (handlerPkgsProp, "|"); 213 ArrayList tmp = new ArrayList (); 214 while( tokeninzer.hasMoreTokens() ) 215 { 216 String pkg = tokeninzer.nextToken().intern(); 217 if( tmp.contains(pkg) == false ) 218 tmp.add(pkg); 219 } 220 if( tmp.contains(PACKAGE_PREFIX) == false ) 222 tmp.add(PACKAGE_PREFIX); 223 handlerPkgs = new String [tmp.size()]; 224 tmp.toArray(handlerPkgs); 225 lastHandlerPkgs = handlerPkgsProp; 226 } 227 } 228 229 } 230 | Popular Tags |