1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.IOException ; 15 import java.net.*; 16 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 17 import org.eclipse.osgi.baseadaptor.loader.BaseClassLoader; 18 import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.framework.*; 21 22 25 26 public abstract class BundleResourceHandler extends URLStreamHandler { 27 public static final String SECURITY_AUTHORIZED = "SECURITY_AUTHORIZED"; protected static BundleContext context; 29 protected BundleEntry bundleEntry; 30 31 34 public BundleResourceHandler() { 35 this(null); 36 } 37 38 public BundleResourceHandler(BundleEntry bundleEntry) { 39 this.bundleEntry = bundleEntry; 40 } 41 42 45 protected void parseURL(URL url, String str, int start, int end) { 46 if (end < start) 47 return; 48 if (url.getPath() != null) 49 bundleEntry = null; 52 String spec = ""; if (start < end) 54 spec = str.substring(start, end); 55 end -= start; 56 String path = url.getPath(); 58 String bundleId = url.getHost(); 59 int resIndex = 0; int pathIdx = 0; 61 if (spec.startsWith("//")) { int bundleIdIdx = 2; 63 pathIdx = spec.indexOf('/', bundleIdIdx); 64 if (pathIdx == -1) { 65 pathIdx = end; 66 path = ""; } 69 int bundleIdEnd = spec.indexOf(':', bundleIdIdx); 70 if (bundleIdEnd > pathIdx || bundleIdEnd == -1) 71 bundleIdEnd = pathIdx; 72 if (bundleIdEnd < pathIdx - 1) 73 try { 74 resIndex = Integer.parseInt(spec.substring(bundleIdEnd + 1, pathIdx)); 75 } catch (NumberFormatException e) { 76 } 78 bundleId = spec.substring(bundleIdIdx, bundleIdEnd); 79 } 80 if (pathIdx < end && spec.charAt(pathIdx) == '/') 81 path = spec.substring(pathIdx, end); 82 else if (end > pathIdx) { 83 if (path == null || path.equals("")) path = "/"; int last = path.lastIndexOf('/') + 1; 86 if (last == 0) 87 path = spec.substring(pathIdx, end); 88 else 89 path = path.substring(0, last) + spec.substring(pathIdx, end); 90 } 91 if (path == null) 92 path = ""; if (path.endsWith("/.") || path.endsWith("/..")) path = path + '/'; 98 int dotIndex; 99 while ((dotIndex = path.indexOf("/./")) >= 0) path = path.substring(0, dotIndex + 1) + path.substring(dotIndex + 3); 101 while ((dotIndex = path.indexOf("/../")) >= 0) { if (dotIndex != 0) 103 path = path.substring(0, path.lastIndexOf('/', dotIndex - 1)) + path.substring(dotIndex + 3); 104 else 105 path = path.substring(dotIndex + 3); 106 } 107 while ((dotIndex = path.indexOf("//")) >= 0) path = path.substring(0, dotIndex + 1) + path.substring(dotIndex + 2); 109 110 checkAdminPermission(context.getBundle(Long.parseLong(bundleId))); 113 114 setURL(url, url.getProtocol(), bundleId, resIndex, SECURITY_AUTHORIZED, null, path, null, url.getRef()); 119 } 120 121 131 protected URLConnection openConnection(URL url) throws IOException { 132 if (bundleEntry != null) return (new BundleURLConnection(url, bundleEntry)); 134 135 String bidString = url.getHost(); 136 if (bidString == null) { 137 throw new IOException (NLS.bind(AdaptorMsg.URL_NO_BUNDLE_ID, url.toExternalForm())); 138 } 139 AbstractBundle bundle = null; 140 long bundleID; 141 try { 142 bundleID = Long.parseLong(bidString); 143 } catch (NumberFormatException nfe) { 144 throw new MalformedURLException(NLS.bind(AdaptorMsg.URL_INVALID_BUNDLE_ID, bidString)); 145 } 146 bundle = (AbstractBundle) context.getBundle(bundleID); 147 if (!url.getAuthority().equals(SECURITY_AUTHORIZED)) { 151 checkAdminPermission(bundle); 153 } 154 155 if (bundle == null) { 156 throw new IOException (NLS.bind(AdaptorMsg.URL_NO_BUNDLE_FOUND, url.toExternalForm())); 157 } 158 return (new BundleURLConnection(url, findBundleEntry(url, bundle))); 159 } 160 161 169 abstract protected BundleEntry findBundleEntry(URL url, AbstractBundle bundle) throws IOException ; 170 171 177 protected String toExternalForm(URL url) { 178 StringBuffer result = new StringBuffer (url.getProtocol()); 179 result.append("://"); 181 String bundleId = url.getHost(); 182 if ((bundleId != null) && (bundleId.length() > 0)) 183 result.append(bundleId); 184 int index = url.getPort(); 185 if (index > 0) 186 result.append(':').append(index); 187 188 String path = url.getPath(); 189 if (path != null) { 190 if ((path.length() > 0) && (path.charAt(0) != '/')) 191 { 192 result.append("/"); } 194 195 result.append(path); 196 } 197 String ref = url.getRef(); 198 if (ref != null && ref.length() > 0) 199 result.append('#').append(ref); 200 201 return (result.toString()); 202 } 203 204 public static void setContext(BundleContext context) { 205 BundleResourceHandler.context = context; 206 } 207 208 protected int hashCode(URL url) { 209 int hash = 0; 210 String protocol = url.getProtocol(); 211 if (protocol != null) 212 hash += protocol.hashCode(); 213 214 String host = url.getHost(); 215 if (host != null) 216 hash += host.hashCode(); 217 218 String path = url.getPath(); 219 if (path != null) 220 hash += path.hashCode(); 221 return hash; 222 } 223 224 protected boolean equals(URL url1, URL url2) { 225 return sameFile(url1, url2); 226 } 227 228 protected synchronized InetAddress getHostAddress(URL url) { 229 return null; 230 } 231 232 protected boolean hostsEqual(URL url1, URL url2) { 233 String host1 = url1.getHost(); 234 String host2 = url2.getHost(); 235 if (host1 != null && host2 != null) 236 return host1.equalsIgnoreCase(host2); 237 return (host1 == null && host2 == null); 238 } 239 240 protected boolean sameFile(URL url1, URL url2) { 241 String p1 = url1.getProtocol(); 242 String p2 = url2.getProtocol(); 243 if (!((p1 == p2) || (p1 != null && p1.equalsIgnoreCase(p2)))) 244 return false; 245 246 if (!hostsEqual(url1, url2)) 247 return false; 248 249 if (url1.getPort() != url2.getPort()) 250 return false; 251 252 String a1 = url1.getAuthority(); 253 String a2 = url2.getAuthority(); 254 if (!((a1 == a2) || (a1 != null && a1.equals(a2)))) 255 return false; 256 257 String path1 = url1.getPath(); 258 String path2 = url2.getPath(); 259 if (!((path1 == path2) || (path1 != null && path1.equals(path2)))) 260 return false; 261 262 return true; 263 } 264 265 protected void checkAdminPermission(Bundle bundle) { 266 SecurityManager sm = System.getSecurityManager(); 267 268 if (sm != null) { 269 sm.checkPermission(new AdminPermission(bundle, AdminPermission.RESOURCE)); 270 } 271 } 272 273 protected static BaseClassLoader getBundleClassLoader(AbstractBundle bundle) { 274 BundleLoader loader = bundle.getBundleLoader(); 275 if (loader == null) 276 return null; 277 return (BaseClassLoader) loader.createClassLoader(); 278 } 279 } 280 | Popular Tags |