KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > PreferencesOSGiUtils


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.preferences;
12
13 import org.eclipse.core.internal.preferences.exchange.ILegacyPreferences;
14 import org.eclipse.osgi.service.datalocation.Location;
15 import org.eclipse.osgi.service.debug.DebugOptions;
16 import org.osgi.framework.*;
17 import org.osgi.service.packageadmin.PackageAdmin;
18 import org.osgi.util.tracker.ServiceTracker;
19
20 /**
21  * This class contains a set of helper OSGI methods for the Preferences plugin.
22  * The closeServices() method should be called before the plugin is stopped.
23  *
24  * @since org.eclipse.equinox.preferences 3.2
25  */

26 public class PreferencesOSGiUtils {
27     private ServiceTracker initTracker = null;
28     private ServiceTracker debugTracker = null;
29     private ServiceTracker bundleTracker = null;
30     private ServiceTracker configurationLocationTracker = null;
31     private ServiceTracker instanceLocationTracker = null;
32
33     private static final PreferencesOSGiUtils singleton = new PreferencesOSGiUtils();
34
35     public static PreferencesOSGiUtils getDefault() {
36         return singleton;
37     }
38
39     /**
40      * Private constructor to block instance creation.
41      */

42     private PreferencesOSGiUtils() {
43         super();
44     }
45
46     void openServices() {
47         BundleContext context = Activator.getContext();
48         if (context == null) {
49             if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
50                 PrefsMessages.message("PreferencesOSGiUtils called before plugin started"); //$NON-NLS-1$
51
return;
52         }
53
54         initTracker = new ServiceTracker(context, ILegacyPreferences.class.getName(), null);
55         initTracker.open(true);
56
57         debugTracker = new ServiceTracker(context, DebugOptions.class.getName(), null);
58         debugTracker.open();
59
60         bundleTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
61         bundleTracker.open();
62
63         // locations
64

65         Filter filter = null;
66         try {
67             filter = context.createFilter(Location.CONFIGURATION_FILTER);
68         } catch (InvalidSyntaxException e) {
69             // ignore this. It should never happen as we have tested the above format.
70
}
71         configurationLocationTracker = new ServiceTracker(context, filter, null);
72         configurationLocationTracker.open();
73
74         try {
75             filter = context.createFilter(Location.INSTANCE_FILTER);
76         } catch (InvalidSyntaxException e) {
77             // ignore this. It should never happen as we have tested the above format.
78
}
79         instanceLocationTracker = new ServiceTracker(context, filter, null);
80         instanceLocationTracker.open();
81     }
82
83     void closeServices() {
84         if (initTracker != null) {
85             initTracker.close();
86             initTracker = null;
87         }
88         if (debugTracker != null) {
89             debugTracker.close();
90             debugTracker = null;
91         }
92         if (bundleTracker != null) {
93             bundleTracker.close();
94             bundleTracker = null;
95         }
96         if (configurationLocationTracker != null) {
97             configurationLocationTracker.close();
98             configurationLocationTracker = null;
99         }
100         if (instanceLocationTracker != null) {
101             instanceLocationTracker.close();
102             instanceLocationTracker = null;
103         }
104     }
105
106     public ILegacyPreferences getLegacyPreferences() {
107         if (initTracker != null)
108             return (ILegacyPreferences) initTracker.getService();
109         if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
110             PrefsMessages.message("Legacy preference tracker is not set"); //$NON-NLS-1$
111
return null;
112     }
113
114     public boolean getBooleanDebugOption(String JavaDoc option, boolean defaultValue) {
115         if (debugTracker == null) {
116             if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
117                 PrefsMessages.message("Debug tracker is not set"); //$NON-NLS-1$
118
return defaultValue;
119         }
120         DebugOptions options = (DebugOptions) debugTracker.getService();
121         if (options != null) {
122             String JavaDoc value = options.getOption(option);
123             if (value != null)
124                 return value.equalsIgnoreCase("true"); //$NON-NLS-1$
125
}
126         return defaultValue;
127     }
128
129     public Bundle getBundle(String JavaDoc bundleName) {
130         if (bundleTracker == null) {
131             if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
132                 PrefsMessages.message("Bundle tracker is not set"); //$NON-NLS-1$
133
return null;
134         }
135         PackageAdmin packageAdmin = (PackageAdmin) bundleTracker.getService();
136         if (packageAdmin == null)
137             return null;
138         Bundle[] bundles = packageAdmin.getBundles(bundleName, null);
139         if (bundles == null)
140             return null;
141         //Return the first bundle that is not installed or uninstalled
142
for (int i = 0; i < bundles.length; i++) {
143             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
144                 return bundles[i];
145             }
146         }
147         return null;
148     }
149
150     public Location getConfigurationLocation() {
151         if (configurationLocationTracker != null)
152             return (Location) configurationLocationTracker.getService();
153         return null;
154     }
155
156     public Location getInstanceLocation() {
157         if (instanceLocationTracker != null)
158             return (Location) instanceLocationTracker.getService();
159         return null;
160     }
161 }
162
Popular Tags