KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > 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.universal.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.Path;
21 import org.eclipse.core.runtime.Platform;
22 import org.osgi.framework.Bundle;
23 import org.osgi.framework.Constants;
24
25 /**
26  * Bundle convenience methods.
27  */

28 public class BundleUtil {
29
30     private static String JavaDoc NL_TAG = "$nl$/"; //$NON-NLS-1$
31

32
33     /**
34      * Utility method to validate the state of a bundle. Log invalid bundles to
35      * log file.
36      */

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

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

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

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

110     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource,
111             Bundle bundle) {
112         return getResolvedResourceLocation(resource, bundle, true);
113     }
114
115
116     public static String JavaDoc getResolvedResourceLocation(String JavaDoc base,
117             String JavaDoc resource, Bundle bundle) {
118         // quick exits.
119
if (resource == null)
120             return null;
121
122         String JavaDoc fullResource = new Path(base).append(resource).toString();
123         String JavaDoc resolvedResource = getResolvedResourceLocation(fullResource,
124             bundle, true);
125
126         if (resolvedResource.equals(fullResource))
127             // return resource as is when the resource does not exist.
128
return resource;
129         return resolvedResource;
130     }
131
132
133     public static String JavaDoc getResolvedResourceLocation(String JavaDoc resource,
134             Bundle bundle, boolean forceNLResolve) {
135         // quick exits.
136
if (resource == null)
137             return null;
138
139         if (bundle == null || !bundleHasValidState(bundle))
140             return resource;
141
142         URL JavaDoc localLocation = null;
143         try {
144             // we need to resolve this URL.
145
String JavaDoc copyResource = resource;
146             if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
147                 if (copyResource.startsWith("/") //$NON-NLS-1$
148
|| copyResource.startsWith("\\")) //$NON-NLS-1$
149
copyResource = resource.substring(1);
150                 copyResource = NL_TAG + copyResource;
151             }
152             IPath resourcePath = new Path(copyResource);
153             localLocation = FileLocator.find(bundle, resourcePath, null);
154             if (localLocation == null) {
155                 // localLocation can be null if the passed resource could not
156
// be found relative to the plugin. log fact, return resource,
157
// as is.
158
String JavaDoc msg = StringUtil.concat("Could not find resource: ", //$NON-NLS-1$
159
resource, " in ", getBundleHeader( //$NON-NLS-1$
160
bundle, Constants.BUNDLE_NAME)).toString();
161                 Log.warning(msg);
162                 return resource;
163             }
164             /*
165             localLocation = FileLocator.toFileURL(localLocation);
166             return localLocation.toExternalForm();
167             */

168             return toExternalForm(localLocation);
169         } catch (Exception JavaDoc e) {
170             String JavaDoc msg = StringUtil.concat("Failed to load resource: ", //$NON-NLS-1$
171
resource, " from ", getBundleHeader(bundle, //$NON-NLS-1$
172
Constants.BUNDLE_NAME)).toString();
173             Log.error(msg, e);
174             return resource;
175         }
176     }
177     
178     private static String JavaDoc toExternalForm(URL JavaDoc localURL) {
179         try {
180             localURL = FileLocator.toFileURL(localURL);
181             String JavaDoc result = localURL.toExternalForm();
182             if (result.startsWith("file:/")) { //$NON-NLS-1$
183
if (result.startsWith("file:///")==false) { //$NON-NLS-1$
184
result = "file:///"+result.substring(6); //$NON-NLS-1$
185
}
186             }
187             return result;
188         }
189         catch (IOException JavaDoc e) {
190             String JavaDoc msg = "Failed to resolve URL: " //$NON-NLS-1$
191
+ localURL.toString();
192                 Log.error(msg, e);
193                 return localURL.toString();
194         }
195     }
196
197
198
199
200     /** *** used by Intro parser ***** */
201     /*
202      * Util method to return an URL to a plugin relative resource.
203      */

204     public static URL JavaDoc getResourceAsURL(String JavaDoc resource, String JavaDoc pluginId) {
205         Bundle bundle = Platform.getBundle(pluginId);
206         URL JavaDoc localLocation = FileLocator.find(bundle, new Path(
207             resource), null);
208         return localLocation;
209     }
210
211
212
213
214     /** ********************* Used by HTML generator ****************** */
215     /**
216      * Get the absolute path of the given bundle, in the form
217      * file:/path_to_plugin
218      *
219      * @param bundle
220      * @return
221      */

222     public static String JavaDoc getResolvedBundleLocation(Bundle bundle) {
223         try {
224             URL JavaDoc bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
225
if (bundleLocation == null)
226                 return null;
227             /*
228             bundleLocation = FileLocator.toFileURL(bundleLocation);
229             return bundleLocation.toExternalForm();
230             */

231             return toExternalForm(bundleLocation);
232         } catch (IllegalStateException JavaDoc e) {
233             Log.error("Failed to access bundle: " //$NON-NLS-1$
234
+ bundle.getSymbolicName(), e);
235             return null;
236         } /* catch (IOException e) {
237             Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
238                     + bundle.getSymbolicName(), e);
239             return null;
240         } */

241     }
242
243     /**
244      * Get the absolute path of the bundle with id <code>bundleId</code>. If
245      * no such bundle is found, return null.
246      *
247      * @param bundleId
248      * @return
249      */

250     public static String JavaDoc getResolvedBundleLocation(String JavaDoc bundleId) {
251         Bundle bundle = Platform.getBundle(bundleId);
252         if (bundle == null)
253             return null;
254         return getResolvedBundleLocation(bundle);
255     }
256
257 }
258
Popular Tags