Search in sources :

Example 1 with SynchronousBundleListener

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);
}
Also used : AdminPermission(org.osgi.framework.AdminPermission) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 2 with SynchronousBundleListener

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);
}
Also used : AdminPermission(org.osgi.framework.AdminPermission) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 3 with SynchronousBundleListener

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);
    }
}
Also used : HashMap(java.util.HashMap) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) BundleEvent(org.osgi.framework.BundleEvent) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 4 with SynchronousBundleListener

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();
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 5 with SynchronousBundleListener

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);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleException(org.osgi.framework.BundleException) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Aggregations

SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)21 BundleEvent (org.osgi.framework.BundleEvent)17 Bundle (org.osgi.framework.Bundle)13 File (java.io.File)8 BundleException (org.osgi.framework.BundleException)8 BundleListener (org.osgi.framework.BundleListener)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Equinox (org.eclipse.osgi.launch.Equinox)3 BundleContext (org.osgi.framework.BundleContext)3 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2