KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > PlatformURLPluginConnection


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.runtime;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import org.eclipse.core.internal.boot.PlatformURLConnection;
16 import org.eclipse.core.internal.boot.PlatformURLHandler;
17 import org.eclipse.osgi.util.NLS;
18 import org.osgi.framework.Bundle;
19
20 /**
21  * Platform URL support
22  * platform:/plugin/pluginId/ maps to pluginDescriptor.getInstallURLInternal()
23  */

24 public class PlatformURLPluginConnection extends PlatformURLConnection {
25
26     private Bundle target = null;
27     private static boolean isRegistered = false;
28     public static final String JavaDoc PLUGIN = "plugin"; //$NON-NLS-1$
29

30     /*
31      * Constructor for the class.
32      */

33     public PlatformURLPluginConnection(URL JavaDoc url) {
34         super(url);
35     }
36
37     /* (non-Javadoc)
38      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#allowCaching()
39      */

40     protected boolean allowCaching() {
41         return true;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#resolve()
46      */

47     protected URL JavaDoc resolve() throws IOException JavaDoc {
48         String JavaDoc spec = url.getFile().trim();
49         if (spec.startsWith("/")) //$NON-NLS-1$
50
spec = spec.substring(1);
51         if (!spec.startsWith(PLUGIN))
52             throw new IOException JavaDoc(NLS.bind(CommonMessages.url_badVariant, url));
53         int ix = spec.indexOf("/", PLUGIN.length() + 1); //$NON-NLS-1$
54
String JavaDoc ref = ix == -1 ? spec.substring(PLUGIN.length() + 1) : spec.substring(PLUGIN.length() + 1, ix);
55         String JavaDoc id = getId(ref);
56         Activator activator = Activator.getDefault();
57         if (activator == null)
58             throw new IOException JavaDoc(CommonMessages.activator_not_available);
59         target = activator.getBundle(id);
60         if (target == null)
61             throw new IOException JavaDoc(NLS.bind(CommonMessages.url_resolvePlugin, url));
62         if (ix == -1 || (ix + 1) >= spec.length())
63             return target.getEntry("/"); //$NON-NLS-1$
64
URL JavaDoc result = target.getEntry(spec.substring(ix + 1));
65         if (result != null)
66             return result;
67         // if the result is null then force the creation of a URL that will throw FileNotFoundExceptions
68
return new URL JavaDoc(target.getEntry("/"), spec.substring(ix + 1)); //$NON-NLS-1$
69

70     }
71
72     public static void startup() {
73         // register connection type for platform:/plugin handling
74
if (isRegistered)
75             return;
76         PlatformURLHandler.register(PLUGIN, PlatformURLPluginConnection.class);
77         isRegistered = true;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#getAuxillaryURLs()
82      */

83     public URL JavaDoc[] getAuxillaryURLs() throws IOException JavaDoc {
84         if (target == null) {
85             String JavaDoc spec = url.getFile().trim();
86             if (spec.startsWith("/")) //$NON-NLS-1$
87
spec = spec.substring(1);
88             if (!spec.startsWith(PLUGIN))
89                 throw new IOException JavaDoc(NLS.bind(CommonMessages.url_badVariant, url));
90             int ix = spec.indexOf("/", PLUGIN.length() + 1); //$NON-NLS-1$
91
String JavaDoc ref = ix == -1 ? spec.substring(PLUGIN.length() + 1) : spec.substring(PLUGIN.length() + 1, ix);
92             String JavaDoc id = getId(ref);
93             Activator activator = Activator.getDefault();
94             if (activator == null)
95                 throw new IOException JavaDoc(CommonMessages.activator_not_available);
96             target = activator.getBundle(id);
97             if (target == null)
98                 throw new IOException JavaDoc(NLS.bind(CommonMessages.url_resolvePlugin, url));
99         }
100         Bundle[] fragments = Activator.getDefault().getFragments(target);
101         int fragmentLength = (fragments == null) ? 0 : fragments.length;
102         if (fragmentLength == 0)
103             return null;
104         URL JavaDoc[] result = new URL JavaDoc[fragmentLength];
105         for (int i = 0; i < fragmentLength; i++)
106             result[i] = fragments[i].getEntry("/"); //$NON-NLS-1$
107
return result;
108     }
109 }
110
Popular Tags