diff --git a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BeanBundleImpl.java b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BeanBundleImpl.java index 78c84c5c..fa147f31 100644 --- a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BeanBundleImpl.java +++ b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BeanBundleImpl.java @@ -51,6 +51,9 @@ public class BeanBundleImpl { @Inject private ServiceEventBridge serviceEventBridge; + @Inject + private BundleEventBridge bundleEventBridge; + /** * Register OSGi services when the bean is initialized */ @@ -58,6 +61,7 @@ public void onInitialized(@Observes ContainerInitialized event) { log.debug("onInitialized {}", bundleContext.getBundle()); serviceEventBridge.start(); componentLifecycleManager.start(); + bundleEventBridge.start(); } /** @@ -68,5 +72,6 @@ public void onDestroy() { log.debug("onDestroy {}", bundleContext.getBundle()); componentLifecycleManager.stop(); serviceEventBridge.stop(); + bundleEventBridge.stop(); } } diff --git a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BundleEventBridge.java b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BundleEventBridge.java index 1bf9b639..de19e8e3 100644 --- a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BundleEventBridge.java +++ b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/BundleEventBridge.java @@ -75,7 +75,7 @@ public Void addingBundle(Bundle bundle, BundleEvent bundleEvent) { return null; } log.debug("adding bundle {} {}", bundle); - Event childEvent = select(bundleEvent); + Event childEvent = select(bundle, bundleEvent); childEvent.fire(new BundleCdiEvent(bundle, bundleEvent)); return null; } @@ -85,14 +85,16 @@ public Void addingBundle(Bundle bundle, BundleEvent bundleEvent) { * @return */ @SuppressWarnings("serial") - private Event select(BundleEvent bundleEvent) { + private Event select(Bundle bundle, BundleEvent bundleEvent) { if (bundleEvent != null) { switch (bundleEvent.getType()) { - case BundleEvent.STARTED: + case BundleEvent.STARTING: + // case BundleEvent.STARTED: return event.select(new AnnotationLiteral() { }); - case BundleEvent.STOPPED: + // case BundleEvent.STOPPED: + case BundleEvent.STOPPING: return event.select(new AnnotationLiteral() { }); @@ -101,19 +103,31 @@ private Event select(BundleEvent bundleEvent) { } } else { - return event; + switch (bundle.getState()) { + case Bundle.ACTIVE: + return event.select(new AnnotationLiteral() { + }); + + case Bundle.RESOLVED: + return event.select(new AnnotationLiteral() { + }); + + default: + return event; + } + } } @Override public void modifiedBundle(Bundle bundle, BundleEvent bundleEvent, Void object) { - Event childEvent = select(bundleEvent); + Event childEvent = select(bundle, bundleEvent); childEvent.fire(new BundleCdiEvent(bundle, bundleEvent)); } @Override public void removedBundle(Bundle bundle, BundleEvent bundleEvent, Void object) { - Event childEvent = select(bundleEvent); + Event childEvent = select(bundle, bundleEvent); childEvent.fire(new BundleCdiEvent(bundle, bundleEvent)); } } diff --git a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/ServiceEventBridge.java b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/ServiceEventBridge.java index 2359a59b..30d56eaf 100644 --- a/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/ServiceEventBridge.java +++ b/pax-cdi-extension/src/main/java/org/ops4j/pax/cdi/extension/impl/ServiceEventBridge.java @@ -63,16 +63,18 @@ public void serviceChanged(ServiceEvent serviceEvent) { createServiceObjectsWrapper(bundleContext, serviceReference); Object service = serviceObjects.getService(); - try { - Class klass = service.getClass(); - event.select(klass, qualifier).fire(service); - - TypeLiteral literal = new ParameterizedTypeLiteral(ServiceCdiEvent.class, klass); - ServiceCdiEvent cdiEvent = new ServiceCdiEvent(serviceReference, service); - event.select(literal, qualifier).fire(cdiEvent); - } - finally { - serviceObjects.ungetService(service); + if (service != null) { + try { + Class klass = service.getClass(); + event.select(klass, qualifier).fire(service); + + TypeLiteral literal = new ParameterizedTypeLiteral(ServiceCdiEvent.class, klass); + ServiceCdiEvent cdiEvent = new ServiceCdiEvent(serviceReference, service); + event.select(literal, qualifier).fire(cdiEvent); + } + finally { + serviceObjects.ungetService(service); + } } }