1 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 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 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"); 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 65 Filter filter = null; 66 try { 67 filter = context.createFilter(Location.CONFIGURATION_FILTER); 68 } catch (InvalidSyntaxException e) { 69 } 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 } 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"); return null; 112 } 113 114 public boolean getBooleanDebugOption(String option, boolean defaultValue) { 115 if (debugTracker == null) { 116 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 117 PrefsMessages.message("Debug tracker is not set"); return defaultValue; 119 } 120 DebugOptions options = (DebugOptions) debugTracker.getService(); 121 if (options != null) { 122 String value = options.getOption(option); 123 if (value != null) 124 return value.equalsIgnoreCase("true"); } 126 return defaultValue; 127 } 128 129 public Bundle getBundle(String bundleName) { 130 if (bundleTracker == null) { 131 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 132 PrefsMessages.message("Bundle tracker is not set"); 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 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 |