KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > protocol > MultiplexingURLStreamHandler


1 /*******************************************************************************
2  * Copyright (c) 2006 Cognos Incorporated, IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  *******************************************************************************/

9 package org.eclipse.osgi.framework.internal.protocol;
10
11 import java.io.IOException JavaDoc;
12 import java.lang.reflect.Field JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.net.*;
15 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
16
17 public class MultiplexingURLStreamHandler extends URLStreamHandler {
18     private static Method JavaDoc openConnectionMethod;
19     private static Method JavaDoc equalsMethod;
20     private static Method JavaDoc getDefaultPortMethod;
21     private static Method JavaDoc getHostAddressMethod;
22     private static Method JavaDoc hashCodeMethod;
23     private static Method JavaDoc hostsEqualMethod;
24     private static Method JavaDoc parseURLMethod;
25     private static Method JavaDoc sameFileMethod;
26     private static Method JavaDoc setURLMethod;
27     private static Method JavaDoc toExternalFormMethod;
28     private static Field JavaDoc handlerField;
29     private static boolean methodsInitialized = false;
30
31     private String JavaDoc 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 JavaDoc[] {URL.class}); //$NON-NLS-1$
39
openConnectionMethod.setAccessible(true);
40
41             equalsMethod = URLStreamHandler.class.getDeclaredMethod("equals", new Class JavaDoc[] {URL.class, URL.class}); //$NON-NLS-1$
42
equalsMethod.setAccessible(true);
43
44             getDefaultPortMethod = URLStreamHandler.class.getDeclaredMethod("getDefaultPort", null); //$NON-NLS-1$
45
getDefaultPortMethod.setAccessible(true);
46
47             getHostAddressMethod = URLStreamHandler.class.getDeclaredMethod("getHostAddress", new Class JavaDoc[] {URL.class}); //$NON-NLS-1$
48
getHostAddressMethod.setAccessible(true);
49
50             hashCodeMethod = URLStreamHandler.class.getDeclaredMethod("hashCode", new Class JavaDoc[] {URL.class}); //$NON-NLS-1$
51
hashCodeMethod.setAccessible(true);
52
53             hostsEqualMethod = URLStreamHandler.class.getDeclaredMethod("hostsEqual", new Class JavaDoc[] {URL.class, URL.class}); //$NON-NLS-1$
54
hostsEqualMethod.setAccessible(true);
55
56             parseURLMethod = URLStreamHandler.class.getDeclaredMethod("parseURL", new Class JavaDoc[] {URL.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE}); //$NON-NLS-1$
57
parseURLMethod.setAccessible(true);
58
59             sameFileMethod = URLStreamHandler.class.getDeclaredMethod("sameFile", new Class JavaDoc[] {URL.class, URL.class}); //$NON-NLS-1$
60
sameFileMethod.setAccessible(true);
61
62             setURLMethod = URLStreamHandler.class.getDeclaredMethod("setURL", new Class JavaDoc[] {URL.class, String JavaDoc.class, String JavaDoc.class, Integer.TYPE, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class}); //$NON-NLS-1$
63
setURLMethod.setAccessible(true);
64
65             toExternalFormMethod = URLStreamHandler.class.getDeclaredMethod("toExternalForm", new Class JavaDoc[] {URL.class}); //$NON-NLS-1$
66
toExternalFormMethod.setAccessible(true);
67
68             handlerField = URL.class.getDeclaredField("handler"); //$NON-NLS-1$
69
handlerField.setAccessible(true);
70         } catch (Exception JavaDoc e) {
71             factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "initializeMethods", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
72
throw new RuntimeException JavaDoc(e.getMessage());
73         }
74         methodsInitialized = true;
75     }
76
77     public MultiplexingURLStreamHandler(String JavaDoc protocol, StreamHandlerFactory factory) {
78         this.protocol = protocol;
79         this.factory = factory;
80         initializeMethods(factory);
81     }
82
83     protected URLConnection openConnection(URL url) throws IOException JavaDoc {
84         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
85         if (handler != null) {
86             try {
87                 return (URLConnection) openConnectionMethod.invoke(handler, new Object JavaDoc[] {url});
88             } catch (Exception JavaDoc e) {
89                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "openConnection", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
90
throw new RuntimeException JavaDoc(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 JavaDoc) equalsMethod.invoke(handler, new Object JavaDoc[] {url1, url2})).booleanValue();
101             } catch (Exception JavaDoc e) {
102                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "equals", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
103
throw new RuntimeException JavaDoc(e.getMessage());
104             }
105         }
106         throw new IllegalStateException JavaDoc();
107     }
108
109     protected int getDefaultPort() {
110         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
111         if (handler != null) {
112             try {
113                 return ((Integer JavaDoc) getDefaultPortMethod.invoke(handler, null)).intValue();
114             } catch (Exception JavaDoc e) {
115                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "getDefaultPort", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
116
throw new RuntimeException JavaDoc(e.getMessage());
117             }
118         }
119         throw new IllegalStateException JavaDoc();
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 JavaDoc[] {url});
127             } catch (Exception JavaDoc e) {
128                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hashCode", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
129
throw new RuntimeException JavaDoc(e.getMessage());
130             }
131         }
132         throw new IllegalStateException JavaDoc();
133     }
134
135     protected int hashCode(URL url) {
136         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
137         if (handler != null) {
138             try {
139                 return ((Integer JavaDoc) hashCodeMethod.invoke(handler, new Object JavaDoc[] {url})).intValue();
140             } catch (Exception JavaDoc e) {
141                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hashCode", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
142
throw new RuntimeException JavaDoc(e.getMessage());
143             }
144         }
145         throw new IllegalStateException JavaDoc();
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 JavaDoc) hostsEqualMethod.invoke(handler, new Object JavaDoc[] {url1, url2})).booleanValue();
153             } catch (Exception JavaDoc e) {
154                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "hostsEqual", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
155
throw new RuntimeException JavaDoc(e.getMessage());
156             }
157         }
158         throw new IllegalStateException JavaDoc();
159     }
160
161     protected void parseURL(URL arg0, String JavaDoc arg1, int arg2, int arg3) {
162         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
163         if (handler != null) {
164             try {
165                 // the handler is set because of a check in setURL (called internally in parseURL)
166
// to ensure that this handler and the URL's handler are the same.
167
handlerField.set(arg0, handler);
168                 parseURLMethod.invoke(handler, new Object JavaDoc[] {arg0, arg1, new Integer JavaDoc(arg2), new Integer JavaDoc(arg3)});
169                 handlerField.set(arg0, this);
170                 return;
171             } catch (Exception JavaDoc e) {
172                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "parseURL", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
173
throw new RuntimeException JavaDoc(e.getMessage());
174             }
175         }
176         throw new IllegalStateException JavaDoc();
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 JavaDoc) sameFileMethod.invoke(handler, new Object JavaDoc[] {url1, url2})).booleanValue();
184             } catch (Exception JavaDoc e) {
185                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "sameFile", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
186
throw new RuntimeException JavaDoc(e.getMessage());
187             }
188         }
189         throw new IllegalStateException JavaDoc();
190     }
191
192     protected void setURL(URL arg0, String JavaDoc arg1, String JavaDoc arg2, int arg3, String JavaDoc arg4, String JavaDoc arg5, String JavaDoc arg6, String JavaDoc arg7, String JavaDoc arg8) {
193         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
194         if (handler != null) {
195             try {
196                 // the handler is set because of a check in setURL to ensure that this handler
197
// and the URL's handler are the same.
198
handlerField.set(arg0, handler);
199                 setURLMethod.invoke(handler, new Object JavaDoc[] {arg0, arg1, arg2, new Integer JavaDoc(arg3), arg4, arg5, arg6, arg7, arg8});
200                 handlerField.set(arg0, this);
201                 return;
202             } catch (Exception JavaDoc e) {
203                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "setURL", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
204
throw new RuntimeException JavaDoc(e.getMessage());
205             }
206         }
207         throw new IllegalStateException JavaDoc();
208     }
209
210     protected String JavaDoc toExternalForm(URL url) {
211         URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol);
212         if (handler != null) {
213             try {
214                 return (String JavaDoc) toExternalFormMethod.invoke(handler, new Object JavaDoc[] {url});
215             } catch (Exception JavaDoc e) {
216                 factory.adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingURLStreamHandler.class.getName(), "toExternalForm", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
217
throw new RuntimeException JavaDoc(e.getMessage());
218             }
219         }
220         throw new IllegalStateException JavaDoc();
221     }
222
223 }
224
Popular Tags