KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > util > BundleUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.ui.internal.intro.impl.model.util;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.runtime.FileLocator;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProduct;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.ui.internal.intro.impl.util.Log;
24 import org.eclipse.ui.internal.intro.impl.util.StringUtil;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.Constants;
27
28 /**
29  * Bundle convenience methods.
30  */

31 public class BundleUtil {
32
33     private static String JavaDoc NL_TAG = "$nl$/"; //$NON-NLS-1$
34
private final static String JavaDoc PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; //$NON-NLS-1$
35
private final static String JavaDoc PLUGINS_ROOT = "PLUGINS_ROOT/"; //$NON-NLS-1$
36

37     /**
38      * Utility method to validate the state of a bundle. Log invalid bundles to
39      * log file.
40      */

41     public static boolean bundleHasValidState(Bundle bundle) {
42         if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
43                 || bundle.getState() == Bundle.INSTALLED) {
44
45             if (bundle == null)
46                 Log.error("Intro tried accessing a NULL bundle.", null); //$NON-NLS-1$
47
else {
48                 String JavaDoc msg = StringUtil
49                     .concat("Intro tried accessing Bundle: ", getBundleHeader( //$NON-NLS-1$
50
bundle, Constants.BUNDLE_NAME), " vendor: ", //$NON-NLS-1$
51
getBundleHeader(bundle, Constants.BUNDLE_VENDOR),
52                         " bundle state: ", String.valueOf(bundle.getState())).toString(); //$NON-NLS-1$
53
Log.error(msg, null);
54             }
55             return false;
56         }
57
58         return true;
59     }
60
61     /**
62      * Retrieves the given key from the bundle header.
63      *
64      * @param bundle
65      * @param key
66      * @return
67      */

68     public static String JavaDoc getBundleHeader(Bundle bundle, String JavaDoc key) {
69         return (String JavaDoc) bundle.getHeaders().get(key);
70     }
71
72
73     public static Bundle getBundleFromConfigurationElement(
74             IConfigurationElement cfg) {
75         return Platform.getBundle(cfg.getContributor().getName());
76     }
77
78
79     /**
80      * Get the resourcelocation, but do not force an $nl$ on it.
81      *
82      * @param resource
83      * @param element
84      * @return
85      */

86     public static String JavaDoc getResourceLocation(String JavaDoc resource,
87             IConfigurationElement element) {
88         Bundle bundle = getBundleFromConfigurationElement(element);
89         return getResolvedResourceLocation(resource, bundle, false);
90     }
91
92
93     /**
94      * Returns the fully qualified location of the passed resource string from
95      * the passed plugin id. If the file could not be loaded from the plugin,
96      * the resource is returned as is.
97      *
98      * @param resource
99      * @return
100      */

101     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource,
102             String JavaDoc pluginId) {
103         Bundle bundle = Platform.getBundle(pluginId);
104         return getResolvedResourceLocation(resource, bundle, true);
105     }
106
107
108     /**
109      * Shorthand util method.
110      *
111      * @param resource
112      * @return
113      */

114     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource,
115             Bundle bundle) {
116         return getResolvedResourceLocation(resource, bundle, true);
117     }
118
119
120     public static String JavaDoc getResolvedResourceLocation(String JavaDoc base,
121             String JavaDoc resource, Bundle bundle) {
122         // quick exits.
123
if (resource == null)
124             return null;
125
126         String JavaDoc fullResource = new Path(base).append(resource).toString();
127         String JavaDoc resolvedResource = getResolvedResourceLocation(fullResource,
128             bundle, true);
129
130         if (resolvedResource.equals(fullResource))
131             // return resource as is when the resource does not exist.
132
return resource;
133         return resolvedResource;
134     }
135
136
137     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource,
138             Bundle bundle, boolean forceNLResolve) {
139         // quick exits.
140
if (resource == null)
141             return null;
142
143         if (bundle == null || !bundleHasValidState(bundle))
144             return resource;
145
146         URL JavaDoc localLocation = null;
147         try {
148             // resolve PLUGINS_ROOT
149
int index = resource.indexOf(PLUGINS_ROOT);
150             if (index != -1) {
151                 resource = resource.substring(index + PLUGINS_ROOT.length());
152                 index = resource.indexOf('/');
153                 if (index != -1) {
154                     String JavaDoc bundleName = resource.substring(0, index);
155                     if (PRODUCT_PLUGIN.equals(bundleName)) {
156                         IProduct product = Platform.getProduct();
157                         if (product != null) {
158                             Bundle productBundle = product.getDefiningBundle();
159                             if (productBundle != null) {
160                                 bundleName = productBundle.getSymbolicName();
161                             }
162                         }
163                     }
164                     resource = resource.substring(index + 1);
165                     Bundle actualBundle = Platform.getBundle(bundleName);
166                     if (actualBundle != null) {
167                         return getResolvedResourceLocation(resource, actualBundle, forceNLResolve);
168                     }
169                 }
170             }
171             
172             // we need to resolve this URL.
173
String JavaDoc copyResource = resource;
174             if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
175                 if (copyResource.startsWith("/") //$NON-NLS-1$
176
|| copyResource.startsWith("\\")) //$NON-NLS-1$
177
copyResource = resource.substring(1);
178                 copyResource = NL_TAG + copyResource;
179             }
180             IPath resourcePath = new Path(copyResource);
181             localLocation = FileLocator.find(bundle, resourcePath, null);
182             if (localLocation == null) {
183                 // localLocation can be null if the passed resource could not
184
// be found relative to the plugin. log fact, return resource,
185
// as is.
186
String JavaDoc msg = StringUtil.concat("Could not find resource: ", //$NON-NLS-1$
187
resource, " in ", getBundleHeader( //$NON-NLS-1$
188
bundle, Constants.BUNDLE_NAME)).toString();
189                 Log.warning(msg);
190                 return resource;
191             }
192             /*
193             localLocation = FileLocator.toFileURL(localLocation);
194             return localLocation.toExternalForm();
195             */

196             return toExternalForm(localLocation);
197         } catch (Exception JavaDoc e) {
198             String JavaDoc msg = StringUtil.concat("Failed to load resource: ", //$NON-NLS-1$
199
resource, " from ", getBundleHeader(bundle, //$NON-NLS-1$
200
Constants.BUNDLE_NAME)).toString();
201             Log.error(msg, e);
202             return resource;
203         }
204     }
205
206
207
208
209     /** *** used by Intro parser ***** */
210     /*
211      * Util method to return an URL to a plugin relative resource.
212      */

213     public static URL JavaDoc getResourceAsURL(String JavaDoc resource, String JavaDoc pluginId) {
214         Bundle bundle = Platform.getBundle(pluginId);
215         URL JavaDoc localLocation = FileLocator.find(bundle, new Path(
216             resource), null);
217         return localLocation;
218     }
219
220
221
222
223     /** ********************* Used by HTML generator ****************** */
224     /**
225      * Get the absolute path of the given bundle, in the form
226      * file:/path_to_plugin
227      *
228      * @param bundle
229      * @return
230      */

231     public static String JavaDoc getResolvedBundleLocation(Bundle bundle) {
232         try {
233             URL JavaDoc bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
234
if (bundleLocation == null)
235                 return null;
236             /*
237             bundleLocation = FileLocator.toFileURL(bundleLocation);
238             return bundleLocation.toExternalForm();
239             */

240             return toExternalForm(bundleLocation);
241         } catch (IllegalStateException JavaDoc e) {
242             Log.error("Failed to access bundle: " //$NON-NLS-1$
243
+ bundle.getSymbolicName(), e);
244             return null;
245         }/* catch (IOException e) {
246             Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
247                     + bundle.getSymbolicName(), e);
248             return null;
249         } */

250     }
251
252     /**
253      * Get the absolute path of the bundle with id <code>bundleId</code>. If
254      * no such bundle is found, return null.
255      *
256      * @param bundleId
257      * @return
258      */

259     public static String JavaDoc getResolvedBundleLocation(String JavaDoc bundleId) {
260         Bundle bundle = Platform.getBundle(bundleId);
261         if (bundle == null)
262             return null;
263         return getResolvedBundleLocation(bundle);
264     }
265     
266     /*
267      * Bug 126085 - need to fix up file: protocol
268      * to a form that IE7 understands (file:///).
269      */

270     
271     private static String JavaDoc toExternalForm(URL JavaDoc localURL) {
272         try {
273             localURL = FileLocator.toFileURL(localURL);
274             String JavaDoc result = localURL.toExternalForm();
275             if (result.startsWith("file:/")) { //$NON-NLS-1$
276
if (result.startsWith("file:///")==false) { //$NON-NLS-1$
277
result = "file:///"+result.substring(6); //$NON-NLS-1$
278
}
279             }
280             return result;
281         }
282         catch (IOException JavaDoc e) {
283             String JavaDoc msg = "Failed to resolve URL: " //$NON-NLS-1$
284
+ localURL.toString();
285                 Log.error(msg, e);
286                 return localURL.toString();
287         }
288     }
289
290 }
291
Popular Tags