01: /*******************************************************************************
02: * Copyright (c) 2003, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.util;
11:
12: import java.net.URL;
13:
14: import org.eclipse.core.runtime.IStatus;
15: import org.eclipse.core.runtime.Path;
16: import org.eclipse.core.runtime.Platform;
17: import org.eclipse.core.runtime.Status;
18: import org.eclipse.ui.internal.WorkbenchPlugin;
19: import org.osgi.framework.Bundle;
20:
21: /**
22: * A set of static methods that provide an nicer interface to common platform
23: * operations related to bundle management.
24: */
25: public class BundleUtility {
26: public static boolean isActive(Bundle bundle) {
27: if (bundle == null) {
28: return false;
29: }
30: return bundle.getState() == Bundle.ACTIVE;
31: }
32:
33: public static boolean isActivated(Bundle bundle) {
34: if ((bundle.getState() & Bundle.STARTING) != 0)
35: return WorkbenchPlugin.getDefault().isStarting(bundle);
36: return bundle != null
37: && (bundle.getState() & (Bundle.ACTIVE | Bundle.STOPPING)) != 0;
38: }
39:
40: // TODO: needs a better name
41: public static boolean isReady(Bundle bundle) {
42: return bundle != null && isReady(bundle.getState());
43: }
44:
45: public static boolean isReady(int bundleState) {
46: return (bundleState & (Bundle.RESOLVED | Bundle.STARTING
47: | Bundle.ACTIVE | Bundle.STOPPING)) != 0;
48: }
49:
50: public static boolean isActive(String bundleId) {
51: return isActive(Platform.getBundle(bundleId));
52: }
53:
54: public static boolean isActivated(String bundleId) {
55: return isActivated(Platform.getBundle(bundleId));
56: }
57:
58: public static boolean isReady(String bundleId) {
59: return isReady(Platform.getBundle(bundleId));
60: }
61:
62: public static URL find(Bundle bundle, String path) {
63: if (bundle == null) {
64: return null;
65: }
66: return Platform.find(bundle, new Path(path));
67: }
68:
69: public static URL find(String bundleId, String path) {
70: return find(Platform.getBundle(bundleId), path);
71: }
72:
73: public static void log(String bundleId, Throwable exception) {
74: Bundle bundle = Platform.getBundle(bundleId);
75: if (bundle == null) {
76: return;
77: }
78: IStatus status = new Status(
79: IStatus.ERROR,
80: bundleId,
81: IStatus.ERROR,
82: exception.getMessage() == null ? "" : exception.getMessage(), //$NON-NLS-1$
83: exception);
84: Platform.getLog(bundle).log(status);
85: }
86: }
|