Search in sources :

Example 11 with SynchronousBundleListener

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompositeBundle(org.osgi.service.framework.CompositeBundle) Bundle(org.osgi.framework.Bundle) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) AriesApplicationManager(org.apache.aries.application.management.AriesApplicationManager) AriesApplication(org.apache.aries.application.management.AriesApplication) BundleEvent(org.osgi.framework.BundleEvent) PerClass(org.ops4j.pax.exam.spi.reactors.PerClass) CompositeBundle(org.osgi.service.framework.CompositeBundle) File(java.io.File) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.itest.AbstractIntegrationTest)

Example 12 with SynchronousBundleListener

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);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Subsystem(org.osgi.service.subsystem.Subsystem) BundleEvent(org.osgi.framework.BundleEvent) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest)

Example 13 with SynchronousBundleListener

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);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) IOException(java.io.IOException) Vector(java.util.Vector) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 14 with SynchronousBundleListener

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();
        }
    }
}
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 15 with SynchronousBundleListener

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);
    }
}
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)

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