1 11 12 package org.eclipse.osgi.framework.internal.protocol; 13 14 import java.io.IOException ; 15 import java.net.*; 16 import org.osgi.framework.*; 17 import org.osgi.service.url.URLConstants; 18 import org.osgi.service.url.URLStreamHandlerService; 19 import org.osgi.util.tracker.ServiceTracker; 20 import org.osgi.util.tracker.ServiceTrackerCustomizer; 21 22 31 32 public class URLStreamHandlerProxy extends URLStreamHandler implements ServiceTrackerCustomizer { 33 protected URLStreamHandlerService realHandlerService; 35 36 protected URLStreamHandlerSetter urlSetter; 37 38 protected ServiceTracker urlStreamHandlerServiceTracker; 39 40 protected BundleContext context; 41 protected ServiceReference urlStreamServiceReference; 42 43 protected String protocol; 44 45 protected int ranking = Integer.MIN_VALUE; 46 47 public URLStreamHandlerProxy(String protocol, ServiceReference reference, BundleContext context) { 48 this.context = context; 49 this.protocol = protocol; 50 51 urlSetter = new URLStreamHandlerSetter(this); 52 53 setNewHandler(reference, getRank(reference)); 55 56 urlStreamHandlerServiceTracker = new ServiceTracker(context, StreamHandlerFactory.URLSTREAMHANDLERCLASS, this); 57 StreamHandlerFactory.secureAction.open(urlStreamHandlerServiceTracker); 58 } 59 60 private void setNewHandler(ServiceReference reference, int rank) { 61 if (urlStreamServiceReference != null) 62 context.ungetService(urlStreamServiceReference); 63 64 urlStreamServiceReference = reference; 65 ranking = rank; 66 67 if (reference == null) 68 realHandlerService = new NullURLStreamHandlerService(); 69 else 70 realHandlerService = (URLStreamHandlerService) StreamHandlerFactory.secureAction.getService(reference, context); 71 } 72 73 76 protected boolean equals(URL url1, URL url2) { 77 return realHandlerService.equals(url1, url2); 78 } 79 80 83 protected int getDefaultPort() { 84 return realHandlerService.getDefaultPort(); 85 } 86 87 90 protected InetAddress getHostAddress(URL url) { 91 return realHandlerService.getHostAddress(url); 92 } 93 94 97 protected int hashCode(URL url) { 98 return realHandlerService.hashCode(url); 99 } 100 101 104 protected boolean hostsEqual(URL url1, URL url2) { 105 return realHandlerService.hostsEqual(url1, url2); 106 } 107 108 111 protected URLConnection openConnection(URL url) throws IOException { 112 return realHandlerService.openConnection(url); 113 } 114 115 118 protected void parseURL(URL url, String str, int start, int end) { 119 realHandlerService.parseURL(urlSetter, url, str, start, end); 120 } 121 122 125 protected boolean sameFile(URL url1, URL url2) { 126 return realHandlerService.sameFile(url1, url2); 127 } 128 129 132 protected String toExternalForm(URL url) { 133 return realHandlerService.toExternalForm(url); 134 } 135 136 139 public void setURL(URL u, String protocol, String host, int port, String authority, String userInfo, String file, String query, String ref) { 140 super.setURL(u, protocol, host, port, authority, userInfo, file, query, ref); 141 } 142 143 144 public void setURL(URL url, String protocol, String host, int port, String file, String ref) { 145 146 super.setURL(url, protocol, host, port, null, null, file, null, ref); 149 } 150 151 154 public Object addingService(ServiceReference reference) { 155 Object prop = reference.getProperty(URLConstants.URL_HANDLER_PROTOCOL); 157 if (!(prop instanceof String [])) 158 return null; 159 String [] protocols = (String []) prop; 160 for (int i = 0; i < protocols.length; i++) { 161 if (protocols[i].equals(protocol)) { 162 int newServiceRanking = getRank(reference); 164 if (newServiceRanking > ranking || urlStreamServiceReference == null) 165 setNewHandler(reference, newServiceRanking); 166 return (reference); 167 } 168 } 169 170 return (null); 172 } 173 174 177 public void modifiedService(ServiceReference reference, Object service) { 179 int newRank = getRank(reference); 180 if (reference == urlStreamServiceReference) { 181 if (newRank < ranking) { 182 ServiceReference newReference = urlStreamHandlerServiceTracker.getServiceReference(); 186 if (newReference != urlStreamServiceReference && newReference != null) { 187 setNewHandler(newReference, ((Integer ) newReference.getProperty(Constants.SERVICE_RANKING)).intValue()); 188 } 189 } 190 } else if (newRank > ranking) { 191 setNewHandler(reference, newRank); 194 } 195 } 196 197 200 public void removedService(ServiceReference reference, Object service) { 201 if (reference != urlStreamServiceReference) 203 return; 204 ServiceReference newReference = urlStreamHandlerServiceTracker.getServiceReference(); 207 setNewHandler(newReference, getRank(newReference)); 209 } 210 211 private int getRank(ServiceReference reference) { 212 if (reference == null) 213 return Integer.MIN_VALUE; 214 Object property = reference.getProperty(Constants.SERVICE_RANKING); 215 return (property instanceof Integer ) ? ((Integer ) property).intValue() : 0; 216 } 217 218 } 219 | Popular Tags |