use of org.osgi.framework.ServiceListener in project aries by apache.
the class BundleEventHookTest method testIgnoreUninstalledOriginBundleInAsyncInstalledEvent.
/*
* Because bundle events are queued for later asynchronous processing while
* the root subsystem is initializing, it is possible to see an installed
* event whose origin bundle has been uninstalled (i.e. the origin bundle's
* revision will be null). These events should result in the installed
* bundle being associated with the root subsystem.
*/
@Test
public void testIgnoreUninstalledOriginBundleInAsyncInstalledEvent() throws Exception {
final Bundle core = getSubsystemCoreBundle();
core.stop();
final Bundle b = bundleContext.installBundle(BUNDLE_B, new FileInputStream(BUNDLE_B));
// Ensure bundle B has a context.
b.start();
final AtomicReference<Bundle> a = new AtomicReference<Bundle>();
bundleContext.addServiceListener(new ServiceListener() {
@SuppressWarnings("unchecked")
@Override
public void serviceChanged(ServiceEvent event) {
if ((event.getType() & (ServiceEvent.REGISTERED | ServiceEvent.MODIFIED)) == 0)
return;
if (a.get() != null)
// We've been here before and already done what needs doing.
return;
ServiceReference sr = (ServiceReference) event.getServiceReference();
bundleContext.getService(sr);
try {
// Queue up the installed event for bundle A using B's context.
a.set(b.getBundleContext().installBundle(BUNDLE_A, new FileInputStream(BUNDLE_A)));
// Ensure the origin bundle will be uninstalled before the event is processed.
b.uninstall();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "(&(objectClass=org.osgi.service.subsystem.Subsystem)(subsystem.id=0)(subsystem.state=RESOLVED))");
try {
// Before the fix, this would fail due to an NPE resulting from a
// null bundle revision.
core.start();
} catch (BundleException e) {
e.printStackTrace();
fail("Subsystems failed to handle an asynchronous bundle installed event after the origin bundle was uninstalled");
}
assertBundleState(a.get(), Bundle.INSTALLED);
assertBundleState(b, Bundle.UNINSTALLED);
Subsystem root = getRootSubsystem();
assertState(Subsystem.State.ACTIVE, root);
assertConstituent(root, a.get().getSymbolicName());
}
use of org.osgi.framework.ServiceListener in project aries by apache.
the class BundleEventHookTest method testNoDeadlockWhenSubsystemsInitializing.
/*
* See https://issues.apache.org/jira/browse/ARIES-982.
*
* When activating, the subsystems bundle must initialize the root subsystem
* along with any persisted subsystems. Part of the root subsystem
* initialization consists of adding all pre-existing bundles as
* constituents. In order to ensure that no bundles are missed, a bundle
* event hook is registered first. The bundle event hook cannot process
* events until the initialization is complete. Another part of
* initialization consists of registering the root subsystem service.
* Therefore, a potential deadlock exists if something reacts to the
* service registration by installing an unmanaged bundle.
*/
@Test
public void testNoDeadlockWhenSubsystemsInitializing() throws Exception {
final Bundle bundle = getSubsystemCoreBundle();
bundle.stop();
final AtomicBoolean completed = new AtomicBoolean(false);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try {
bundleContext.addServiceListener(new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
Future<?> future = executor.submit(new Runnable() {
public void run() {
try {
Bundle a = bundle.getBundleContext().installBundle(BUNDLE_A, new FileInputStream(BUNDLE_A));
completed.set(true);
a.uninstall();
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
future.get();
completed.set(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}, "(&(objectClass=org.osgi.service.subsystem.Subsystem)(subsystem.id=0))");
Future<?> future = executor.submit(new Runnable() {
public void run() {
try {
bundle.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
future.get(3, TimeUnit.SECONDS);
assertTrue("Deadlock detected", completed.get());
} catch (TimeoutException e) {
fail("Deadlock detected");
}
} finally {
executor.shutdownNow();
}
}
use of org.osgi.framework.ServiceListener in project spring-roo by spring-projects.
the class Activator method start.
public void start(final BundleContext context) throws Exception {
startLevelServiceReference = context.getServiceReference(StartLevel.class.getName());
startLevel = (StartLevel) context.getService(startLevelServiceReference);
for (final Bundle bundle : context.getBundles()) {
final String value = bundle.getHeaders().get("Service-Component");
if (value != null) {
List<String> componentDescriptions = Arrays.asList(value.split("\\s*,\\s*"));
for (String desc : componentDescriptions) {
final URL url = bundle.getResource(desc);
process(url);
}
}
}
// Ensure I'm notified of other services changes
final BundleContext myContext = context;
context.addServiceListener(new ServiceListener() {
public void serviceChanged(final ServiceEvent event) {
final ServiceReference sr = event.getServiceReference();
final String className = getClassName(sr, myContext);
if (sr != null) {
myContext.ungetService(sr);
}
if (className == null) {
// Something went wrong
return;
}
if (event.getType() == ServiceEvent.REGISTERED) {
if (requiredImplementations.keySet().contains(className)) {
runningImplementations.add(className);
potentiallyChangeStartLevel();
}
} else if (event.getType() == ServiceEvent.UNREGISTERING) {
if (runningImplementations.contains(className)) {
runningImplementations.remove(className);
potentiallyChangeStartLevel();
}
}
}
});
// Now identify if any services I was interested in are already running
for (final String requiredService : requiredImplementations.keySet()) {
final String correspondingInterface = requiredImplementations.get(requiredService);
final ServiceReference[] srs = context.getServiceReferences(correspondingInterface, null);
if (srs != null) {
for (final ServiceReference sr : srs) {
final String className = getClassName(sr, context);
if (className == null) {
// Something went wrong
continue;
}
if (requiredImplementations.keySet().contains(className)) {
runningImplementations.add(className);
}
}
}
}
// Potentially change the start level, now that we've added all the
// known started services
potentiallyChangeStartLevel();
}
Aggregations