001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.universal.util;
011:
012: import java.io.IOException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.FileLocator;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.core.runtime.Platform;
020: import org.osgi.framework.Bundle;
021: import org.osgi.framework.Constants;
022:
023: /**
024: * Bundle convenience methods.
025: */
026: public class BundleUtil {
027:
028: private static String NL_TAG = "$nl$/"; //$NON-NLS-1$
029:
030: /**
031: * Utility method to validate the state of a bundle. Log invalid bundles to
032: * log file.
033: */
034: public static boolean bundleHasValidState(Bundle bundle) {
035: if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
036: || bundle.getState() == Bundle.INSTALLED) {
037:
038: if (bundle == null)
039: Log
040: .error(
041: "Universal Welcome tried accessing a NULL bundle.", null); //$NON-NLS-1$
042: else {
043: String msg = StringUtil
044: .concat(
045: "Universal Welcome tried accessing Bundle: ", getBundleHeader( //$NON-NLS-1$
046: bundle, Constants.BUNDLE_NAME),
047: " vendor: ", //$NON-NLS-1$
048: getBundleHeader(bundle,
049: Constants.BUNDLE_VENDOR),
050: " bundle state: ", String.valueOf(bundle.getState())).toString(); //$NON-NLS-1$
051: Log.error(msg, null);
052: }
053: return false;
054: }
055:
056: return true;
057: }
058:
059: /**
060: * Retrieves the given key from the bundle header.
061: *
062: * @param bundle
063: * @param key
064: * @return
065: */
066: public static String getBundleHeader(Bundle bundle, String key) {
067: return (String) bundle.getHeaders().get(key);
068: }
069:
070: public static Bundle getBundleFromConfigurationElement(
071: IConfigurationElement cfg) {
072: return Platform.getBundle(cfg.getNamespaceIdentifier());
073: }
074:
075: /**
076: * Get the resourcelocation, but do not force an $nl$ on it.
077: *
078: * @param resource
079: * @param element
080: * @return
081: */
082: public static String getResourceLocation(String resource,
083: IConfigurationElement element) {
084: Bundle bundle = getBundleFromConfigurationElement(element);
085: return getResolvedResourceLocation(resource, bundle, false);
086: }
087:
088: /**
089: * Returns the fully qualified location of the passed resource string from
090: * the passed plugin id. If the file could not be loaded from the plugin,
091: * the resource is returned as is.
092: *
093: * @param resource
094: * @return
095: */
096: public static String getResolvedResourceLocation(String resource,
097: String pluginId) {
098: Bundle bundle = Platform.getBundle(pluginId);
099: return getResolvedResourceLocation(resource, bundle, true);
100: }
101:
102: /**
103: * Shorthand util method.
104: *
105: * @param resource
106: * @return
107: */
108: public static String getResolvedResourceLocation(String resource,
109: Bundle bundle) {
110: return getResolvedResourceLocation(resource, bundle, true);
111: }
112:
113: public static String getResolvedResourceLocation(String base,
114: String resource, Bundle bundle) {
115: // quick exits.
116: if (resource == null)
117: return null;
118:
119: String fullResource = new Path(base).append(resource)
120: .toString();
121: String resolvedResource = getResolvedResourceLocation(
122: fullResource, bundle, true);
123:
124: if (resolvedResource.equals(fullResource))
125: // return resource as is when the resource does not exist.
126: return resource;
127: return resolvedResource;
128: }
129:
130: public static String getResolvedResourceLocation(String resource,
131: Bundle bundle, boolean forceNLResolve) {
132: // quick exits.
133: if (resource == null)
134: return null;
135:
136: if (bundle == null || !bundleHasValidState(bundle))
137: return resource;
138:
139: URL localLocation = null;
140: try {
141: // we need to resolve this URL.
142: String copyResource = resource;
143: if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
144: if (copyResource.startsWith("/") //$NON-NLS-1$
145: || copyResource.startsWith("\\")) //$NON-NLS-1$
146: copyResource = resource.substring(1);
147: copyResource = NL_TAG + copyResource;
148: }
149: IPath resourcePath = new Path(copyResource);
150: localLocation = FileLocator
151: .find(bundle, resourcePath, null);
152: if (localLocation == null) {
153: // localLocation can be null if the passed resource could not
154: // be found relative to the plugin. log fact, return resource,
155: // as is.
156: String msg = StringUtil.concat(
157: "Could not find resource: ", //$NON-NLS-1$
158: resource, " in ", getBundleHeader( //$NON-NLS-1$
159: bundle, Constants.BUNDLE_NAME))
160: .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 e) {
170: String 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 toExternalForm(URL localURL) {
179: try {
180: localURL = FileLocator.toFileURL(localURL);
181: String 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: } catch (IOException e) {
189: String msg = "Failed to resolve URL: " //$NON-NLS-1$
190: + localURL.toString();
191: Log.error(msg, e);
192: return localURL.toString();
193: }
194: }
195:
196: /** *** used by Intro parser ***** */
197: /*
198: * Util method to return an URL to a plugin relative resource.
199: */
200: public static URL getResourceAsURL(String resource, String pluginId) {
201: Bundle bundle = Platform.getBundle(pluginId);
202: URL localLocation = FileLocator.find(bundle,
203: new Path(resource), null);
204: return localLocation;
205: }
206:
207: /** ********************* Used by HTML generator ****************** */
208: /**
209: * Get the absolute path of the given bundle, in the form
210: * file:/path_to_plugin
211: *
212: * @param bundle
213: * @return
214: */
215: public static String getResolvedBundleLocation(Bundle bundle) {
216: try {
217: URL bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
218: if (bundleLocation == null)
219: return null;
220: /*
221: bundleLocation = FileLocator.toFileURL(bundleLocation);
222: return bundleLocation.toExternalForm();
223: */
224: return toExternalForm(bundleLocation);
225: } catch (IllegalStateException e) {
226: Log.error("Failed to access bundle: " //$NON-NLS-1$
227: + bundle.getSymbolicName(), e);
228: return null;
229: } /* catch (IOException e) {
230: Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
231: + bundle.getSymbolicName(), e);
232: return null;
233: } */
234: }
235:
236: /**
237: * Get the absolute path of the bundle with id <code>bundleId</code>. If
238: * no such bundle is found, return null.
239: *
240: * @param bundleId
241: * @return
242: */
243: public static String getResolvedBundleLocation(String bundleId) {
244: Bundle bundle = Platform.getBundle(bundleId);
245: if (bundle == null)
246: return null;
247: return getResolvedBundleLocation(bundle);
248: }
249:
250: }
|