1 11 12 package org.eclipse.osgi.framework.internal.protocol; 13 14 import java.lang.reflect.Method ; 15 import java.net.ContentHandler ; 16 import java.net.URLConnection ; 17 import java.util.*; 18 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 19 import org.eclipse.osgi.framework.internal.core.Msg; 20 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.framework.BundleContext; 23 import org.osgi.service.url.URLConstants; 24 import org.osgi.util.tracker.ServiceTracker; 25 26 33 public class ContentHandlerFactory extends MultiplexingFactory implements java.net.ContentHandlerFactory { 35 private ServiceTracker contentHandlerTracker; 36 37 private static final String contentHandlerClazz = "java.net.ContentHandler"; private static final String CONTENT_HANDLER_PKGS = "java.content.handler.pkgs"; private static final String DEFAULT_VM_CONTENT_HANDLERS = "sun.net.www.content"; 41 private static final List ignoredClasses = Arrays.asList(new Class [] {MultiplexingContentHandler.class, ContentHandlerFactory.class, URLConnection .class}); 42 43 private Hashtable proxies; 44 private java.net.ContentHandlerFactory parentFactory; 45 46 public ContentHandlerFactory(BundleContext context, FrameworkAdaptor adaptor) { 47 super(context, adaptor); 48 49 proxies = new Hashtable(5); 50 51 contentHandlerTracker = new ServiceTracker(context, contentHandlerClazz, null); 53 contentHandlerTracker.open(); 54 } 55 56 59 public ContentHandler createContentHandler(String contentType) { 61 String builtInHandlers = StreamHandlerFactory.secureAction.getProperty(CONTENT_HANDLER_PKGS); 64 builtInHandlers = builtInHandlers == null ? DEFAULT_VM_CONTENT_HANDLERS : DEFAULT_VM_CONTENT_HANDLERS + '|' + builtInHandlers; 65 Class clazz = null; 66 if (builtInHandlers != null) { 67 70 String convertedContentType = contentType.replace('.', '_'); 72 convertedContentType = convertedContentType.replace('/', '.'); 73 convertedContentType = convertedContentType.replace('-', '_'); 74 StringTokenizer tok = new StringTokenizer(builtInHandlers, "|"); while (tok.hasMoreElements()) { 76 StringBuffer name = new StringBuffer (); 77 name.append(tok.nextToken()); 78 name.append("."); name.append(convertedContentType); 80 try { 81 clazz = StreamHandlerFactory.secureAction.loadSystemClass(name.toString()); 82 if (clazz != null) { 83 return (null); } 85 } catch (ClassNotFoundException ex) { 86 } 88 } 89 } 90 91 if (isMultiplexing()) 92 return new MultiplexingContentHandler(contentType, this); 93 94 return createInternalContentHandler(contentType); 95 } 96 97 public ContentHandler createInternalContentHandler(String contentType) { 98 ContentHandlerProxy proxy = (ContentHandlerProxy) proxies.get(contentType); 100 if (proxy != null) { 101 return (proxy); 102 } 103 org.osgi.framework.ServiceReference[] serviceReferences = contentHandlerTracker.getServiceReferences(); 104 if (serviceReferences != null) { 105 for (int i = 0; i < serviceReferences.length; i++) { 106 Object prop = serviceReferences[i].getProperty(URLConstants.URL_CONTENT_MIMETYPE); 107 if (prop instanceof String ) 108 prop = new String [] {(String ) prop}; if (!(prop instanceof String [])) { 110 String message = NLS.bind(Msg.URL_HANDLER_INCORRECT_TYPE, new Object [] {URLConstants.URL_CONTENT_MIMETYPE, contentHandlerClazz, serviceReferences[i].getBundle()}); 111 adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, message, 0, null, null)); 112 continue; 113 } 114 String [] contentHandler = (String []) prop; 115 for (int j = 0; j < contentHandler.length; j++) { 116 if (contentHandler[j].equals(contentType)) { 117 proxy = new ContentHandlerProxy(contentType, serviceReferences[i], context); 118 proxies.put(contentType, proxy); 119 return (proxy); 120 } 121 } 122 } 123 } 124 if (parentFactory != null) { 126 ContentHandler parentHandler = parentFactory.createContentHandler(contentType); 127 if (parentHandler != null) 128 return parentHandler; 129 } 130 proxy = new ContentHandlerProxy(contentType, null, context); 133 proxies.put(contentType, proxy); 134 return (proxy); 135 } 136 137 public synchronized ContentHandler findAuthorizedContentHandler(String contentType) { 138 Object factory = findAuthorizedFactory(ignoredClasses); 139 if (factory == null) 140 return null; 141 142 if (factory == this) 143 return createInternalContentHandler(contentType); 144 145 try { 146 Method createInternalContentHandlerMethod = factory.getClass().getMethod("createInternalContentHandler", new Class [] {String .class}); return (ContentHandler ) createInternalContentHandlerMethod.invoke(factory, new Object [] {contentType}); 148 } catch (Exception e) { 149 adaptor.getFrameworkLog().log(new FrameworkLogEntry(ContentHandlerFactory.class.getName(), "findAuthorizedContentHandler-loop", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 151 } 152 } 153 154 public Object getParentFactory() { 155 return parentFactory; 156 } 157 158 public void setParentFactory(Object parentFactory) { 159 if (this.parentFactory == null) this.parentFactory = (java.net.ContentHandlerFactory ) parentFactory; 161 } 162 } 163 | Popular Tags |