1 17 18 package org.apache.geronimo.system.url; 19 20 import java.lang.reflect.Field ; 21 import java.net.URL ; 22 import java.net.URLStreamHandler ; 23 import java.security.AccessController ; 24 import java.security.PrivilegedAction ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import org.apache.geronimo.gbean.GBeanInfo; 33 import org.apache.geronimo.gbean.GBeanInfoBuilder; 34 import org.apache.geronimo.gbean.GBeanLifecycle; 35 36 43 public class GeronimoURLFactory implements GBeanLifecycle { 44 private static final URLStreamHandlerFactory factory = new URLStreamHandlerFactory(); 45 private static boolean installed = false; 46 47 public void doStart() throws Exception { 48 install(); 51 } 52 53 public void doStop() throws Exception { 54 } 55 56 public void doFail() { 57 } 58 59 66 public void registerHandler(String protocol, URLStreamHandler handler) { 67 factory.registerHandler(protocol, handler); 68 } 69 70 76 public URLStreamHandler getRegisteredHandler(String protocol) { 77 return factory.getRegisteredHandler(protocol); 78 } 79 80 85 public Map getRegisteredHandlers() { 86 return factory.getRegisteredHandlers(); 87 } 88 89 94 public static boolean isInstalled() { 95 return installed; 96 } 97 98 108 public static void install() throws Error , SecurityException { 109 if (!installed) { 110 URL.setURLStreamHandlerFactory(factory); 111 installed = true; 112 } 113 } 114 115 126 public static void forceInstall() throws Error , SecurityException { 127 if (!installed) { 128 Throwable t = (Throwable ) AccessController.doPrivileged(new PrivilegedAction () { 130 public Object run() { 131 try { 132 Field streamHandlerLockField = URL .class.getDeclaredField("streamHandlerLock"); 135 streamHandlerLockField.setAccessible(true); 136 Object streamHandlerLock = streamHandlerLockField.get(null); 137 138 synchronized (streamHandlerLock) { 139 Field factoryField = URL .class.getDeclaredField("factory"); 142 factoryField.setAccessible(true); 143 144 Field handlersField = URL .class.getDeclaredField("handlers"); 147 handlersField.setAccessible(true); 148 149 Map handlers = (Map ) handlersField.get(null); 151 152 factoryField.set(null, factory); 154 155 handlers.clear(); 157 } 158 } catch (Throwable e) { 159 return e; 160 } 161 return null; 162 } 163 }); 164 165 if (t != null) { 166 if (t instanceof SecurityException ) { 167 throw (SecurityException ) t; 168 } else if (t instanceof Error ) { 169 throw (Error ) t; 170 } 171 throw new Error ("Unknown error while force installing URL factory", t); 172 } 173 installed = true; 174 } 175 } 176 177 private static class URLStreamHandlerFactory implements java.net.URLStreamHandlerFactory { 178 private final Map handlers = new HashMap (); 179 private final List handlerPackages = new LinkedList (); 180 181 private URLStreamHandlerFactory() { 182 String systemPackages = System.getProperty("java.protocol.handler.pkgs"); 184 if (systemPackages != null) { 185 StringTokenizer stok = new StringTokenizer (systemPackages, "|"); 186 while (stok.hasMoreTokens()) { 187 handlerPackages.add(stok.nextToken().trim()); 188 } 189 } 190 handlerPackages.add("sun.net.www.protocol"); 192 193 handlers.put("file", new org.apache.geronimo.system.url.file.Handler()); 197 handlers.put("resource", new org.apache.geronimo.system.url.resource.Handler()); 198 } 199 200 public URLStreamHandler createURLStreamHandler(String protocol) { 201 if (protocol == null) { 202 throw new IllegalArgumentException ("protocol is null"); 203 } 204 protocol = protocol.trim(); 205 206 URLStreamHandler handler; 207 208 synchronized (this) { 210 handler = (URLStreamHandler ) handlers.get(protocol); 211 } 212 if (handler != null) { 213 return handler; 214 } 215 216 Class type = findProtocolHandler(protocol); 218 if (type == null) { 219 throw new IllegalArgumentException ("unknown protocol: " + protocol); 220 } 221 222 try { 223 return (URLStreamHandler ) type.newInstance(); 224 } catch (Exception e) { 225 throw new IllegalArgumentException ("Error constructing protocol handler:" + 226 "protocol " + protocol + 227 " messgae=" + e.getMessage()); 228 } 229 } 230 231 private synchronized void registerHandler(String protocol, URLStreamHandler handler) { 232 assert protocol != null: "protocol is null"; 233 assert handler != null: "handler is null"; 234 if (handlers.containsKey(protocol)) { 235 throw new IllegalStateException ("Protocol already has a registered handler: " + protocol); 236 } 237 handlers.put(protocol, handler); 238 } 239 240 private synchronized URLStreamHandler getRegisteredHandler(String protocol) { 241 return (URLStreamHandler ) handlers.get(protocol); 242 } 243 244 private synchronized Map getRegisteredHandlers() { 245 return new HashMap (handlers); 246 } 247 248 255 private Class findProtocolHandler(final String protocol) { 256 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 257 if (classLoader == null) { 258 classLoader = ClassLoader.getSystemClassLoader(); 259 } 260 for (Iterator iterator = handlerPackages.iterator(); iterator.hasNext();) { 261 String pkg = (String ) iterator.next(); 262 String classname = pkg + "." + protocol + ".Handler"; 263 264 try { 265 return classLoader.loadClass(classname); 266 } catch (Throwable e) { 267 } 269 } 270 return null; 271 } 272 } 273 274 public static final GBeanInfo GBEAN_INFO; 275 276 static { 277 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(GeronimoURLFactory.class); 278 infoFactory.addOperation("registerHandler", new Class []{String .class, URLStreamHandler .class}); 279 infoFactory.addOperation("getRegisteredHandler", new Class []{String .class}); 280 infoFactory.addOperation("getRegisteredHandlers", new Class []{}); 281 GBEAN_INFO = infoFactory.getBeanInfo(); 282 } 283 284 public static GBeanInfo getGBeanInfo() { 285 return GBEAN_INFO; 286 } 287 } 288 | Popular Tags |