Search in sources :

Example 41 with BundleEvent

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

the class LogSupportTest method testBundleChanges.

@Test
public void testBundleChanges() throws Exception {
    logSupport.bundleChanged(new BundleEvent(BundleEvent.INSTALLED, bundle));
    Mockito.verify(testLogger).info("BundleEvent INSTALLED", (Throwable) null);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 42 with BundleEvent

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

the class ModelAdapterFactoryUtil method addModelsForPackage.

/**
     * Scan classpaths for given package name (and sub packages) to scan for and
     * register all classes with @Model annotation.
     * @param packageName Java package name
     */
public static void addModelsForPackage(BundleContext bundleContext, Class... classes) {
    Bundle bundle = new ModelsPackageBundle(classes, Bundle.ACTIVE, bundleContext);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 43 with BundleEvent

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

the class SystemBundleTests method testBug414070.

public void testBug414070() throws BundleException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    equinox.init();
    BundleContext systemContext = equinox.getBundleContext();
    Bundle systemBundle = systemContext.getBundle();
    // $NON-NLS-1$
    Bundle chainTest = systemContext.installBundle(installer.getBundleLocation("chain.test"));
    // $NON-NLS-1$
    final Bundle chainTestD = systemContext.installBundle(installer.getBundleLocation("chain.test.d"));
    // $NON-NLS-1$
    Bundle chainTestA = systemContext.installBundle(installer.getBundleLocation("chain.test.a"));
    // $NON-NLS-1$
    Bundle chainTestB = systemContext.installBundle(installer.getBundleLocation("chain.test.b"));
    // $NON-NLS-1$
    Bundle chainTestC = systemContext.installBundle(installer.getBundleLocation("chain.test.c"));
    systemContext.registerService(WeavingHook.class, new WeavingHook() {

        public void weave(WovenClass wovenClass) {
            if (!chainTestD.equals(wovenClass.getBundleWiring().getBundle()))
                return;
            if (!"chain.test.d.DMultipleChain1".equals(wovenClass.getClassName()))
                return;
            List dynamicImports = wovenClass.getDynamicImports();
            dynamicImports.add("*");
        }
    }, null);
    equinox.start();
    // $NON-NLS-1$
    chainTest.loadClass("chain.test.TestMultiChain").newInstance();
    // force a dynamic wire to cause a cycle
    chainTestD.loadClass("chain.test.a.AMultiChain1");
    // make sure all bundles are active now
    assertEquals("A is not active.", Bundle.ACTIVE, chainTestA.getState());
    assertEquals("B is not active.", Bundle.ACTIVE, chainTestB.getState());
    assertEquals("C is not active.", Bundle.ACTIVE, chainTestC.getState());
    assertEquals("D is not active.", Bundle.ACTIVE, chainTestD.getState());
    // record STOPPING order
    final List<Bundle> stoppingOrder = new ArrayList<Bundle>();
    systemContext.addBundleListener(new SynchronousBundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            if (event.getType() == BundleEvent.STOPPING) {
                stoppingOrder.add(event.getBundle());
            }
        }
    });
    equinox.stop();
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("Unexpected interruption.", e);
    }
    List<Bundle> expectedOrder = Arrays.asList(systemBundle, chainTest, chainTestA, chainTestB, chainTestC, chainTestD);
    assertEquals("Wrong stopping order", expectedOrder.toArray(), stoppingOrder.toArray());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Equinox(org.eclipse.osgi.launch.Equinox) BundleEvent(org.osgi.framework.BundleEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) BundleContext(org.osgi.framework.BundleContext)

Example 44 with BundleEvent

use of org.osgi.framework.BundleEvent 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 45 with BundleEvent

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

the class ClassLoadingBundleTests method testOSGiLazyStart.

public void testOSGiLazyStart() throws Exception {
    // $NON-NLS-1$
    Bundle osgi = installer.installBundle("osgi.lazystart");
    // $NON-NLS-1$
    Bundle osgiA = installer.installBundle("osgi.lazystart.a");
    // $NON-NLS-1$
    Bundle osgiB = installer.installBundle("osgi.lazystart.b");
    // $NON-NLS-1$
    Bundle osgiC = installer.installBundle("osgi.lazystart.c");
    // $NON-NLS-1$
    assertTrue("osgi lazy start resolve", installer.resolveBundles(new Bundle[] { osgi, osgiA, osgiB, osgiC }));
    // $NON-NLS-1$
    ((ITestRunner) osgi.loadClass("osgi.lazystart.LazySimple").newInstance()).testIt();
    Object[] expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiA);
    Object[] actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
    // $NON-NLS-1$
    ((ITestRunner) osgi.loadClass("osgi.lazystart.LazyExclude1").newInstance()).testIt();
    // $NON-NLS-1$
    assertTrue("exceptions no event", simpleResults.getResults(0).length == 0);
    // $NON-NLS-1$
    ((ITestRunner) osgi.loadClass("osgi.lazystart.LazyExclude2").newInstance()).testIt();
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiB);
    actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
    // $NON-NLS-1$
    ((ITestRunner) osgi.loadClass("osgi.lazystart.LazyInclude1").newInstance()).testIt();
    // $NON-NLS-1$
    assertTrue("exceptions no event", simpleResults.getResults(0).length == 0);
    // $NON-NLS-1$
    ((ITestRunner) osgi.loadClass("osgi.lazystart.LazyInclude2").newInstance()).testIt();
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiC);
    actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)53 Bundle (org.osgi.framework.Bundle)41 Test (org.junit.Test)12 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)12 BundleListener (org.osgi.framework.BundleListener)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 BundleException (org.osgi.framework.BundleException)6 File (java.io.File)5 BundleContext (org.osgi.framework.BundleContext)5 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Endpoint (javax.xml.ws.Endpoint)3 BundleTracker (org.osgi.util.tracker.BundleTracker)3 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2