use of org.osgi.framework.SynchronousBundleListener in project felix by apache.
the class BundleContextImpl method addBundleListener.
public void addBundleListener(BundleListener l) {
checkValidity();
// CONCURRENCY NOTE: This is a check-then-act situation, but
// internally the event dispatcher double checks whether or not
// the bundle context is valid before adding the service listener
// while holding the event queue lock, so it will either succeed
// or fail.
Object sm = System.getSecurityManager();
if (sm != null) {
if (l instanceof SynchronousBundleListener) {
((SecurityManager) sm).checkPermission(new AdminPermission(m_bundle, AdminPermission.LISTENER));
}
}
m_felix.addBundleListener(m_bundle, l);
}
use of org.osgi.framework.SynchronousBundleListener in project felix by apache.
the class BundleContextImpl method removeBundleListener.
public void removeBundleListener(BundleListener l) {
checkValidity();
// CONCURRENCY NOTE: This is a check-then-act situation,
// but we ignore it since the time window is small and
// the result is the same as if the calling thread had
// won the race condition.
Object sm = System.getSecurityManager();
if (sm != null) {
if (l instanceof SynchronousBundleListener) {
((SecurityManager) sm).checkPermission(new AdminPermission(m_bundle, AdminPermission.LISTENER));
}
}
m_felix.removeBundleListener(m_bundle, l);
}
use of org.osgi.framework.SynchronousBundleListener in project felix by apache.
the class ConcurrentBundleUpdateTest method testConcurrentBundleCycleUpdate.
public void testConcurrentBundleCycleUpdate() 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: " + ConcurrentBundleUpdaterCycleActivator.class.getName() + "\n\n";
final BundleImpl updater = (BundleImpl) felix.getBundleContext().installBundle(createBundle(mf, ConcurrentBundleUpdaterCycleActivator.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) {
step.release();
}
}
}.start();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
felix.getBundleContext().removeBundleListener(listenerStarting);
assertEquals(Bundle.STARTING, updater.getState());
assertEquals(0, step.availablePermits());
((Runnable) updater.getActivator()).run();
assertTrue(step.tryAcquire(1, TimeUnit.SECONDS));
assertEquals(Bundle.RESOLVED, updater.getState());
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);
}
}
use of org.osgi.framework.SynchronousBundleListener in project sling by apache.
the class BundleInstallUpgradeExceptionRetryTest method testUpdateSuccedsWithLessThanMaxRetrys.
@Test
public void testUpdateSuccedsWithLessThanMaxRetrys() 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(3);
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"))) {
if (ref.get() == null) {
try {
event.getBundle().start();
ref.set(bundleContext.registerService(AtomicInteger.class, counter, null));
} catch (BundleException e) {
}
} 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, "2.0.0", Bundle.ACTIVE);
} finally {
if (ref.get() != null) {
ref.get().unregister();
}
}
}
use of org.osgi.framework.SynchronousBundleListener in project rt.equinox.framework by eclipse.
the class ClassLoadingBundleTests method testBug258659_03.
public void testBug258659_03() throws Exception {
// install a bundle
// $NON-NLS-1$
Bundle test = installer.installBundle("test");
SynchronousBundleListener testLoadClassListener = new SynchronousBundleListener() {
public void bundleChanged(BundleEvent event) {
if (event.getType() == BundleEvent.STARTED)
try {
event.getBundle().stop();
} catch (BundleException e) {
simpleResults.addEvent(e);
}
}
};
OSGiTestsActivator.getContext().addBundleListener(testLoadClassListener);
try {
test.start();
Object[] expectedEvents = new Object[2];
expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, test);
expectedEvents[1] = new BundleEvent(BundleEvent.STOPPED, test);
Object[] actualEvents = simpleResults.getResults(2);
compareResults(expectedEvents, actualEvents);
} finally {
OSGiTestsActivator.getContext().removeBundleListener(testLoadClassListener);
}
}
Aggregations