1 11 12 package org.eclipse.osgi.framework.adaptor.core; 13 14 import java.io.IOException ; 15 import java.net.*; 16 import org.eclipse.osgi.framework.internal.core.AbstractBundle; 17 import org.osgi.framework.AdminPermission; 18 import org.osgi.framework.BundleContext; 19 20 23 24 public abstract class BundleResourceHandler extends URLStreamHandler { 25 public static final String SECURITY_AUTHORIZED = "SECURITY_AUTHORIZED"; protected static BundleContext context; 27 protected BundleEntry bundleEntry; 28 29 30 protected AdminPermission adminPermission; 31 32 35 public BundleResourceHandler() { 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 checkAdminPermission(); 49 if (end < start) 50 return; 51 if (url.getPath() != null) 52 bundleEntry = null; 55 String spec = ""; if (start < end) 57 spec = str.substring(start, end); 58 end -= start; 59 String path = url.getPath(); 61 String bundleId = url.getHost(); 62 int pathIdx = 0; 63 if (spec.startsWith("//")) { int bundleIdIdx = 2; 65 pathIdx = spec.indexOf('/', bundleIdIdx); 66 if (pathIdx == -1) { 67 pathIdx = end; 68 path = ""; } 71 bundleId = spec.substring(bundleIdIdx, pathIdx); 72 } 73 if (pathIdx < end && spec.charAt(pathIdx) == '/') 74 path = spec.substring(pathIdx, end); 75 else if (end > pathIdx) { 76 if (path == null || path.equals("")) path = "/"; int last = path.lastIndexOf('/') + 1; 79 if (last == 0) 80 path = spec.substring(pathIdx, end); 81 else 82 path = path.substring(0, last) + spec.substring(pathIdx, end); 83 } 84 if (path == null) 85 path = ""; int dotIndex; 88 while ((dotIndex = path.indexOf("/./")) >= 0) path = path.substring(0, dotIndex + 1) + path.substring(dotIndex + 3); 90 if (path.endsWith("/.")) path = path.substring(0, path.length() - 1); 92 while ((dotIndex = path.indexOf("/../")) >= 0) { if (dotIndex != 0) 94 path = path.substring(0, path.lastIndexOf('/', dotIndex - 1)) + path.substring(dotIndex + 3); 95 else 96 path = path.substring(dotIndex + 3); 97 } 98 if (path.endsWith("/..") && path.length() > 3) path = path.substring(0, path.length() - 2); 100 101 setURL(url, url.getProtocol(), bundleId, 0, SECURITY_AUTHORIZED, null, path, null, null); 106 } 107 108 118 protected URLConnection openConnection(URL url) throws IOException { 119 String authority = url.getAuthority(); 120 if (!url.getAuthority().equals(SECURITY_AUTHORIZED)) { 124 checkAdminPermission(); 126 } 127 128 if (bundleEntry != null) { 129 return (new BundleURLConnection(url, bundleEntry)); 130 } else { 131 String bidString = url.getHost(); 132 if (bidString == null) { 133 throw new IOException (AdaptorMsg.formatter.getString("URL_NO_BUNDLE_ID", url.toExternalForm())); } 135 AbstractBundle bundle = null; 136 try { 137 Long bundleID = new Long (bidString); 138 bundle = (AbstractBundle) context.getBundle(bundleID.longValue()); 139 } catch (NumberFormatException nfe) { 140 throw new MalformedURLException(AdaptorMsg.formatter.getString("URL_INVALID_BUNDLE_ID", bidString)); } 142 143 if (bundle == null) { 144 throw new IOException (AdaptorMsg.formatter.getString("URL_NO_BUNDLE_FOUND", url.toExternalForm())); } 146 return (new BundleURLConnection(url, findBundleEntry(url, bundle))); 147 } 148 } 149 150 158 abstract protected BundleEntry findBundleEntry(URL url, AbstractBundle bundle) throws IOException ; 159 160 166 protected String toExternalForm(URL url) { 167 StringBuffer result = new StringBuffer (url.getProtocol()); 168 result.append("://"); 170 String bundleId = url.getHost(); 171 if ((bundleId != null) && (bundleId.length() > 0)) { 172 result.append(bundleId); 173 } 174 175 String path = url.getPath(); 176 if (path != null) { 177 if ((path.length() > 0) && (path.charAt(0) != '/')) 178 { 179 result.append("/"); } 181 182 result.append(path); 183 } 184 185 return (result.toString()); 186 } 187 188 public static void setContext(BundleContext context) { 189 BundleResourceHandler.context = context; 190 } 191 192 protected int hashCode(URL url) { 193 int hash = 0; 194 String protocol = url.getProtocol(); 195 if (protocol != null) 196 hash += protocol.hashCode(); 197 198 String host = url.getHost(); 199 if (host != null) 200 hash += host.hashCode(); 201 202 String path = url.getPath(); 203 if (path != null) 204 hash += path.hashCode(); 205 return hash; 206 } 207 208 protected boolean equals(URL url1, URL url2) { 209 return sameFile(url1, url2); 210 } 211 212 protected synchronized InetAddress getHostAddress(URL url) { 213 return null; 214 } 215 216 protected boolean hostsEqual(URL url1, URL url2) { 217 String host1 = url1.getHost(); 218 String host2 = url2.getHost(); 219 if (host1 != null && host2 != null) 220 return host1.equalsIgnoreCase(host2); 221 else 222 return (host1 == null && host2 == null); 223 } 224 225 protected boolean sameFile(URL url1, URL url2) { 226 String p1 = url1.getProtocol(); 227 String p2 = url2.getProtocol(); 228 if (!((p1 == p2) || (p1 != null && p1.equalsIgnoreCase(p2)))) 229 return false; 230 231 if (!hostsEqual(url1, url2)) 232 return false; 233 234 String a1 = url1.getAuthority(); 235 String a2 = url2.getAuthority(); 236 if (!((a1 == a2) || (a1 != null && a1.equals(a2)))) 237 return false; 238 239 String path1 = url1.getPath(); 240 String path2 = url2.getPath(); 241 if (!((path1 == path2) || (path1 != null && path1.equals(path2)))) 242 return false; 243 244 return true; 245 } 246 247 protected void checkAdminPermission() { 248 SecurityManager sm = System.getSecurityManager(); 249 250 if (sm != null) { 251 if (adminPermission == null) { 252 adminPermission = new AdminPermission(); 253 } 254 255 sm.checkPermission(adminPermission); 256 } 257 } 258 } | Popular Tags |