diff options
Diffstat (limited to 'bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java')
-rw-r--r-- | bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java | 301 |
1 files changed, 175 insertions, 126 deletions
diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java index a9535a916..4ec4bcaf5 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/AspectJAdaptorFactory.java @@ -7,10 +7,14 @@ * * Contributors: * David Knibb initial implementation - * Matthew Webster Eclipse 3.2 changes + * Matthew Webster Eclipse 3.2 changes + * Heiko Seeberger Enhancements for service dynamics *******************************************************************************/ + package org.eclipse.equinox.weaving.adaptors; +import java.util.Iterator; + import org.eclipse.equinox.service.weaving.ICachingService; import org.eclipse.equinox.service.weaving.IWeavingService; import org.eclipse.equinox.service.weaving.SupplementerRegistry; @@ -20,134 +24,179 @@ import org.eclipse.osgi.service.resolver.BundleDescription; import org.eclipse.osgi.service.resolver.State; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.service.packageadmin.PackageAdmin; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; public class AspectJAdaptorFactory { - private BundleContext bundleContext; - private IWeavingService singletonWeavingService; - private ICachingService singletonCachingService; - private PackageAdmin packageAdminService; - private SupplementerRegistry supplementerRegistry; - - public AspectJAdaptorFactory () { - } - - public void initialize (BundleContext context, SupplementerRegistry supplementerRegistry) { - if (Debug.DEBUG_GENERAL) Debug.println("> AspectJAdaptorFactory.initialize() context=" + context); - this.bundleContext = context; - this.supplementerRegistry = supplementerRegistry; - - String weavingFilter = "(objectclass="+IWeavingService.class.getName()+")"; - String cachingFilter = "(objectclass="+ICachingService.class.getName()+")"; - /* - * Add listeners to listen for the - * registration of the weaving and caching services - */ - ServiceListener weavingListener = new ServiceListener() { - - public void serviceChanged(ServiceEvent event) { - if(event.getType() == ServiceEvent.REGISTERED) { -// System.err.println("ServiceListener.serviceChanged() event=" + event); - initializeWeavingService(); - } - } - - }; - - ServiceListener cachingListener = new ServiceListener(){ - - public void serviceChanged(ServiceEvent event) { - if(event.getType() == ServiceEvent.REGISTERED) { -// System.err.println("ServiceListener.serviceChanged() event=" + event); - initializeCachingService(); - } - } - - }; - - try { - bundleContext.addServiceListener(weavingListener,weavingFilter); - bundleContext.addServiceListener(cachingListener,cachingFilter); - } - catch (Exception ex) { - ex.printStackTrace(); - } - - initializePackageAdminService(context); - - if (Debug.DEBUG_GENERAL) Debug.println("< AspectJAdaptorFactory.initialize() weavingListener=" + weavingListener + ", cachingListener=" + cachingListener); - } - - protected void initializeWeavingService () { - if (Debug.DEBUG_GENERAL) Debug.println("> AspectJAdaptorFactory.initializeWeavingService()"); - ServiceReference ref = bundleContext.getServiceReference(IWeavingService.class.getName()); - if (ref!=null){ - singletonWeavingService = (IWeavingService)bundleContext.getService(ref); - } - if (Debug.DEBUG_GENERAL) Debug.println("< AspectJAdaptorFactory.initializeWeavingService() weavingService=" + singletonWeavingService); - } - - protected IWeavingService getWeavingService (BaseClassLoader loader) { - if (Debug.DEBUG_WEAVE) Debug.println("> AspectJAdaptorFactory.getWeavingService() baseClassLoader=" + loader); - IWeavingService weavingService = null; - - if (singletonWeavingService != null) { - BaseData baseData = loader.getClasspathManager().getBaseData(); - State state = baseData.getAdaptor().getState(); - Bundle bundle = baseData.getBundle(); - BundleDescription bundleDescription = state.getBundle(bundle.getBundleId()); - weavingService = singletonWeavingService.getInstance((ClassLoader)loader, bundle, state, bundleDescription, supplementerRegistry); - } - if (Debug.DEBUG_WEAVE) Debug.println("< AspectJAdaptorFactory.getWeavingService() service=" + weavingService); - return weavingService; - } - - protected void initializeCachingService () { - if (Debug.DEBUG_CACHE) Debug.println("> AspectJAdaptorFactory.initializeCachingService()"); - ServiceReference ref = bundleContext.getServiceReference(ICachingService.class.getName()); - if (ref != null){ - singletonCachingService = (ICachingService)bundleContext.getService(ref); - } - if (Debug.DEBUG_CACHE) Debug.println("< AspectJAdaptorFactory.initializeCachingService() singletonCachingService=" + singletonCachingService); - } - - protected ICachingService getCachingService (BaseClassLoader loader, Bundle bundle, IWeavingService weavingService) { - if (Debug.DEBUG_CACHE) Debug.println("> AspectJAdaptorFactory.getCachingService() bundle=" + bundle + ", weavingService=" + weavingService); - ICachingService service = null; - String key = ""; - - if (weavingService != null) { - key = weavingService.getKey(); - } - if (singletonCachingService != null) { - service = singletonCachingService.getInstance((ClassLoader)loader,bundle,key); - } - if (Debug.DEBUG_CACHE) Debug.println("< AspectJAdaptorFactory.getCachingService() service=" + service + ", key='" + key + "'"); - return service; - } - - private void initializePackageAdminService (BundleContext context) { - if (Debug.DEBUG_GENERAL) Debug.println("> AspectJAdaptorFactory.initializePackageAdminService() context=" + context); - - ServiceReference ref = context.getServiceReference(PackageAdmin.class.getName()); - if (ref != null) { - packageAdminService = (PackageAdmin)context.getService(ref); - } - - if (Debug.DEBUG_GENERAL) Debug.println("< AspectJAdaptorFactory.initializePackageAdminService() " + packageAdminService); - } - - public Bundle getHost (Bundle fragment) { - if (Debug.DEBUG_GENERAL) Debug.println("> AspectJAdaptorFactory.getHost() fragment=" + fragment); - - Bundle host = null; - if (packageAdminService != null) host = packageAdminService.getHosts(fragment)[0]; - - if (Debug.DEBUG_GENERAL) Debug.println("< AspectJAdaptorFactory.getHost() " + host); - return host; - } + private BundleContext bundleContext; + + private ServiceTracker cachingServiceTracker; + + private PackageAdmin packageAdminService; + + private SupplementerRegistry supplementerRegistry; + + private ServiceTracker weavingServiceTracker; + + public AspectJAdaptorFactory() { + } + + public void dispose() { + weavingServiceTracker.close(); + if (Debug.DEBUG_WEAVE) + Debug.println("> Closed service tracker for weaving service."); + + cachingServiceTracker.close(); + if (Debug.DEBUG_CACHE) + Debug.println("> Closed service tracker for caching service."); + } + + public Bundle getHost(final Bundle fragment) { + if (Debug.DEBUG_GENERAL) + Debug.println("> AspectJAdaptorFactory.getHost() fragment=" + + fragment); + + Bundle host = null; + if (packageAdminService != null) + host = packageAdminService.getHosts(fragment)[0]; + + if (Debug.DEBUG_GENERAL) + Debug.println("< AspectJAdaptorFactory.getHost() " + host); + return host; + } + + public void initialize(final BundleContext context, + final SupplementerRegistry supplementerRegistry) { + if (Debug.DEBUG_GENERAL) + Debug.println("> AspectJAdaptorFactory.initialize() context=" + + context); + this.bundleContext = context; + this.supplementerRegistry = supplementerRegistry; + + // Service tracker for weaving service + weavingServiceTracker = new ServiceTracker(context, + IWeavingService.class.getName(), + new ServiceTrackerCustomizer() { + + public Object addingService(final ServiceReference reference) { + updateSupplementedBundles(context, supplementerRegistry); + return context.getService(reference); + } + + public void modifiedService( + final ServiceReference reference, + final Object service) { + // Nothing to be done! + } + + public void removedService( + final ServiceReference reference, + final Object service) { + updateSupplementedBundles(context, supplementerRegistry); + context.ungetService(reference); + } + + private void updateSupplementedBundles( + final BundleContext context, + final SupplementerRegistry supplementerRegistry) { + final Iterator supplementedBundlesIterator = supplementerRegistry + .getSupplementedBundles().iterator(); + while (supplementedBundlesIterator.hasNext()) { + final Bundle supplementedBundle = (Bundle) supplementedBundlesIterator + .next(); + supplementerRegistry + .updateInstalledBundle(supplementedBundle); + if (Debug.DEBUG_WEAVE) + Debug.println("> Updated supplemented bundle " + + supplementedBundle.getSymbolicName()); + System.err.println("> Updated supplemented bundle " + + supplementedBundle.getSymbolicName()); + } + } + }); + weavingServiceTracker.open(); + if (Debug.DEBUG_WEAVE) + Debug.println("> Opened service tracker for weaving service."); + + // Service tracker for caching service + cachingServiceTracker = new ServiceTracker(context, + ICachingService.class.getName(), null); + cachingServiceTracker.open(); + if (Debug.DEBUG_CACHE) + Debug.println("> Opened service tracker for caching service."); + + initializePackageAdminService(context); + } + + protected ICachingService getCachingService(final BaseClassLoader loader, + final Bundle bundle, final IWeavingService weavingService) { + if (Debug.DEBUG_CACHE) + Debug.println("> AspectJAdaptorFactory.getCachingService() bundle=" + + bundle + ", weavingService=" + weavingService); + ICachingService service = null; + String key = ""; + + if (weavingService != null) { + key = weavingService.getKey(); + } + final ICachingService singletonCachingService = (ICachingService) cachingServiceTracker + .getService(); + if (singletonCachingService != null) { + service = singletonCachingService.getInstance((ClassLoader) loader, + bundle, key); + } + if (Debug.DEBUG_CACHE) + Debug + .println("< AspectJAdaptorFactory.getCachingService() service=" + + service + ", key='" + key + "'"); + return service; + } + + protected IWeavingService getWeavingService(final BaseClassLoader loader) { + if (Debug.DEBUG_WEAVE) + Debug + .println("> AspectJAdaptorFactory.getWeavingService() baseClassLoader=" + + loader); + + IWeavingService weavingService = null; + final IWeavingService singletonWeavingService = (IWeavingService) weavingServiceTracker + .getService(); + if (singletonWeavingService != null) { + final BaseData baseData = loader.getClasspathManager() + .getBaseData(); + final State state = baseData.getAdaptor().getState(); + final Bundle bundle = baseData.getBundle(); + final BundleDescription bundleDescription = state.getBundle(bundle + .getBundleId()); + weavingService = singletonWeavingService.getInstance( + (ClassLoader) loader, bundle, state, bundleDescription, + supplementerRegistry); + } + if (Debug.DEBUG_WEAVE) + Debug + .println("< AspectJAdaptorFactory.getWeavingService() service=" + + weavingService); + return weavingService; + } + + private void initializePackageAdminService(final BundleContext context) { + if (Debug.DEBUG_GENERAL) + Debug + .println("> AspectJAdaptorFactory.initializePackageAdminService() context=" + + context); + + final ServiceReference ref = context + .getServiceReference(PackageAdmin.class.getName()); + if (ref != null) { + packageAdminService = (PackageAdmin) context.getService(ref); + } + + if (Debug.DEBUG_GENERAL) + Debug + .println("< AspectJAdaptorFactory.initializePackageAdminService() " + + packageAdminService); + } } |