Search in sources :

Example 16 with BundleEvent

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

the class ModelAdapterFactoryUtil method addModelsForClasses.

/**
     * Search classpath for given class names to scan for and register all classes with @Model annotation.
     * @param bundleContext Bundle context
     * @param classNames Java class names
     */
public static void addModelsForClasses(BundleContext bundleContext, String... classNames) {
    Bundle bundle = new RegisterModelsBundle(bundleContext, Bundle.ACTIVE, null, classNames);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 17 with BundleEvent

use of org.osgi.framework.BundleEvent in project jo-client-platform by jo-source.

the class CapClientOsgiWorkbenchRunner method start.

@Override
public void start(final BundleContext context) throws Exception {
    final Bundle bundle = context.getBundle();
    context.addBundleListener(new BundleListener() {

        @Override
        public void bundleChanged(final BundleEvent event) {
            if (event.getBundle().equals(bundle) && BundleEvent.STARTED == event.getType()) {
                bundleStartet();
            }
        }
    });
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Example 18 with BundleEvent

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

the class ClassLoadingBundleTests method testStartTransient.

public void testStartTransient() throws Exception {
    // install a bundle and set its start-level high, then crank up the framework start-level.  This should result in no events
    // $NON-NLS-1$
    Bundle osgiA = installer.installBundle("osgi.lazystart.a");
    installer.resolveBundles(new Bundle[] { osgiA });
    StartLevel startLevel = installer.getStartLevel();
    startLevel.setBundleStartLevel(osgiA, startLevel.getStartLevel() + 10);
    // test transient start Bundle.start(START_TRANSIENT)
    startLevel.setStartLevel(startLevel.getStartLevel() + 15);
    Object[] expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    startLevel.setStartLevel(startLevel.getStartLevel() - 15);
    expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    Object[] expectedEvents = new Object[0];
    Object[] actualEvents = simpleResults.getResults(0);
    compareResults(expectedEvents, actualEvents);
    // now call start(START_TRANSIENT) before the start-level is met.  This should result in no events
    try {
        osgiA.start(Bundle.START_TRANSIENT);
        // $NON-NLS-1$
        assertFalse("Bundle is started!!", osgiA.getState() == Bundle.ACTIVE);
    } catch (BundleException e) {
        // $NON-NLS-1$
        assertEquals("Expected invalid operation", BundleException.START_TRANSIENT_ERROR, e.getType());
    }
    expectedEvents = new Object[0];
    actualEvents = simpleResults.getResults(0);
    compareResults(expectedEvents, actualEvents);
    startLevel.setStartLevel(startLevel.getStartLevel() + 15);
    expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    startLevel.setStartLevel(startLevel.getStartLevel() - 15);
    expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    expectedEvents = new Object[0];
    actualEvents = simpleResults.getResults(0);
    compareResults(expectedEvents, actualEvents);
    // now call start(START_TRANSIENT) while start-level is met.
    startLevel.setStartLevel(startLevel.getStartLevel() + 15);
    expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    osgiA.start(Bundle.START_TRANSIENT);
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiA);
    actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
    startLevel.setStartLevel(startLevel.getStartLevel() - 15);
    expectedFrameworkEvents = new Object[1];
    expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
    actualFrameworkEvents = frameworkListenerResults.getResults(1);
    compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STOPPED, osgiA);
    actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) BundleEvent(org.osgi.framework.BundleEvent) BundleException(org.osgi.framework.BundleException)

Example 19 with BundleEvent

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

the class ClassLoadingBundleTests method testBug300692_01.

public void testBug300692_01() throws BundleException {
    // $NON-NLS-1$
    Bundle chainTest = installer.installBundle("chain.test");
    // $NON-NLS-1$
    Bundle chainTestA = installer.installBundle("chain.test.a");
    // $NON-NLS-1$
    Bundle chainTestB = installer.installBundle("chain.test.b");
    // $NON-NLS-1$
    Bundle chainTestC = installer.installBundle("chain.test.c");
    // $NON-NLS-1$
    Bundle chainTestD = installer.installBundle("chain.test.d");
    syncListenerResults.getResults(0);
    StartLevel sl = installer.getStartLevel();
    int currentSL = sl.getStartLevel();
    int testSL = currentSL + 1;
    sl.setBundleStartLevel(chainTest, testSL);
    sl.setBundleStartLevel(chainTestA, testSL);
    sl.setBundleStartLevel(chainTestB, testSL);
    sl.setBundleStartLevel(chainTestC, testSL);
    sl.setBundleStartLevel(chainTestD, testSL);
    installer.resolveBundles(new Bundle[] { chainTest, chainTestA, chainTestB, chainTestC, chainTestD });
    Object[] expectedEvents = new Object[5];
    expectedEvents[0] = new BundleEvent(BundleEvent.RESOLVED, chainTestD);
    expectedEvents[1] = new BundleEvent(BundleEvent.RESOLVED, chainTestC);
    expectedEvents[2] = new BundleEvent(BundleEvent.RESOLVED, chainTestB);
    expectedEvents[3] = new BundleEvent(BundleEvent.RESOLVED, chainTestA);
    expectedEvents[4] = new BundleEvent(BundleEvent.RESOLVED, chainTest);
    Object[] actualEvents = syncListenerResults.getResults(5);
    compareResults(expectedEvents, actualEvents);
    try {
        System.setProperty("test.bug300692", "true");
        chainTest.start();
        sl.setStartLevel(testSL);
        Object[] expectedFrameworkEvents = new Object[1];
        expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
        Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
        compareResults(expectedFrameworkEvents, actualFrameworkEvents);
        expectedEvents = new Object[14];
        int i = 0;
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTest);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTest);
        actualFrameworkEvents = syncListenerResults.getResults(14);
        compareResults(expectedEvents, actualFrameworkEvents);
    } finally {
        System.getProperties().remove("test.bug300692");
        sl.setStartLevel(currentSL);
        Object[] expectedFrameworkEvents = new Object[1];
        expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
        Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
        compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) BundleEvent(org.osgi.framework.BundleEvent) Endpoint(javax.xml.ws.Endpoint)

Example 20 with BundleEvent

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

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