00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import java.net.*;
00038 import org.osgi.service.url.*;
00039 import org.osgi.framework.*;
00040 import java.util.Map;
00041 import java.util.HashMap;
00042
00047 public class ServiceURLStreamHandlerFactory
00048 implements URLStreamHandlerFactory
00049 {
00050 Framework framework;
00051
00052
00053
00054
00055
00056 Map handlers = new HashMap();
00057
00058
00059 String[] jvmPkgs = null;
00060
00061
00062
00063
00064
00065
00066
00067 Map wrapMap = new HashMap();
00068
00069
00070 ServiceURLStreamHandlerFactory(Framework fw) {
00071 this.framework = fw;
00072
00073
00074 String s = System.getProperty("java.protocol.handler.pkgs", "");
00075
00076 jvmPkgs = Util.splitwords(s, "|");
00077
00078 for(int i = 0; i < jvmPkgs.length; i++) {
00079 jvmPkgs[i] = jvmPkgs[i].trim();
00080 if(Debug.url) {
00081 Debug.println("JVMClassPath - URLHandler jvmPkgs[" + i + "]=" + jvmPkgs[i]);
00082 }
00083 }
00084 }
00085
00086 public URLStreamHandler createURLStreamHandler(String protocol) {
00087 synchronized(handlers) {
00088
00089 if(Debug.url) {
00090 Debug.println("createURLStreamHandler protocol=" + protocol);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 URLStreamHandler handler = getJVMClassPathHandler(protocol);
00100 if(handler != null) {
00101 if(Debug.url) {
00102 Debug.println("using JVMClassPath handler for " + protocol);
00103 }
00104 return handler;
00105 }
00106
00107
00108 handler = (URLStreamHandler)handlers.get(protocol);
00109 if(handler != null) {
00110 if(Debug.url) {
00111 Debug.println("using predefined handler for " + protocol);
00112 }
00113 return handler;
00114 }
00115
00116 handler = getServiceHandler(protocol);
00117 if(handler != null) {
00118 if(Debug.url) {
00119 Debug.println("Using service URLHandler for " + protocol);
00120 }
00121 return handler;
00122 }
00123
00124 if(Debug.url) {
00125 Debug.println("Using default URLHandler for " + protocol);
00126 }
00127 return null;
00128 }
00129 }
00130
00131 URLStreamHandler getServiceHandler(String protocol) {
00132 try {
00133 String filter =
00134 "(" +
00135 URLConstants.URL_HANDLER_PROTOCOL +
00136 "=" + protocol +
00137 ")";
00138
00139 ServiceReference[] srl = framework.services
00140 .get(URLStreamHandlerService.class.getName(), filter, null, false);
00141
00142 if(srl != null && srl.length > 0) {
00143 URLStreamHandlerWrapper wrapper = (URLStreamHandlerWrapper)
00144 wrapMap.get(protocol);
00145
00146 if(wrapper == null) {
00147 wrapper = new URLStreamHandlerWrapper(framework, protocol);
00148 wrapMap.put(protocol, wrapper);
00149 }
00150
00151 return wrapper;
00152 }
00153 } catch (InvalidSyntaxException e) {
00154 throw new RuntimeException("Failed to get service: " + e);
00155 }
00156
00157
00158 return null;
00159 }
00160
00161
00165 URLStreamHandler getJVMClassPathHandler(String protocol) {
00166
00167 for(int i = 0; i < jvmPkgs.length; i++) {
00168 String className = jvmPkgs[i] + "." + protocol + ".Handler";
00169 try {
00170 if(Debug.url) {
00171 Debug.println("JVMClassPath - trying URLHandler class=" + className);
00172 }
00173 Class clazz = Class.forName(className);
00174 URLStreamHandler handler = (URLStreamHandler)clazz.newInstance();
00175
00176 if(Debug.url) {
00177 Debug.println("JVMClassPath - created URLHandler class=" + className);
00178 }
00179
00180 return handler;
00181 } catch (Throwable t) {
00182 if(Debug.url) {
00183 Debug.println("JVMClassPath - no URLHandler class " + className);
00184 }
00185 }
00186 }
00187
00188 if(Debug.url) {
00189 Debug.println("JVMClassPath - no URLHandler for " + protocol);
00190 }
00191
00192 return null;
00193 }
00194
00195
00206 void setURLStreamHandler(String protocol, URLStreamHandler handler) {
00207 handlers.put(protocol, handler);
00208 }
00209 }
00210