KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > boot > PlatformURLHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.core.internal.boot;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.net.*;
16 import java.util.Hashtable JavaDoc;
17 import org.eclipse.core.internal.runtime.CommonMessages;
18 import org.eclipse.osgi.util.NLS;
19 import org.osgi.service.url.AbstractURLStreamHandlerService;
20
21 /**
22  * URL handler for the "platform" protocol
23  */

24 public class PlatformURLHandler extends AbstractURLStreamHandlerService {
25
26     private static Hashtable JavaDoc connectionType = new Hashtable JavaDoc();
27
28     // URL protocol designations
29
public static final String JavaDoc PROTOCOL = "platform"; //$NON-NLS-1$
30
public static final String JavaDoc FILE = "file"; //$NON-NLS-1$
31
public static final String JavaDoc JAR = "jar"; //$NON-NLS-1$
32
public static final String JavaDoc BUNDLE = "bundle"; //$NON-NLS-1$
33
public static final String JavaDoc JAR_SEPARATOR = "!/"; //$NON-NLS-1$
34
public static final String JavaDoc PROTOCOL_SEPARATOR = ":"; //$NON-NLS-1$
35

36     /*
37      * Constructor for the class.
38      */

39     public PlatformURLHandler() {
40         super();
41     }
42
43     /* (non-Javadoc)
44      * @see org.osgi.service.url.AbstractURLStreamHandlerService#openConnection(java.net.URL)
45      */

46     public URLConnection openConnection(URL url) throws IOException JavaDoc {
47         // Note: openConnection() method is made public (rather than protected)
48
// to enable request delegation from proxy handlers
49
String JavaDoc spec = url.getFile().trim();
50         if (spec.startsWith("/")) //$NON-NLS-1$
51
spec = spec.substring(1);
52         int ix = spec.indexOf("/"); //$NON-NLS-1$
53
if (ix == -1)
54             throw new MalformedURLException(NLS.bind(CommonMessages.url_invalidURL, url.toExternalForm()));
55
56         String JavaDoc type = spec.substring(0, ix);
57         Constructor JavaDoc construct = (Constructor JavaDoc) 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 JavaDoc[] {url});
64         } catch (Exception JavaDoc e) {
65             throw new IOException JavaDoc(NLS.bind(CommonMessages.url_createConnection, e.getMessage()));
66         }
67         connection.setResolvedURL(connection.resolve());
68         return connection;
69     }
70
71     public static void register(String JavaDoc type, Class JavaDoc connectionClass) {
72         try {
73             Constructor JavaDoc c = connectionClass.getConstructor(new Class JavaDoc[] {URL.class});
74             connectionType.put(type, c);
75         } catch (NoSuchMethodException JavaDoc e) {
76             //don't register connection classes that don't conform to the spec
77
}
78     }
79     
80     public static void unregister(String JavaDoc type) {
81         connectionType.remove(type);
82     }
83 }
84
Popular Tags