Search in sources :

Example 26 with ServiceListener

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());
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) Subsystem(org.osgi.service.subsystem.Subsystem) AtomicReference(java.util.concurrent.atomic.AtomicReference) BundleException(org.osgi.framework.BundleException) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 27 with ServiceListener

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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 28 with ServiceListener

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();
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceListener (org.osgi.framework.ServiceListener)28 ServiceEvent (org.osgi.framework.ServiceEvent)21 ServiceReference (org.osgi.framework.ServiceReference)16 Bundle (org.osgi.framework.Bundle)8 Test (org.junit.Test)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 IOException (java.io.IOException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 ArrayList (java.util.ArrayList)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BundleContext (org.osgi.framework.BundleContext)4 BundleException (org.osgi.framework.BundleException)4 Hashtable (java.util.Hashtable)3 Dictionary (java.util.Dictionary)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ObjectName (javax.management.ObjectName)2