use of org.osgi.framework.SynchronousBundleListener in project aries by apache.
the class IsolatedRuntimeTest method testFrameworkResolvedBeforeInnerBundlesStart.
@Test
@Ignore
public void testFrameworkResolvedBeforeInnerBundlesStart() throws Exception {
/*
* Lazy bundles have in the past triggered recursive bundle trackers to handle them before
* the composite bundle framework was even resolved. In such a case the below loadClass
* operation on a class that depends on a class imported from the outside of the composite
* will fail with an NPE.
*/
final AtomicBoolean loadedClass = new AtomicBoolean(false);
context().addBundleListener(new SynchronousBundleListener() {
public void bundleChanged(BundleEvent event) {
Bundle b = event.getBundle();
if (event.getType() == BundleEvent.STARTING || event.getType() == BundleEvent.LAZY_ACTIVATION) {
if (b.getEntry("org/apache/aries/isolated/sample/SharedImpl.class") != null) {
try {
Class<?> cl = b.loadClass("org.apache.aries.isolated.sample.SharedImpl");
cl.newInstance();
loadedClass.set(true);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} else if (event.getType() == BundleEvent.INSTALLED && b instanceof CompositeBundle) {
((CompositeBundle) b).getCompositeFramework().getBundleContext().addBundleListener(this);
}
}
});
AriesApplicationManager manager = context().getService(AriesApplicationManager.class);
AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("test2.eba")));
AriesApplicationContext ctx = manager.install(app);
try {
ctx.start();
app = ctx.getApplication();
assertEquals(1, app.getDeploymentMetadata().getApplicationDeploymentContents().size());
assertEquals(1, app.getDeploymentMetadata().getApplicationProvisionBundles().size());
assertTrue(loadedClass.get());
} finally {
manager.uninstall(ctx);
}
}
use of org.osgi.framework.SynchronousBundleListener in project aries by apache.
the class Aries1399Test method testBundleEventOrder.
@Test
public void testBundleEventOrder() throws Exception {
Subsystem root = getRootSubsystem();
BundleContext context = root.getBundleContext();
final List<BundleEvent> events = Collections.synchronizedList(new ArrayList<BundleEvent>());
context.addBundleListener(new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
events.add(event);
}
});
Bundle bundle = context.installBundle("bundle", TinyBundles.bundle().set(Constants.BUNDLE_SYMBOLICNAME, "bundle").build());
try {
bundle.start();
// INSTALLED, RESOLVED, STARTING, STARTED
assertEquals(4, events.size());
assertEquals(BundleEvent.INSTALLED, events.get(0).getType());
assertEquals(BundleEvent.RESOLVED, events.get(1).getType());
assertEquals(BundleEvent.STARTING, events.get(2).getType());
assertEquals(BundleEvent.STARTED, events.get(3).getType());
} finally {
uninstallSilently(bundle);
}
}
use of org.osgi.framework.SynchronousBundleListener in project bnd by bndtools.
the class Activator method automatic.
void automatic(File reportDir) throws IOException {
final List<Bundle> queue = new Vector<Bundle>();
if (!reportDir.exists() && !reportDir.mkdirs()) {
throw new IOException("Could not create directory " + reportDir);
}
trace("using %s", reportDir);
trace("adding Bundle Listener for getting test bundle events");
context.addBundleListener(new SynchronousBundleListener() {
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.STARTED) {
checkBundle(queue, event.getBundle());
}
}
});
for (Bundle b : context.getBundles()) {
checkBundle(queue, b);
}
trace("starting queue");
int result = 0;
outer: while (active) {
Bundle bundle;
synchronized (queue) {
while (queue.isEmpty() && active) {
try {
queue.wait();
} catch (InterruptedException e) {
trace("tests bundle queue interrupted");
thread.interrupt();
break outer;
}
}
}
try {
bundle = queue.remove(0);
trace("received bundle to test: %s", bundle.getLocation());
try (Writer report = getReportWriter(reportDir, bundle)) {
trace("test will run");
result += test(bundle, (String) bundle.getHeaders().get(aQute.bnd.osgi.Constants.TESTCASES), report);
trace("test ran");
if (queue.isEmpty() && !continuous) {
trace("queue " + queue);
System.exit(result);
}
}
} catch (Exception e) {
error("Not sure what happened anymore %s", e);
System.exit(-2);
}
}
}
use of org.osgi.framework.SynchronousBundleListener in project sling by apache.
the class BundleInstallUpgradeExceptionRetryTest method testUpdateFailsWithMoreThanMaxRetrys.
@Test
public void testUpdateFailsWithMoreThanMaxRetrys() throws Exception {
// install version 1.1 and fail activation with exception all attempts
final Object listener = this.startObservingBundleEvents();
final AtomicReference<ServiceRegistration<AtomicInteger>> ref = new AtomicReference<ServiceRegistration<AtomicInteger>>(null);
final AtomicInteger counter = new AtomicInteger(5);
bundleContext.addBundleListener(new SynchronousBundleListener() {
@Override
public void bundleChanged(org.osgi.framework.BundleEvent event) {
if (event.getType() == org.osgi.framework.BundleEvent.STOPPED && event.getBundle().getSymbolicName().equals(symbolicName) && event.getBundle().getVersion().equals(new Version("1.0.0"))) {
System.out.println(event.getSource() + " " + event.getType());
Thread.dumpStack();
if (ref.get() == null) {
try {
event.getBundle().start();
ref.set(bundleContext.registerService(AtomicInteger.class, counter, null));
} catch (BundleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
ref.getAndSet(null).unregister();
if (counter.get() == 0) {
bundleContext.removeBundleListener(this);
}
}
}
}
});
installer.updateResources(URL_SCHEME, getInstallableResource(createTestBundle(new Version("2.0.0"))), null);
try {
long time = 0;
while (counter.get() >= 0 && time < 1000) {
sleep(100);
time += 100;
}
assertBundle("After installing", symbolicName, "1.0.0", Bundle.ACTIVE);
} finally {
if (ref.get() != null) {
ref.get().unregister();
}
}
}
use of org.osgi.framework.SynchronousBundleListener in project felix by apache.
the class ConcurrentBundleUpdateTest method testConcurrentBundleUpdate.
public void testConcurrentBundleUpdate() throws Exception {
Map params = new HashMap();
params.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.4.0," + "org.osgi.service.packageadmin; version=1.2.0," + "org.osgi.service.startlevel; version=1.1.0," + "org.osgi.util.tracker; version=1.3.3," + "org.osgi.service.url; version=1.0.0");
File cacheDir = File.createTempFile("felix-cache", ".dir");
cacheDir.delete();
cacheDir.mkdirs();
String cache = cacheDir.getPath();
params.put("felix.cache.profiledir", cache);
params.put("felix.cache.dir", cache);
params.put(Constants.FRAMEWORK_STORAGE, cache);
try {
Felix felix = new Felix(params);
try {
felix.init();
felix.start();
String mf = "Bundle-SymbolicName: test.concurrent.bundleupdate\n" + "Bundle-Version: 1.0.0\n" + "Bundle-ManifestVersion: 2\n" + "Import-Package: org.osgi.framework\n" + "Manifest-Version: 1.0\n" + "Bundle-Activator: " + ConcurrentBundleUpdaterActivator.class.getName() + "\n\n";
final BundleImpl updater = (BundleImpl) felix.getBundleContext().installBundle(createBundle(mf, ConcurrentBundleUpdaterActivator.class).toURI().toURL().toString());
final Semaphore step = new Semaphore(0);
SynchronousBundleListener listenerStarting = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
if (event.getBundle().equals(updater) && event.getType() == BundleEvent.STARTING) {
step.release();
}
}
};
felix.getBundleContext().addBundleListener(listenerStarting);
new Thread() {
public void run() {
try {
updater.start();
} catch (Exception ex) {
}
}
}.start();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
felix.getBundleContext().removeBundleListener(listenerStarting);
assertEquals(Bundle.STARTING, updater.getState());
assertEquals(0, step.availablePermits());
new Thread() {
public void run() {
try {
step.release();
updater.update();
step.release();
} catch (Exception ex) {
}
}
}.start();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
SynchronousBundleListener listenerStarted = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
if (event.getBundle().equals(updater) && event.getType() == BundleEvent.STARTED) {
step.release();
}
if (event.getBundle().equals(updater) && event.getType() == BundleEvent.STOPPING) {
step.release();
}
}
};
felix.getBundleContext().addBundleListener(listenerStarted);
((Runnable) updater.getActivator()).run();
assertTrue(step.tryAcquire(2, 1, TimeUnit.SECONDS));
felix.getBundleContext().removeBundleListener(listenerStarted);
assertEquals(0, step.availablePermits());
assertEquals(Bundle.STOPPING, updater.getState());
felix.getBundleContext().addBundleListener(listenerStarting);
((Runnable) updater.getActivator()).run();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
felix.getBundleContext().removeBundleListener(listenerStarting);
assertEquals(Bundle.STARTING, updater.getState());
((Runnable) updater.getActivator()).run();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
assertEquals(Bundle.ACTIVE, updater.getState());
((Runnable) updater.getActivator()).run();
updater.uninstall();
assertEquals(Bundle.UNINSTALLED, updater.getState());
try {
updater.update();
fail("Expected exception on update of uninstalled bundle");
} catch (IllegalStateException expected) {
}
} finally {
felix.stop();
felix.waitForStop(1000);
}
} finally {
delete(cacheDir);
}
}
Aggregations