KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.protocol;
13
14 import java.io.IOException JavaDoc;
15 import java.net.*;
16 import org.osgi.framework.*;
17 import org.osgi.service.url.URLConstants;
18 import org.osgi.service.url.URLStreamHandlerService;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.osgi.util.tracker.ServiceTrackerCustomizer;
21
22 /**
23  * The URLStreamHandlerProxy is a URLStreamHandler that acts as a proxy for registered
24  * URLStreamHandlerServices. When a URLStreamHandler is requested from the URLStreamHandlerFactory
25  * and it exists in the service registry, a URLStreamHandlerProxy is created which will pass all the
26  * requests from the requestor to the real URLStreamHandlerService. We can't return the real
27  * URLStreamHandlerService from the URLStreamHandlerFactory because the JVM caches URLStreamHandlers
28  * and therefore would not support a dynamic environment of URLStreamHandlerServices being registered
29  * and unregistered.
30  */

31
32 public class URLStreamHandlerProxy extends URLStreamHandler implements ServiceTrackerCustomizer {
33     // TODO lots of type-based names
34
protected URLStreamHandlerService realHandlerService;
35
36     protected URLStreamHandlerSetter urlSetter;
37
38     protected ServiceTracker urlStreamHandlerServiceTracker;
39
40     protected BundleContext context;
41     protected ServiceReference urlStreamServiceReference;
42
43     protected String JavaDoc protocol;
44
45     protected int ranking = Integer.MIN_VALUE;
46
47     public URLStreamHandlerProxy(String JavaDoc protocol, ServiceReference reference, BundleContext context) {
48         this.context = context;
49         this.protocol = protocol;
50
51         urlSetter = new URLStreamHandlerSetter(this);
52
53         //set the handler and ranking
54
setNewHandler(reference, getRank(reference));
55
56         urlStreamHandlerServiceTracker = new ServiceTracker(context, StreamHandlerFactory.URLSTREAMHANDLERCLASS, this);
57         StreamHandlerFactory.secureAction.open(urlStreamHandlerServiceTracker);
58     }
59
60     private void setNewHandler(ServiceReference reference, int rank) {
61         if (urlStreamServiceReference != null)
62             context.ungetService(urlStreamServiceReference);
63
64         urlStreamServiceReference = reference;
65         ranking = rank;
66
67         if (reference == null)
68             realHandlerService = new NullURLStreamHandlerService();
69         else
70             realHandlerService = (URLStreamHandlerService) StreamHandlerFactory.secureAction.getService(reference, context);
71     }
72
73     /**
74      * @see java.net.URLStreamHandler#equals(URL, URL)
75      */

76     protected boolean equals(URL url1, URL url2) {
77         return realHandlerService.equals(url1, url2);
78     }
79
80     /**
81      * @see java.net.URLStreamHandler#getDefaultPort()
82      */

83     protected int getDefaultPort() {
84         return realHandlerService.getDefaultPort();
85     }
86
87     /**
88      * @see java.net.URLStreamHandler#getHostAddress(URL)
89      */

90     protected InetAddress getHostAddress(URL url) {
91         return realHandlerService.getHostAddress(url);
92     }
93
94     /**
95      * @see java.net.URLStreamHandler#hashCode(URL)
96      */

97     protected int hashCode(URL url) {
98         return realHandlerService.hashCode(url);
99     }
100
101     /**
102      * @see java.net.URLStreamHandler#hostsEqual(URL, URL)
103      */

104     protected boolean hostsEqual(URL url1, URL url2) {
105         return realHandlerService.hostsEqual(url1, url2);
106     }
107
108     /**
109      * @see java.net.URLStreamHandler#openConnection(URL)
110      */

111     protected URLConnection openConnection(URL url) throws IOException JavaDoc {
112         return realHandlerService.openConnection(url);
113     }
114
115     /**
116      * @see java.net.URLStreamHandler#parseURL(URL, String, int, int)
117      */

118     protected void parseURL(URL url, String JavaDoc str, int start, int end) {
119         realHandlerService.parseURL(urlSetter, url, str, start, end);
120     }
121
122     /**
123      * @see java.net.URLStreamHandler#sameFile(URL, URL)
124      */

125     protected boolean sameFile(URL url1, URL url2) {
126         return realHandlerService.sameFile(url1, url2);
127     }
128
129     /**
130      * @see java.net.URLStreamHandler#toExternalForm(URL)
131      */

132     protected String JavaDoc toExternalForm(URL url) {
133         return realHandlerService.toExternalForm(url);
134     }
135
136     /**
137      * @see java.net.URLStreamHandler#setURL(URL, String, String, int, String, String, String, String, String)
138      */

139     public void setURL(URL u, String JavaDoc protocol, String JavaDoc host, int port, String JavaDoc authority, String JavaDoc userInfo, String JavaDoc file, String JavaDoc query, String JavaDoc ref) {
140         super.setURL(u, protocol, host, port, authority, userInfo, file, query, ref);
141     }
142
143
144     public void setURL(URL url, String JavaDoc protocol, String JavaDoc host, int port, String JavaDoc file, String JavaDoc ref) {
145
146         //using non-deprecated URLStreamHandler.setURL method.
147
//setURL(URL u, String protocol, String host, int port, String authority, String userInfo, String file, String query, String ref)
148
super.setURL(url, protocol, host, port, null, null, file, null, ref);
149     }
150
151     /**
152      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(ServiceReference)
153      */

154     public Object JavaDoc addingService(ServiceReference reference) {
155         //check to see if our protocol is being registered by another service
156
Object JavaDoc prop = reference.getProperty(URLConstants.URL_HANDLER_PROTOCOL);
157         if (!(prop instanceof String JavaDoc[]))
158             return null;
159         String JavaDoc[] protocols = (String JavaDoc[]) prop;
160         for (int i = 0; i < protocols.length; i++) {
161             if (protocols[i].equals(protocol)) {
162                 //If our protocol is registered by another service, check the service ranking and switch URLStreamHandlers if nessecary.
163
int newServiceRanking = getRank(reference);
164                 if (newServiceRanking > ranking || urlStreamServiceReference == null)
165                     setNewHandler(reference, newServiceRanking);
166                 return (reference);
167             }
168         }
169
170         //we don't want to continue hearing events about a URLStreamHandlerService not registered under our protocol
171
return (null);
172     }
173
174     /**
175      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(ServiceReference, Object)
176      */

177     // check to see if the ranking has changed. If so, re-select a new URLHandler
178
public void modifiedService(ServiceReference reference, Object JavaDoc service) {
179         int newRank = getRank(reference);
180         if (reference == urlStreamServiceReference) {
181             if (newRank < ranking) {
182                 // The URLHandler we are currently using has dropped it's ranking below a URLHandler registered
183
// for the same protocol. We need to swap out URLHandlers.
184
// this should get us the highest ranked service, if available
185
ServiceReference newReference = urlStreamHandlerServiceTracker.getServiceReference();
186                 if (newReference != urlStreamServiceReference && newReference != null) {
187                     setNewHandler(newReference, ((Integer JavaDoc) newReference.getProperty(Constants.SERVICE_RANKING)).intValue());
188                 }
189             }
190         } else if (newRank > ranking) {
191             // the service changed is another URLHandler that we are not currently using
192
// If it's ranking is higher, we must swap it in.
193
setNewHandler(reference, newRank);
194         }
195     }
196
197     /**
198      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(ServiceReference, Object)
199      */

200     public void removedService(ServiceReference reference, Object JavaDoc service) {
201         // check to see if our URLStreamHandler was unregistered.
202
if (reference != urlStreamServiceReference)
203             return;
204         // If so, look for a lower ranking URLHandler
205
// this should get us the highest ranking service left, if available
206
ServiceReference newReference = urlStreamHandlerServiceTracker.getServiceReference();
207         // if newReference == null then we will use the NullURLStreamHandlerService here
208
setNewHandler(newReference, getRank(newReference));
209     }
210
211     private int getRank(ServiceReference reference) {
212         if (reference == null)
213             return Integer.MIN_VALUE;
214         Object JavaDoc property = reference.getProperty(Constants.SERVICE_RANKING);
215         return (property instanceof Integer JavaDoc) ? ((Integer JavaDoc) property).intValue() : 0;
216     }
217
218 }
219
Popular Tags