KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > adaptor > core > BundleResourceHandler


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.io.IOException JavaDoc;
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 /**
21  * URLStreamHandler the bundleentry and bundleresource protocols.
22  */

23
24 public abstract class BundleResourceHandler extends URLStreamHandler {
25     public static final String JavaDoc SECURITY_AUTHORIZED = "SECURITY_AUTHORIZED"; //$NON-NLS-1$
26
protected static BundleContext context;
27     protected BundleEntry bundleEntry;
28
29     /** Single object for permission checks */
30     protected AdminPermission adminPermission;
31
32     /**
33      * Constructor for a bundle protocol resource URLStreamHandler.
34      */

35     public BundleResourceHandler() {
36     }
37
38     public BundleResourceHandler(BundleEntry bundleEntry) {
39         this.bundleEntry = bundleEntry;
40     }
41
42     /**
43      * Parse reference URL.
44      */

45     protected void parseURL(URL url, String JavaDoc str, int start, int end) {
46         // Check the permission of the caller to see if they
47
// are allowed access to the resource.
48
checkAdminPermission();
49         if (end < start)
50             return;
51         if (url.getPath() != null)
52             // A call to a URL constructor has been made that uses an authorized URL as its context.
53
// Null out bundleEntry because it will not be valid for the new path
54
bundleEntry = null;
55         String JavaDoc spec = ""; //$NON-NLS-1$
56
if (start < end)
57             spec = str.substring(start, end);
58         end -= start;
59         //Default is to use path and bundleId from context
60
String JavaDoc path = url.getPath();
61         String JavaDoc bundleId = url.getHost();
62         int pathIdx = 0;
63         if (spec.startsWith("//")) { //$NON-NLS-1$
64
int bundleIdIdx = 2;
65             pathIdx = spec.indexOf('/', bundleIdIdx);
66             if (pathIdx == -1) {
67                 pathIdx = end;
68                 // Use default
69
path = ""; //$NON-NLS-1$
70
}
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("")) //$NON-NLS-1$
77
path = "/"; //$NON-NLS-1$
78
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 = ""; //$NON-NLS-1$
86
//modify path if there's any relative references
87
int dotIndex;
88         while ((dotIndex = path.indexOf("/./")) >= 0) //$NON-NLS-1$
89
path = path.substring(0, dotIndex + 1) + path.substring(dotIndex + 3);
90         if (path.endsWith("/.")) //$NON-NLS-1$
91
path = path.substring(0, path.length() - 1);
92         while ((dotIndex = path.indexOf("/../")) >= 0) { //$NON-NLS-1$
93
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) //$NON-NLS-1$
99
path = path.substring(0, path.length() - 2);
100
101         // Setting the authority portion of the URL to SECURITY_ATHORIZED
102
// ensures that this URL was created by using this parseURL
103
// method. The openConnection method will only open URLs
104
// that have the authority set to this.
105
setURL(url, url.getProtocol(), bundleId, 0, SECURITY_AUTHORIZED, null, path, null, null);
106     }
107
108     /**
109      * Establishes a connection to the resource specified by <code>URL</code>.
110      * Since different protocols may have unique ways of connecting, it must be
111      * overridden by the subclass.
112      *
113      * @return java.net.URLConnection
114      * @param url java.net.URL
115      *
116      * @exception IOException thrown if an IO error occurs during connection establishment
117      */

118     protected URLConnection openConnection(URL url) throws IOException JavaDoc {
119         String JavaDoc authority = url.getAuthority();
120         // check to make sure that this URL was created using the
121
// parseURL method. This ensures the security check was done
122
// at URL construction.
123
if (!url.getAuthority().equals(SECURITY_AUTHORIZED)) {
124             // No admin security check was made better check now.
125
checkAdminPermission();
126         }
127
128         if (bundleEntry != null) {
129             return (new BundleURLConnection(url, bundleEntry));
130         } else {
131             String JavaDoc bidString = url.getHost();
132             if (bidString == null) {
133                 throw new IOException JavaDoc(AdaptorMsg.formatter.getString("URL_NO_BUNDLE_ID", url.toExternalForm())); //$NON-NLS-1$
134
}
135             AbstractBundle bundle = null;
136             try {
137                 Long JavaDoc bundleID = new Long JavaDoc(bidString);
138                 bundle = (AbstractBundle) context.getBundle(bundleID.longValue());
139             } catch (NumberFormatException JavaDoc nfe) {
140                 throw new MalformedURLException(AdaptorMsg.formatter.getString("URL_INVALID_BUNDLE_ID", bidString)); //$NON-NLS-1$
141
}
142
143             if (bundle == null) {
144                 throw new IOException JavaDoc(AdaptorMsg.formatter.getString("URL_NO_BUNDLE_FOUND", url.toExternalForm())); //$NON-NLS-1$
145
}
146             return (new BundleURLConnection(url, findBundleEntry(url, bundle)));
147         }
148     }
149
150     /**
151      * Finds the bundle entry for this protocal. This is handled
152      * differently for Bundle.gerResource() and Bundle.getEntry()
153      * because getResource uses the bundle classloader and getEntry
154      * only used the base bundle file.
155      * @param url The URL to find the BundleEntry for.
156      * @return
157      */

158     abstract protected BundleEntry findBundleEntry(URL url, AbstractBundle bundle) throws IOException JavaDoc;
159
160     /**
161      * Converts a bundle URL to a String.
162      *
163      * @param url the URL.
164      * @return a string representation of the URL.
165      */

166     protected String JavaDoc toExternalForm(URL url) {
167         StringBuffer JavaDoc result = new StringBuffer JavaDoc(url.getProtocol());
168         result.append("://"); //$NON-NLS-1$
169

170         String JavaDoc bundleId = url.getHost();
171         if ((bundleId != null) && (bundleId.length() > 0)) {
172             result.append(bundleId);
173         }
174
175         String JavaDoc path = url.getPath();
176         if (path != null) {
177             if ((path.length() > 0) && (path.charAt(0) != '/')) /* if name doesn't have a leading slash */
178             {
179                 result.append("/"); //$NON-NLS-1$
180
}
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 JavaDoc protocol = url.getProtocol();
195         if (protocol != null)
196             hash += protocol.hashCode();
197
198         String JavaDoc host = url.getHost();
199         if (host != null)
200             hash += host.hashCode();
201
202         String JavaDoc 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 JavaDoc host1 = url1.getHost();
218         String JavaDoc 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 JavaDoc p1 = url1.getProtocol();
227         String JavaDoc 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 JavaDoc a1 = url1.getAuthority();
235         String JavaDoc a2 = url2.getAuthority();
236         if (!((a1 == a2) || (a1 != null && a1.equals(a2))))
237             return false;
238
239         String JavaDoc path1 = url1.getPath();
240         String JavaDoc 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 JavaDoc 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