KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > osgi > OSGIUtils


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.registry.osgi;
12
13 import org.eclipse.core.internal.registry.RegistryMessages;
14 import org.eclipse.core.internal.runtime.RuntimeLog;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.osgi.service.datalocation.Location;
18 import org.eclipse.osgi.service.debug.DebugOptions;
19 import org.osgi.framework.*;
20 import org.osgi.service.packageadmin.PackageAdmin;
21 import org.osgi.util.tracker.ServiceTracker;
22
23 /**
24  * The class contains a set of helper methods for the runtime content plugin.
25  * The closeServices() method should be called before the plugin is stopped.
26  *
27  * @since org.eclipse.equinox.registry 3.2
28  */

29 public class OSGIUtils {
30     private ServiceTracker debugTracker = null;
31     private ServiceTracker bundleTracker = null;
32     private ServiceTracker configurationLocationTracker = null;
33
34     // OSGI system properties. Copied from EclipseStarter
35
public static final String JavaDoc PROP_CONFIG_AREA = "osgi.configuration.area"; //$NON-NLS-1$
36
public static final String JavaDoc PROP_INSTANCE_AREA = "osgi.instance.area"; //$NON-NLS-1$
37

38     private static final OSGIUtils singleton = new OSGIUtils();
39
40     public static OSGIUtils getDefault() {
41         return singleton;
42     }
43
44     /**
45      * Private constructor to block instance creation.
46      */

47     private OSGIUtils() {
48         super();
49         initServices();
50     }
51
52     private void initServices() {
53         BundleContext context = Activator.getContext();
54         if (context == null) {
55             RuntimeLog.log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, 0, RegistryMessages.bundle_not_activated, null));
56             return;
57         }
58
59         debugTracker = new ServiceTracker(context, DebugOptions.class.getName(), null);
60         debugTracker.open();
61
62         bundleTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
63         bundleTracker.open();
64
65         // locations
66
final String JavaDoc FILTER_PREFIX = "(&(objectClass=org.eclipse.osgi.service.datalocation.Location)(type="; //$NON-NLS-1$
67
Filter filter = null;
68         try {
69             filter = context.createFilter(FILTER_PREFIX + PROP_CONFIG_AREA + "))"); //$NON-NLS-1$
70
} catch (InvalidSyntaxException e) {
71             // ignore this. It should never happen as we have tested the above format.
72
}
73         configurationLocationTracker = new ServiceTracker(context, filter, null);
74         configurationLocationTracker.open();
75
76     }
77
78     void closeServices() {
79         if (debugTracker != null) {
80             debugTracker.close();
81             debugTracker = null;
82         }
83         if (bundleTracker != null) {
84             bundleTracker.close();
85             bundleTracker = null;
86         }
87         if (configurationLocationTracker != null) {
88             configurationLocationTracker.close();
89             configurationLocationTracker = null;
90         }
91     }
92
93     public boolean getBooleanDebugOption(String JavaDoc option, boolean defaultValue) {
94         if (debugTracker == null) {
95             RuntimeLog.log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, 0, RegistryMessages.bundle_not_activated, null));
96             return defaultValue;
97         }
98         DebugOptions options = (DebugOptions) debugTracker.getService();
99         if (options != null) {
100             String JavaDoc value = options.getOption(option);
101             if (value != null)
102                 return value.equalsIgnoreCase("true"); //$NON-NLS-1$
103
}
104         return defaultValue;
105     }
106
107     public PackageAdmin getPackageAdmin() {
108         if (bundleTracker == null) {
109             RuntimeLog.log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, 0, RegistryMessages.bundle_not_activated, null));
110             return null;
111         }
112         return (PackageAdmin) bundleTracker.getService();
113     }
114
115     public Bundle getBundle(String JavaDoc bundleName) {
116         PackageAdmin packageAdmin = getPackageAdmin();
117         if (packageAdmin == null)
118             return null;
119         Bundle[] bundles = packageAdmin.getBundles(bundleName, null);
120         if (bundles == null)
121             return null;
122         //Return the first bundle that is not installed or uninstalled
123
for (int i = 0; i < bundles.length; i++) {
124             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
125                 return bundles[i];
126             }
127         }
128         return null;
129     }
130
131     public Bundle[] getFragments(Bundle bundle) {
132         PackageAdmin packageAdmin = getPackageAdmin();
133         if (packageAdmin == null)
134             return null;
135         return packageAdmin.getFragments(bundle);
136     }
137
138     public boolean isFragment(Bundle bundle) {
139         PackageAdmin packageAdmin = getPackageAdmin();
140         if (packageAdmin == null)
141             return false;
142         return (packageAdmin.getBundleType(bundle) & PackageAdmin.BUNDLE_TYPE_FRAGMENT) > 0;
143     }
144
145     public Bundle[] getHosts(Bundle bundle) {
146         PackageAdmin packageAdmin = getPackageAdmin();
147         if (packageAdmin == null)
148             return null;
149         return packageAdmin.getHosts(bundle);
150     }
151
152     public Location getConfigurationLocation() {
153         if (configurationLocationTracker == null)
154             return null;
155         return (Location) configurationLocationTracker.getService();
156     }
157 }
158
Popular Tags