1 9 package org.eclipse.osgi.framework.internal.protocol; 10 11 import java.io.IOException ; 12 import java.lang.reflect.Field ; 13 import java.lang.reflect.Method ; 14 import java.net.*; 15 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 16 17 public class MultiplexingURLStreamHandler extends URLStreamHandler { 18 private static Method openConnectionMethod; 19 private static Method equalsMethod; 20 private static Method getDefaultPortMethod; 21 private static Method getHostAddressMethod; 22 private static Method hashCodeMethod; 23 private static Method hostsEqualMethod; 24 private static Method parseURLMethod; 25 private static Method sameFileMethod; 26 private static Method setURLMethod; 27 private static Method toExternalFormMethod; 28 private static Field handlerField; 29 private static boolean methodsInitialized = false; 30 31 private String protocol; 32 private StreamHandlerFactory factory; 33 34 private static synchronized void initializeMethods(StreamHandlerFactory factory) { 35 if (methodsInitialized) 36 return; 37 try { 38 openConnectionMethod = URLStreamHandler.class.getDeclaredMethod("openConnection", new Class [] {URL.class}); openConnectionMethod.setAccessible(true); 40 41 equalsMethod = URLStreamHandler.class.getDeclaredMethod("equals", new Class [] {URL.class, URL.class}); equalsMethod.setAccessible(true); 43 44 getDefaultPortMethod = URLStreamHandler.class.getDeclaredMethod("getDefaultPort", null); getDefaultPortMethod.setAccessible(true); 46 47 getHostAddressMethod = URLStreamHandler.class.getDeclaredMethod("getHostAddress", new Class [] {URL.class}); getHostAddressMethod.setAccessible(true); 49 50 hashCodeMethod = URLStreamHandler.class.getDeclaredMethod("hashCode", new Class [] {URL.class}); hashCodeMethod.setAccessible(true); 52 53 hostsEqualMethod = URLStreamHandler.class.getDeclaredMethod("hostsEqual", new Class [] {URL.class, URL.class}); hostsEqualMethod.setAccessible(true); 55 56 parseURLMethod = URLStreamHandler.class.getDeclaredMethod("parseURL", new Class [] {URL.class, String .class, Integer.TYPE, Integer.TYPE}); parseURLMethod.setAccessible(true); 58 59 sameFileMethod = URLStreamHandler.class.getDeclaredMethod("sameFile", new Class [] {URL.class, URL.class}); sameFileMethod.setAccessible(true); 61 62 setURLMethod = URLStreamHandler.class.getDeclaredMethod("setURL", new Class [] {URL.class, String .class, String .class, Integer.TYPE, String .class, String .class, String .class, String .class, String .class}); setURLMethod.setAccessible(true); 64 65 toExternalFormMethod = URLStreamHandler.class.getDeclaredMethod("toExternalForm", new Class [] {URL.class}); toExternalFormMethod.setAccessible(true); 67 68 handlerField = URL.class.getDeclaredField("handler"); handlerField.setAccessible(true); 70 } catch (Exception e) { 71 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "initializeMethods", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 73 } 74 methodsInitialized = true; 75 } 76 77 public MultiplexingURLStreamHandler(String protocol, StreamHandlerFactory factory) { 78 this.protocol = protocol; 79 this.factory = factory; 80 initializeMethods(factory); 81 } 82 83 protected URLConnection openConnection(URL url) throws IOException { 84 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 85 if (handler != null) { 86 try { 87 return (URLConnection) openConnectionMethod.invoke(handler, new Object [] {url}); 88 } catch (Exception e) { 89 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "openConnection", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 91 } 92 } 93 throw new MalformedURLException(); 94 } 95 96 protected boolean equals(URL url1, URL url2) { 97 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 98 if (handler != null) { 99 try { 100 return ((Boolean ) equalsMethod.invoke(handler, new Object [] {url1, url2})).booleanValue(); 101 } catch (Exception e) { 102 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "equals", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 104 } 105 } 106 throw new IllegalStateException (); 107 } 108 109 protected int getDefaultPort() { 110 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 111 if (handler != null) { 112 try { 113 return ((Integer ) getDefaultPortMethod.invoke(handler, null)).intValue(); 114 } catch (Exception e) { 115 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "getDefaultPort", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 117 } 118 } 119 throw new IllegalStateException (); 120 } 121 122 protected InetAddress getHostAddress(URL url) { 123 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 124 if (handler != null) { 125 try { 126 return (InetAddress) getHostAddressMethod.invoke(handler, new Object [] {url}); 127 } catch (Exception e) { 128 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hashCode", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 130 } 131 } 132 throw new IllegalStateException (); 133 } 134 135 protected int hashCode(URL url) { 136 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 137 if (handler != null) { 138 try { 139 return ((Integer ) hashCodeMethod.invoke(handler, new Object [] {url})).intValue(); 140 } catch (Exception e) { 141 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hashCode", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 143 } 144 } 145 throw new IllegalStateException (); 146 } 147 148 protected boolean hostsEqual(URL url1, URL url2) { 149 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 150 if (handler != null) { 151 try { 152 return ((Boolean ) hostsEqualMethod.invoke(handler, new Object [] {url1, url2})).booleanValue(); 153 } catch (Exception e) { 154 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hostsEqual", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 156 } 157 } 158 throw new IllegalStateException (); 159 } 160 161 protected void parseURL(URL arg0, String arg1, int arg2, int arg3) { 162 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 163 if (handler != null) { 164 try { 165 handlerField.set(arg0, handler); 168 parseURLMethod.invoke(handler, new Object [] {arg0, arg1, new Integer (arg2), new Integer (arg3)}); 169 handlerField.set(arg0, this); 170 return; 171 } catch (Exception e) { 172 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "parseURL", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 174 } 175 } 176 throw new IllegalStateException (); 177 } 178 179 protected boolean sameFile(URL url1, URL url2) { 180 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 181 if (handler != null) { 182 try { 183 return ((Boolean ) sameFileMethod.invoke(handler, new Object [] {url1, url2})).booleanValue(); 184 } catch (Exception e) { 185 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "sameFile", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 187 } 188 } 189 throw new IllegalStateException (); 190 } 191 192 protected void setURL(URL arg0, String arg1, String arg2, int arg3, String arg4, String arg5, String arg6, String arg7, String arg8) { 193 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 194 if (handler != null) { 195 try { 196 handlerField.set(arg0, handler); 199 setURLMethod.invoke(handler, new Object [] {arg0, arg1, arg2, new Integer (arg3), arg4, arg5, arg6, arg7, arg8}); 200 handlerField.set(arg0, this); 201 return; 202 } catch (Exception e) { 203 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "setURL", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 205 } 206 } 207 throw new IllegalStateException (); 208 } 209 210 protected String toExternalForm(URL url) { 211 URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); 212 if (handler != null) { 213 try { 214 return (String ) toExternalFormMethod.invoke(handler, new Object [] {url}); 215 } catch (Exception e) { 216 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "toExternalForm", FrameworkLogEntry.ERROR, e, null)); throw new RuntimeException (e.getMessage()); 218 } 219 } 220 throw new IllegalStateException (); 221 } 222 223 } 224 | Popular Tags |