KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > protocol > StreamHandlerFactory


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.protocol;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.net.*;
16 import java.security.AccessController JavaDoc;
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 /**
29  * This class contains the URL stream handler factory for the OSGi framework.
30  */

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 JavaDoc URLSTREAMHANDLERCLASS = "org.osgi.service.url.URLStreamHandlerService"; //$NON-NLS-1$
37
protected static final String JavaDoc PROTOCOL_HANDLER_PKGS = "java.protocol.handler.pkgs"; //$NON-NLS-1$
38
protected static final String JavaDoc INTERNAL_PROTOCOL_HANDLER_PKG = "org.eclipse.osgi.framework.internal.protocol"; //$NON-NLS-1$
39

40     private static final List ignoredClasses = Arrays.asList(new Class JavaDoc[] {MultiplexingURLStreamHandler.class, StreamHandlerFactory.class, URL.class});
41     private Hashtable proxies;
42     private URLStreamHandlerFactory parentFactory;
43
44     /**
45      * Create the factory.
46      *
47      * @param context BundleContext for the system bundle
48      */

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 JavaDoc getBuiltIn(String JavaDoc protocol, String JavaDoc builtInHandlers, boolean fromFramework) {
58         if (builtInHandlers == null)
59             return null;
60         Class JavaDoc clazz;
61         StringTokenizer tok = new StringTokenizer(builtInHandlers, "|"); //$NON-NLS-1$
62
while (tok.hasMoreElements()) {
63             StringBuffer JavaDoc name = new StringBuffer JavaDoc();
64             name.append(tok.nextToken());
65             name.append("."); //$NON-NLS-1$
66
name.append(protocol);
67             name.append(".Handler"); //$NON-NLS-1$
68
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; //this class exists, it is a built in handler
75
} catch (ClassNotFoundException JavaDoc ex) {
76                 // keep looking
77
}
78         }
79         return null;
80     }
81
82     /**
83      * Creates a new URLStreamHandler instance for the specified
84      * protocol.
85      *
86      * @param protocol The desired protocol
87      * @return a URLStreamHandler for the specific protocol.
88      */

89     public URLStreamHandler createURLStreamHandler(String JavaDoc protocol) {
90         //first check for built in handlers
91
String JavaDoc builtInHandlers = secureAction.getProperty(PROTOCOL_HANDLER_PKGS);
92         Class JavaDoc clazz = getBuiltIn(protocol, builtInHandlers, false);
93         if (clazz != null)
94             return null; // let the VM handle it
95
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 parent is present do parent lookup
103
if (result == null && parentFactory != null)
104             result = parentFactory.createURLStreamHandler(protocol);
105         return result; //result may be null; let the VM handle it (consider sun.net.protocol.www.*)
106
}
107
108     public URLStreamHandler createInternalURLStreamHandler(String JavaDoc protocol) {
109
110         //internal protocol handlers
111
String JavaDoc internalHandlerPkgs = secureAction.getProperty(Constants.INTERNAL_HANDLER_PKGS);
112         internalHandlerPkgs = internalHandlerPkgs == null ? INTERNAL_PROTOCOL_HANDLER_PKG : internalHandlerPkgs + '|' + INTERNAL_PROTOCOL_HANDLER_PKG;
113         Class JavaDoc clazz = getBuiltIn(protocol, internalHandlerPkgs, true);
114
115         if (clazz == null) {
116             //Now we check the service registry
117
//first check to see if the handler is in the cache
118
URLStreamHandlerProxy handler = (URLStreamHandlerProxy) proxies.get(protocol);
119             if (handler != null)
120                 return (handler);
121             //look through the service registry for a URLStramHandler registered for this protocol
122
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 JavaDoc prop = serviceReferences[i].getProperty(URLConstants.URL_HANDLER_PROTOCOL);
127                 if (prop instanceof String JavaDoc)
128                     prop = new String JavaDoc[] {(String JavaDoc) prop}; // TODO should this be a warning?
129
if (!(prop instanceof String JavaDoc[])) {
130                     String JavaDoc message = NLS.bind(Msg.URL_HANDLER_INCORRECT_TYPE, new Object JavaDoc[] {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 JavaDoc[] protocols = (String JavaDoc[]) 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         // must be a built-in handler
146
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 JavaDoc e) {
155             return null;
156         }
157     }
158
159     protected URLStreamHandler findAuthorizedURLStreamHandler(String JavaDoc protocol) {
160         Object JavaDoc factory = findAuthorizedFactory(ignoredClasses);
161         if (factory == null)
162             return null;
163
164         if (factory == this)
165             return createInternalURLStreamHandler(protocol);
166
167         try {
168             Method JavaDoc createInternalURLStreamHandlerMethod = factory.getClass().getMethod("createInternalURLStreamHandler", new Class JavaDoc[] {String JavaDoc.class}); //$NON-NLS-1$
169
return (URLStreamHandler) createInternalURLStreamHandlerMethod.invoke(factory, new Object JavaDoc[] {protocol});
170         } catch (Exception JavaDoc e) {
171             adaptor.getFrameworkLog().log(new FrameworkLogEntry(StreamHandlerFactory.class.getName(), "findAuthorizedURLStreamHandler-loop", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
172
throw new RuntimeException JavaDoc(e.getMessage());
173         }
174     }
175
176     public Object JavaDoc getParentFactory() {
177         return parentFactory;
178     }
179
180     public void setParentFactory(Object JavaDoc parentFactory) {
181         if (this.parentFactory == null) // only allow it to be set once
182
this.parentFactory = (URLStreamHandlerFactory) parentFactory;
183     }
184 }
185
Popular Tags