1 11 12 package org.eclipse.osgi.framework.internal.protocol; 13 14 import java.lang.reflect.Method ; 15 import java.net.*; 16 import java.security.AccessController ; 17 import java.util.*; 18 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 19 import org.eclipse.osgi.framework.internal.core.Constants; 20 import org.eclipse.osgi.framework.internal.core.Msg; 21 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 22 import org.eclipse.osgi.framework.util.SecureAction; 23 import org.eclipse.osgi.util.NLS; 24 import org.osgi.framework.BundleContext; 25 import org.osgi.service.url.URLConstants; 26 import org.osgi.util.tracker.ServiceTracker; 27 28 31 public class StreamHandlerFactory extends MultiplexingFactory implements URLStreamHandlerFactory { 32 static final SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction()); 33 34 private ServiceTracker handlerTracker; 35 36 protected static final String URLSTREAMHANDLERCLASS = "org.osgi.service.url.URLStreamHandlerService"; protected static final String PROTOCOL_HANDLER_PKGS = "java.protocol.handler.pkgs"; protected static final String INTERNAL_PROTOCOL_HANDLER_PKG = "org.eclipse.osgi.framework.internal.protocol"; 40 private static final List ignoredClasses = Arrays.asList(new Class [] {MultiplexingURLStreamHandler.class, StreamHandlerFactory.class, URL.class}); 41 private Hashtable proxies; 42 private URLStreamHandlerFactory parentFactory; 43 44 49 public StreamHandlerFactory(BundleContext context, FrameworkAdaptor adaptor) { 50 super(context, adaptor); 51 52 proxies = new Hashtable(15); 53 handlerTracker = new ServiceTracker(context, URLSTREAMHANDLERCLASS, null); 54 handlerTracker.open(); 55 } 56 57 private Class getBuiltIn(String protocol, String builtInHandlers, boolean fromFramework) { 58 if (builtInHandlers == null) 59 return null; 60 Class clazz; 61 StringTokenizer tok = new StringTokenizer(builtInHandlers, "|"); while (tok.hasMoreElements()) { 63 StringBuffer name = new StringBuffer (); 64 name.append(tok.nextToken()); 65 name.append("."); name.append(protocol); 67 name.append(".Handler"); try { 69 if (fromFramework) 70 clazz = secureAction.forName(name.toString()); 71 else 72 clazz = secureAction.loadSystemClass(name.toString()); 73 if (clazz != null) 74 return clazz; } catch (ClassNotFoundException ex) { 76 } 78 } 79 return null; 80 } 81 82 89 public URLStreamHandler createURLStreamHandler(String protocol) { 90 String builtInHandlers = secureAction.getProperty(PROTOCOL_HANDLER_PKGS); 92 Class clazz = getBuiltIn(protocol, builtInHandlers, false); 93 if (clazz != null) 94 return null; URLStreamHandler result = null; 96 if (isMultiplexing()) { 97 if (findAuthorizedURLStreamHandler(protocol) != null) 98 result = new MultiplexingURLStreamHandler(protocol, this); 99 } else { 100 result = createInternalURLStreamHandler(protocol); 101 } 102 if (result == null && parentFactory != null) 104 result = parentFactory.createURLStreamHandler(protocol); 105 return result; } 107 108 public URLStreamHandler createInternalURLStreamHandler(String protocol) { 109 110 String internalHandlerPkgs = secureAction.getProperty(Constants.INTERNAL_HANDLER_PKGS); 112 internalHandlerPkgs = internalHandlerPkgs == null ? INTERNAL_PROTOCOL_HANDLER_PKG : internalHandlerPkgs + '|' + INTERNAL_PROTOCOL_HANDLER_PKG; 113 Class clazz = getBuiltIn(protocol, internalHandlerPkgs, true); 114 115 if (clazz == null) { 116 URLStreamHandlerProxy handler = (URLStreamHandlerProxy) proxies.get(protocol); 119 if (handler != null) 120 return (handler); 121 org.osgi.framework.ServiceReference[] serviceReferences = handlerTracker.getServiceReferences(); 123 if (serviceReferences == null) 124 return null; 125 for (int i = 0; i < serviceReferences.length; i++) { 126 Object prop = serviceReferences[i].getProperty(URLConstants.URL_HANDLER_PROTOCOL); 127 if (prop instanceof String ) 128 prop = new String [] {(String ) prop}; if (!(prop instanceof String [])) { 130 String message = NLS.bind(Msg.URL_HANDLER_INCORRECT_TYPE, new Object [] {URLConstants.URL_HANDLER_PROTOCOL, URLSTREAMHANDLERCLASS, serviceReferences[i].getBundle()}); 131 adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, message, 0, null, null)); 132 continue; 133 } 134 String [] protocols = (String []) prop; 135 for (int j = 0; j < protocols.length; j++) 136 if (protocols[j].equals(protocol)) { 137 handler = new URLStreamHandlerProxy(protocol, serviceReferences[i], context); 138 proxies.put(protocol, handler); 139 return (handler); 140 } 141 } 142 return null; 143 } 144 145 try { 147 URLStreamHandler handler = (URLStreamHandler) clazz.newInstance(); 148 149 if (handler instanceof ProtocolActivator) { 150 ((ProtocolActivator) handler).start(context, adaptor); 151 } 152 153 return handler; 154 } catch (Exception e) { 155 return null; 156 } 157 } 158 159 protected URLStreamHandler findAuthorizedURLStreamHandler(String protocol) { 160 Object factory = findAuthorizedFactory(ignoredClasses); 161 if (factory == null) 162 return null; 163 164 if (factory == this) 165 return createInternalURLStreamHandler(protocol); 166 167 try { 168 Method createInternalURLStreamHandlerMethod = factory.getClass().getMethod("createInternalURLStreamHandler", new Class [] {String .class}); return (URLStreamHandler) createInternalURLStreamHandlerMethod.invoke(factory, new Object [] {protocol}); 170 } catch (Exception e) { 171 adaptor.getFrameworkLog().log(new FrameworkLogEntry(StreamHandlerFactory.class.getName(), "findAuthorizedURLStreamHandler-loop", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 173 } 174 } 175 176 public Object getParentFactory() { 177 return parentFactory; 178 } 179 180 public void setParentFactory(Object parentFactory) { 181 if (this.parentFactory == null) this.parentFactory = (URLStreamHandlerFactory) parentFactory; 183 } 184 } 185 | Popular Tags |