1 11 package org.eclipse.core.internal.runtime; 12 13 import java.net.URL ; 14 import java.util.*; 15 import org.eclipse.core.internal.boot.PlatformURLBaseConnection; 16 import org.eclipse.core.internal.boot.PlatformURLHandler; 17 import org.eclipse.core.runtime.IAdapterManager; 18 import org.eclipse.osgi.framework.log.FrameworkLog; 19 import org.eclipse.osgi.service.datalocation.Location; 20 import org.eclipse.osgi.service.debug.DebugOptions; 21 import org.eclipse.osgi.service.localization.BundleLocalization; 22 import org.eclipse.osgi.service.urlconversion.URLConverter; 23 import org.osgi.framework.*; 24 import org.osgi.service.packageadmin.PackageAdmin; 25 import org.osgi.service.url.URLConstants; 26 import org.osgi.service.url.URLStreamHandlerService; 27 import org.osgi.util.tracker.ServiceTracker; 28 29 34 public class Activator implements BundleActivator { 35 36 39 private static Map urlTrackers = new HashMap(); 40 private static BundleContext bundleContext; 41 private static Activator singleton; 42 private ServiceRegistration platformURLConverterService = null; 43 private ServiceRegistration adapterManagerService = null; 44 private ServiceTracker installLocationTracker = null; 45 private ServiceTracker instanceLocationTracker = null; 46 private ServiceTracker configLocationTracker = null; 47 private ServiceTracker bundleTracker = null; 48 private ServiceTracker debugTracker = null; 49 private ServiceTracker logTracker = null; 50 private ServiceTracker localizationTracker = null; 51 52 56 public static Activator getDefault() { 57 return singleton; 58 } 59 60 64 public static void message(String message) { 65 StringBuffer buffer = new StringBuffer (); 66 buffer.append(new Date(System.currentTimeMillis())); 67 buffer.append(" - ["); buffer.append(Thread.currentThread().getName()); 69 buffer.append("] "); buffer.append(message); 71 System.out.println(buffer.toString()); 72 } 73 74 77 public void start(BundleContext context) throws Exception { 78 bundleContext = context; 79 singleton = this; 80 Dictionary urlProperties = new Hashtable(); 81 urlProperties.put("protocol", "platform"); platformURLConverterService = context.registerService(URLConverter.class.getName(), new PlatformURLConverter(), urlProperties); 83 adapterManagerService = context.registerService(IAdapterManager.class.getName(), AdapterManager.getDefault(), null); 84 installPlatformURLSupport(); 85 } 86 87 90 public Location getConfigurationLocation() { 91 if (configLocationTracker == null) { 92 Filter filter = null; 93 try { 94 filter = bundleContext.createFilter(Location.CONFIGURATION_FILTER); 95 } catch (InvalidSyntaxException e) { 96 } 98 configLocationTracker = new ServiceTracker(bundleContext, filter, null); 99 configLocationTracker.open(); 100 } 101 return (Location) configLocationTracker.getService(); 102 } 103 104 107 public DebugOptions getDebugOptions() { 108 if (debugTracker == null) { 109 debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null); 110 debugTracker.open(); 111 } 112 return (DebugOptions) debugTracker.getService(); 113 } 114 115 118 public FrameworkLog getFrameworkLog() { 119 if (logTracker == null) { 120 logTracker = new ServiceTracker(bundleContext, FrameworkLog.class.getName(), null); 121 logTracker.open(); 122 } 123 return (FrameworkLog) logTracker.getService(); 124 } 125 126 129 public Location getInstanceLocation() { 130 if (instanceLocationTracker == null) { 131 Filter filter = null; 132 try { 133 filter = bundleContext.createFilter(Location.INSTANCE_FILTER); 134 } catch (InvalidSyntaxException e) { 135 } 137 instanceLocationTracker = new ServiceTracker(bundleContext, filter, null); 138 instanceLocationTracker.open(); 139 } 140 return (Location) instanceLocationTracker.getService(); 141 } 142 143 148 public Bundle getBundle(String symbolicName) { 149 PackageAdmin admin = getBundleAdmin(); 150 if (admin == null) 151 return null; 152 Bundle[] bundles = admin.getBundles(symbolicName, null); 153 if (bundles == null) 154 return null; 155 for (int i = 0; i < bundles.length; i++) { 157 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { 158 return bundles[i]; 159 } 160 } 161 return null; 162 } 163 164 167 private PackageAdmin getBundleAdmin() { 168 if (bundleTracker == null) { 169 bundleTracker = new ServiceTracker(getContext(), PackageAdmin.class.getName(), null); 170 bundleTracker.open(); 171 } 172 return (PackageAdmin) bundleTracker.getService(); 173 } 174 175 178 public Bundle[] getFragments(Bundle host) { 179 PackageAdmin admin = getBundleAdmin(); 180 if (admin == null) 181 return new Bundle[0]; 182 return admin.getFragments(host); 183 } 184 185 188 public Location getInstallLocation() { 189 if (installLocationTracker == null) { 190 Filter filter = null; 191 try { 192 filter = bundleContext.createFilter(Location.INSTALL_FILTER); 193 } catch (InvalidSyntaxException e) { 194 } 196 installLocationTracker = new ServiceTracker(bundleContext, filter, null); 197 installLocationTracker.open(); 198 } 199 return (Location) installLocationTracker.getService(); 200 } 201 202 206 public String getBundleId(Object object) { 207 if (object == null) 208 return null; 209 if (bundleTracker == null) { 210 message("Bundle tracker is not set"); return null; 212 } 213 PackageAdmin packageAdmin = (PackageAdmin) bundleTracker.getService(); 214 if (packageAdmin == null) 215 return null; 216 217 Bundle source = packageAdmin.getBundle(object.getClass()); 218 if (source != null && source.getSymbolicName() != null) 219 return source.getSymbolicName(); 220 return null; 221 } 222 223 public ResourceBundle getLocalization(Bundle bundle, String locale) { 224 if (localizationTracker == null) { 225 BundleContext context = Activator.getContext(); 226 if (context == null) { 227 message("ResourceTranslator called before plugin is started"); return null; 229 } 230 localizationTracker = new ServiceTracker(context, BundleLocalization.class.getName(), null); 231 localizationTracker.open(); 232 } 233 BundleLocalization location = (BundleLocalization) localizationTracker.getService(); 234 if (location != null) 235 return location.getLocalization(bundle, locale); 236 237 return null; 238 } 239 240 243 public void stop(BundleContext context) throws Exception { 244 closeURLTrackerServices(); 245 if (platformURLConverterService != null) { 246 platformURLConverterService.unregister(); 247 platformURLConverterService = null; 248 } 249 if (adapterManagerService != null) { 250 adapterManagerService.unregister(); 251 adapterManagerService = null; 252 } 253 if (installLocationTracker != null) { 254 installLocationTracker.close(); 255 installLocationTracker = null; 256 } 257 if (configLocationTracker != null) { 258 configLocationTracker.close(); 259 configLocationTracker = null; 260 } 261 if (bundleTracker != null) { 262 bundleTracker.close(); 263 bundleTracker = null; 264 } 265 if (debugTracker != null) { 266 debugTracker.close(); 267 debugTracker = null; 268 } 269 if (logTracker != null) { 270 logTracker.close(); 271 logTracker = null; 272 } 273 if (instanceLocationTracker != null) { 274 instanceLocationTracker.close(); 275 instanceLocationTracker = null; 276 } 277 if (localizationTracker != null) { 278 localizationTracker.close(); 279 localizationTracker = null; 280 } 281 bundleContext = null; 282 singleton = null; 283 } 284 285 288 static BundleContext getContext() { 289 return bundleContext; 290 } 291 292 295 private static void closeURLTrackerServices() { 296 synchronized (urlTrackers) { 297 if (!urlTrackers.isEmpty()) { 298 for (Iterator iter = urlTrackers.keySet().iterator(); iter.hasNext();) { 299 String key = (String ) iter.next(); 300 ServiceTracker tracker = (ServiceTracker) urlTrackers.get(key); 301 tracker.close(); 302 } 303 urlTrackers = new HashMap(); 304 } 305 } 306 } 307 308 312 public static URLConverter getURLConverter(URL url) { 313 String protocol = url.getProtocol(); 314 synchronized (urlTrackers) { 315 ServiceTracker tracker = (ServiceTracker) urlTrackers.get(protocol); 316 if (tracker == null) { 317 String FILTER_PREFIX = "(&(objectClass=" + URLConverter.class.getName() + ")(protocol="; String FILTER_POSTFIX = "))"; Filter filter = null; 321 try { 322 filter = getContext().createFilter(FILTER_PREFIX + protocol + FILTER_POSTFIX); 323 } catch (InvalidSyntaxException e) { 324 return null; 325 } 326 tracker = new ServiceTracker(getContext(), filter, null); 327 tracker.open(); 328 urlTrackers.put(protocol, tracker); 330 } 331 return (URLConverter) tracker.getService(); 332 } 333 } 334 335 338 private void installPlatformURLSupport() { 339 PlatformURLPluginConnection.startup(); 340 PlatformURLFragmentConnection.startup(); 341 PlatformURLMetaConnection.startup(); 342 PlatformURLConfigConnection.startup(); 343 344 Location service = getInstallLocation(); 345 if (service != null) 346 PlatformURLBaseConnection.startup(service.getURL()); 347 348 Hashtable properties = new Hashtable(1); 349 properties.put(URLConstants.URL_HANDLER_PROTOCOL, new String [] {PlatformURLHandler.PROTOCOL}); 350 getContext().registerService(URLStreamHandlerService.class.getName(), new PlatformURLHandler(), properties); 351 } 352 353 } 354 | Popular Tags |