Skip to content

Commit

Permalink
PAXCDI-196: fix NPE in ServiceEventBridge, enabled BundleEventBridge …
Browse files Browse the repository at this point in the history
…and fire events to observers
  • Loading branch information
Локтионов Артем committed Jul 8, 2015
1 parent 19db58c commit 40c353e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ public class BeanBundleImpl {
@Inject
private ServiceEventBridge serviceEventBridge;

@Inject
private BundleEventBridge bundleEventBridge;

/**
* Register OSGi services when the bean is initialized
*/
public void onInitialized(@Observes ContainerInitialized event) {
log.debug("onInitialized {}", bundleContext.getBundle());
serviceEventBridge.start();
componentLifecycleManager.start();
bundleEventBridge.start();
}

/**
Expand All @@ -68,5 +72,6 @@ public void onDestroy() {
log.debug("onDestroy {}", bundleContext.getBundle());
componentLifecycleManager.stop();
serviceEventBridge.stop();
bundleEventBridge.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Void addingBundle(Bundle bundle, BundleEvent bundleEvent) {
return null;
}
log.debug("adding bundle {} {}", bundle);
Event<BundleCdiEvent> childEvent = select(bundleEvent);
Event<BundleCdiEvent> childEvent = select(bundle, bundleEvent);
childEvent.fire(new BundleCdiEvent(bundle, bundleEvent));
return null;
}
Expand All @@ -85,14 +85,16 @@ public Void addingBundle(Bundle bundle, BundleEvent bundleEvent) {
* @return
*/
@SuppressWarnings("serial")
private Event<BundleCdiEvent> select(BundleEvent bundleEvent) {
private Event<BundleCdiEvent> 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<BundleStarted>() {
});

case BundleEvent.STOPPED:
// case BundleEvent.STOPPED:
case BundleEvent.STOPPING:
return event.select(new AnnotationLiteral<BundleStopped>() {
});

Expand All @@ -101,19 +103,31 @@ private Event<BundleCdiEvent> select(BundleEvent bundleEvent) {
}
}
else {
return event;
switch (bundle.getState()) {
case Bundle.ACTIVE:
return event.select(new AnnotationLiteral<BundleStarted>() {
});

case Bundle.RESOLVED:
return event.select(new AnnotationLiteral<BundleStopped>() {
});

default:
return event;
}

}
}

@Override
public void modifiedBundle(Bundle bundle, BundleEvent bundleEvent, Void object) {
Event<BundleCdiEvent> childEvent = select(bundleEvent);
Event<BundleCdiEvent> childEvent = select(bundle, bundleEvent);
childEvent.fire(new BundleCdiEvent(bundle, bundleEvent));
}

@Override
public void removedBundle(Bundle bundle, BundleEvent bundleEvent, Void object) {
Event<BundleCdiEvent> childEvent = select(bundleEvent);
Event<BundleCdiEvent> childEvent = select(bundle, bundleEvent);
childEvent.fire(new BundleCdiEvent(bundle, bundleEvent));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ public void serviceChanged(ServiceEvent serviceEvent) {
}

ServiceReference serviceReference = serviceEvent.getServiceReference();
ServiceObjectsWrapper serviceObjects =
ServiceObjectsWrapper serviceObjects =
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);
}
}
}

Expand Down

0 comments on commit 40c353e

Please sign in to comment.