Search in sources :

Example 11 with BundleListener

use of org.osgi.framework.BundleListener in project bnd by bndtools.

the class GC method start.

@Override
public void start(BundleContext context) throws Exception {
    this.context = context;
    context.addBundleListener(new BundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.UNINSTALLED) {
                Bundle b = event.getBundle();
                String embedded = b.getHeaders().get("Bnd-Embedded");
                if (embedded != null) {
                    uninstalled(b);
                }
            }
        }
    });
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Example 12 with BundleListener

use of org.osgi.framework.BundleListener in project sling by apache.

the class MockBundleContextTest method testBundleListener.

@Test
public void testBundleListener() throws Exception {
    BundleListener bundleListener = mock(BundleListener.class);
    BundleEvent bundleEvent = mock(BundleEvent.class);
    bundleContext.addBundleListener(bundleListener);
    MockOsgi.sendBundleEvent(bundleContext, bundleEvent);
    verify(bundleListener).bundleChanged(bundleEvent);
    bundleContext.removeBundleListener(bundleListener);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) Test(org.junit.Test)

Example 13 with BundleListener

use of org.osgi.framework.BundleListener in project felix by apache.

the class BundleTrackerTest method testTracking.

@Test
public void testTracking() {
    Bundle bundle = mock(Bundle.class);
    when(bundle.getState()).thenReturn(Bundle.ACTIVE);
    BundleContext context = Mockito.mock(BundleContext.class);
    @SuppressWarnings("unchecked") BundleTrackerCustomizer<Bundle> customizer = mock(BundleTrackerCustomizer.class);
    when(customizer.addingBundle(Mockito.eq(bundle), Mockito.any(BundleEvent.class))).thenReturn(bundle);
    BundleTracker<Bundle> tracker = new BundleTracker<Bundle>(context, Bundle.ACTIVE | Bundle.STARTING, customizer);
    tracker.open();
    ArgumentCaptor<BundleListener> listenerCaptor = ArgumentCaptor.forClass(BundleListener.class);
    verify(context).addBundleListener(listenerCaptor.capture());
    BundleListener listener = listenerCaptor.getValue();
    BundleEvent startedEvent = new BundleEvent(BundleEvent.STARTED, bundle);
    listener.bundleChanged(startedEvent);
    verify(customizer).addingBundle(bundle, startedEvent);
    when(bundle.getState()).thenReturn(Bundle.INSTALLED);
    BundleEvent stoppedEvent = new BundleEvent(BundleEvent.STOPPED, bundle);
    listener.bundleChanged(stoppedEvent);
    verify(customizer).removedBundle(bundle, stoppedEvent, bundle);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 14 with BundleListener

use of org.osgi.framework.BundleListener in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testStartLevelSorting.

public void testStartLevelSorting() throws IOException, InterruptedException {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map configuration = new HashMap();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = null;
    final int numBundles = 200;
    final File[] testBundleFiles = createBundles(new File(config, "testBundles"), numBundles);
    try {
        equinox = new Equinox(configuration);
        equinox.start();
        final List<Bundle> testBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        final List<Bundle> startedBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        for (int i = 0; i < numBundles / 2; i++) {
            Bundle b = equinox.getBundleContext().installBundle("reference:file:///" + testBundleFiles[i].getAbsolutePath());
            b.adapt(BundleStartLevel.class).setStartLevel(i + 2);
            testBundles.add(b);
            b.start();
        }
        final Equinox finalEquinox = equinox;
        BundleListener initialListener = new SynchronousBundleListener() {

            AtomicBoolean reverseStartLevel = new AtomicBoolean();

            AtomicBoolean installBundles = new AtomicBoolean();

            @Override
            public void bundleChanged(BundleEvent event) {
                Bundle eBundle = event.getBundle();
                String bsn = eBundle.getSymbolicName();
                if (!bsn.startsWith("bundle-b") || event.getType() != BundleEvent.STARTED) {
                    return;
                }
                startedBundles.add(eBundle);
                if (reverseStartLevel.compareAndSet(false, true)) {
                    for (int i = numBundles / 4, j = numBundles; i < numBundles / 2; i++, j--) {
                        BundleStartLevel tbsl = testBundles.get(i).adapt(BundleStartLevel.class);
                        tbsl.setStartLevel(j + 2 + numBundles);
                    }
                } else if (installBundles.compareAndSet(false, true)) {
                    for (int i = numBundles / 2; i < numBundles; i++) {
                        try {
                            Bundle b = finalEquinox.getBundleContext().installBundle("reference:file:///" + testBundleFiles[i].getAbsolutePath());
                            b.adapt(BundleStartLevel.class).setStartLevel(i + 2);
                            testBundles.add(b);
                            b.start();
                        } catch (BundleException e) {
                        // do nothing
                        }
                    }
                }
            }
        };
        equinox.getBundleContext().addBundleListener(initialListener);
        long startTime = System.currentTimeMillis();
        final CountDownLatch waitForStartLevel = new CountDownLatch(1);
        equinox.adapt(FrameworkStartLevel.class).setStartLevel(numBundles * 3, new FrameworkListener() {

            @Override
            public void frameworkEvent(FrameworkEvent event) {
                waitForStartLevel.countDown();
            }
        });
        waitForStartLevel.await(20, TimeUnit.SECONDS);
        System.out.println("Start time: " + (System.currentTimeMillis() - startTime));
        assertEquals("Did not finish start level setting.", 0, waitForStartLevel.getCount());
        assertEquals("Did not install all the expected bundles.", numBundles, testBundles.size());
        List<Bundle> expectedStartOrder = new ArrayList<Bundle>();
        for (int i = 0; i < numBundles / 4; i++) {
            expectedStartOrder.add(testBundles.get(i));
        }
        for (int i = numBundles / 2; i < numBundles; i++) {
            expectedStartOrder.add(testBundles.get(i));
        }
        for (int i = (numBundles / 2) - 1; i >= numBundles / 4; i--) {
            expectedStartOrder.add(testBundles.get(i));
        }
        assertEquals("Size on expected order is wrong.", numBundles, expectedStartOrder.size());
        for (int i = 0; i < numBundles; i++) {
            assertEquals("Wrong bundle at: " + i, expectedStartOrder.get(i), startedBundles.get(i));
        }
        equinox.getBundleContext().removeBundleListener(initialListener);
        final List<Bundle> stoppedBundles = Collections.synchronizedList(new ArrayList<Bundle>());
        BundleListener shutdownListener = new SynchronousBundleListener() {

            AtomicBoolean reverseStartLevel = new AtomicBoolean();

            AtomicBoolean uninstallBundles = new AtomicBoolean();

            AtomicBoolean inUninstall = new AtomicBoolean();

            @Override
            public void bundleChanged(BundleEvent event) {
                if (inUninstall.get()) {
                    return;
                }
                Bundle eBundle = event.getBundle();
                String bsn = eBundle.getSymbolicName();
                if (!bsn.startsWith("bundle-b") || event.getType() != BundleEvent.STOPPED) {
                    return;
                }
                stoppedBundles.add(eBundle);
                if (reverseStartLevel.compareAndSet(false, true)) {
                    for (int i = numBundles / 2, j = numBundles - 1; i < numBundles; i++, j--) {
                        BundleStartLevel tbsl = testBundles.get(i).adapt(BundleStartLevel.class);
                        tbsl.setStartLevel(j + 2);
                    }
                } else if (uninstallBundles.compareAndSet(false, true)) {
                    for (int i = 0; i < numBundles / 4; i++) {
                        try {
                            inUninstall.set(true);
                            testBundles.get(i).uninstall();
                        } catch (BundleException e) {
                        // do nothing
                        } finally {
                            inUninstall.set(false);
                        }
                    }
                }
            }
        };
        equinox.getBundleContext().addBundleListener(shutdownListener);
        List<Bundle> expectedStopOrder = new ArrayList<Bundle>(expectedStartOrder);
        Collections.reverse(expectedStopOrder);
        Collections.reverse(expectedStopOrder.subList(numBundles / 4, 3 * (numBundles / 4)));
        expectedStopOrder = new ArrayList(expectedStopOrder.subList(0, 3 * (numBundles / 4)));
        long stopTime = System.currentTimeMillis();
        equinox.stop();
        equinox.waitForStop(20000);
        System.out.println("Stop time: " + (System.currentTimeMillis() - stopTime));
        assertEquals("Size on expected order is wrong.", expectedStopOrder.size(), stoppedBundles.size());
        for (int i = 0; i < expectedStopOrder.size(); i++) {
            assertEquals("Wrong bundle at: " + i, expectedStopOrder.get(i), stoppedBundles.get(i));
        }
    } catch (BundleException e) {
        fail("Failed init", e);
    } finally {
        try {
            if (equinox != null) {
                equinox.stop();
                equinox.waitForStop(1000);
            }
        } catch (BundleException e) {
            fail("Failed to stop framework.", e);
        } catch (InterruptedException e) {
            fail("Failed to stop framework.", e);
        }
    }
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Equinox(org.eclipse.osgi.launch.Equinox) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) BundleException(org.osgi.framework.BundleException) FrameworkListener(org.osgi.framework.FrameworkListener) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 15 with BundleListener

use of org.osgi.framework.BundleListener in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBug490902.

public void testBug490902() throws BundleException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    final Bundle a1 = installer.installBundle("test.bug490902.a");
    final Bundle b1 = installer.installBundle("test.bug490902.b");
    installer.resolveBundles(new Bundle[] { a1, b1 });
    final CountDownLatch startingB = new CountDownLatch(1);
    final CountDownLatch endedSecondThread = new CountDownLatch(1);
    BundleListener delayB1 = new SynchronousBundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getBundle() == b1 && BundleEvent.STARTING == event.getType()) {
                try {
                    startingB.countDown();
                    System.out.println(getName() + ": Delaying now ...");
                    Thread.sleep(15000);
                    System.out.println(getName() + ": Done delaying.");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    };
    getContext().addBundleListener(delayB1);
    try {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    System.out.println(getName() + ": Initial load test.");
                    a1.loadClass("test.bug490902.a.TestLoadA1").newInstance();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }, "Initial load test thread.").start();
        startingB.await();
        Thread secondThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    System.out.println(getName() + ": Second load test.");
                    a1.loadClass("test.bug490902.a.TestLoadA1").newInstance();
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    endedSecondThread.countDown();
                }
            }
        }, "Second load test thread.");
        secondThread.start();
        // hack to make sure secondThread is in the middle of Class.forName
        Thread.sleep(10000);
        System.out.println(getName() + ": About to interrupt:" + secondThread.getName());
        secondThread.interrupt();
        endedSecondThread.await();
        a1.loadClass("test.bug490902.a.TestLoadA1").newInstance();
    } finally {
        getContext().removeBundleListener(delayB1);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) CountDownLatch(java.util.concurrent.CountDownLatch) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Aggregations

BundleListener (org.osgi.framework.BundleListener)18 BundleEvent (org.osgi.framework.BundleEvent)14 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)9 Bundle (org.osgi.framework.Bundle)8 File (java.io.File)5 BundleContext (org.osgi.framework.BundleContext)5 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 FileInputStream (java.io.FileInputStream)3 Properties (java.util.Properties)3 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 Logger (org.apache.aries.jmx.Logger)2 Git (org.eclipse.jgit.api.Git)2