1 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 29 public class OSGIUtils { 30 private ServiceTracker debugTracker = null; 31 private ServiceTracker bundleTracker = null; 32 private ServiceTracker configurationLocationTracker = null; 33 34 public static final String PROP_CONFIG_AREA = "osgi.configuration.area"; public static final String PROP_INSTANCE_AREA = "osgi.instance.area"; 38 private static final OSGIUtils singleton = new OSGIUtils(); 39 40 public static OSGIUtils getDefault() { 41 return singleton; 42 } 43 44 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 final String FILTER_PREFIX = "(&(objectClass=org.eclipse.osgi.service.datalocation.Location)(type="; Filter filter = null; 68 try { 69 filter = context.createFilter(FILTER_PREFIX + PROP_CONFIG_AREA + "))"); } catch (InvalidSyntaxException e) { 71 } 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 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 value = options.getOption(option); 101 if (value != null) 102 return value.equalsIgnoreCase("true"); } 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 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 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 |