1 11 package org.eclipse.core.internal.boot; 12 13 import java.io.IOException ; 14 import java.lang.reflect.Constructor ; 15 import java.net.*; 16 import java.util.Hashtable ; 17 import org.eclipse.core.internal.runtime.CommonMessages; 18 import org.eclipse.osgi.util.NLS; 19 import org.osgi.service.url.AbstractURLStreamHandlerService; 20 21 24 public class PlatformURLHandler extends AbstractURLStreamHandlerService { 25 26 private static Hashtable connectionType = new Hashtable (); 27 28 public static final String PROTOCOL = "platform"; public static final String FILE = "file"; public static final String JAR = "jar"; public static final String BUNDLE = "bundle"; public static final String JAR_SEPARATOR = "!/"; public static final String PROTOCOL_SEPARATOR = ":"; 36 39 public PlatformURLHandler() { 40 super(); 41 } 42 43 46 public URLConnection openConnection(URL url) throws IOException { 47 String spec = url.getFile().trim(); 50 if (spec.startsWith("/")) spec = spec.substring(1); 52 int ix = spec.indexOf("/"); if (ix == -1) 54 throw new MalformedURLException(NLS.bind(CommonMessages.url_invalidURL, url.toExternalForm())); 55 56 String type = spec.substring(0, ix); 57 Constructor construct = (Constructor ) connectionType.get(type); 58 if (construct == null) 59 throw new MalformedURLException(NLS.bind(CommonMessages.url_badVariant, type)); 60 61 PlatformURLConnection connection = null; 62 try { 63 connection = (PlatformURLConnection) construct.newInstance(new Object [] {url}); 64 } catch (Exception e) { 65 throw new IOException (NLS.bind(CommonMessages.url_createConnection, e.getMessage())); 66 } 67 connection.setResolvedURL(connection.resolve()); 68 return connection; 69 } 70 71 public static void register(String type, Class connectionClass) { 72 try { 73 Constructor c = connectionClass.getConstructor(new Class [] {URL.class}); 74 connectionType.put(type, c); 75 } catch (NoSuchMethodException e) { 76 } 78 } 79 80 public static void unregister(String type) { 81 connectionType.remove(type); 82 } 83 } 84 | Popular Tags |