Search in sources :

Example 66 with BundleEvent

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

the class ClassLoadingBundleTests method testStopTransient.

public void testStopTransient() throws Exception {
    // $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);
    // persistently start the bundle
    osgiA.start();
    // test that the bundle is started when start-level is met
    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);
    Object[] expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiA);
    Object[] 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);
    // now call stop(STOP_TRANSIENT) while the 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);
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, osgiA);
    actualEvents = simpleResults.getResults(1);
    compareResults(expectedEvents, actualEvents);
    osgiA.stop(Bundle.STOP_TRANSIENT);
    expectedEvents = new Object[1];
    expectedEvents[0] = new BundleEvent(BundleEvent.STOPPED, 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);
    // now make sure the bundle still restarts when 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);
    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)

Example 67 with BundleEvent

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

the class ClassLoadingBundleTests method testMultiChainDepedencies01.

public void testMultiChainDepedencies01() throws Exception {
    // $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");
    // $NON-NLS-1$
    chainTest.loadClass("chain.test.TestMultiChain").newInstance();
    Object[] expectedEvents = new Object[8];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, chainTestD);
    expectedEvents[1] = new BundleEvent(BundleEvent.STARTED, chainTestB);
    expectedEvents[2] = new BundleEvent(BundleEvent.STARTED, chainTestC);
    expectedEvents[3] = new BundleEvent(BundleEvent.STARTED, chainTestA);
    expectedEvents[4] = new BundleEvent(BundleEvent.STOPPED, chainTestA);
    expectedEvents[5] = new BundleEvent(BundleEvent.STOPPED, chainTestB);
    expectedEvents[6] = new BundleEvent(BundleEvent.STOPPED, chainTestC);
    expectedEvents[7] = new BundleEvent(BundleEvent.STOPPED, chainTestD);
    installer.refreshPackages(new Bundle[] { chainTestC, chainTestD });
    Object[] actualEvents = simpleResults.getResults(8);
    compareResults(expectedEvents, actualEvents);
    // $NON-NLS-1$
    chainTest.loadClass("chain.test.TestMultiChain").newInstance();
    expectedEvents = new Object[4];
    expectedEvents[0] = new BundleEvent(BundleEvent.STARTED, chainTestD);
    expectedEvents[1] = new BundleEvent(BundleEvent.STARTED, chainTestB);
    expectedEvents[2] = new BundleEvent(BundleEvent.STARTED, chainTestC);
    expectedEvents[3] = new BundleEvent(BundleEvent.STARTED, chainTestA);
    actualEvents = simpleResults.getResults(4);
    compareResults(expectedEvents, actualEvents);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 68 with BundleEvent

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

the class ClassLoadingBundleTests method testThreadLock.

public void testThreadLock() throws Exception {
    // $NON-NLS-1$
    Bundle threadLockTest = installer.installBundle("thread.locktest");
    // $NON-NLS-1$
    threadLockTest.loadClass("thread.locktest.ATest");
    Object[] expectedEvents = new Object[2];
    expectedEvents[0] = new Long(5000);
    expectedEvents[1] = new BundleEvent(BundleEvent.STARTED, threadLockTest);
    Object[] actualEvents = simpleResults.getResults(2);
    compareResults(expectedEvents, actualEvents);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 69 with BundleEvent

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

Example 70 with BundleEvent

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

the class BundleEventAdapter method bundleChanged.

/**
 * Once a bundle event is received this method assembles and posts an event via
 * the <tt>EventAdmin</tt> as specified in 113.6.4 OSGi R4 compendium.
 *
 * @param event The event to adapt.
 */
@Override
public void bundleChanged(final BundleEvent event) {
    final Dictionary<String, Object> properties = new Hashtable<String, Object>();
    properties.put(EventConstants.EVENT, event);
    properties.put("bundle.id", new Long(event.getBundle().getBundleId()));
    final String symbolicName = event.getBundle().getSymbolicName();
    if (null != symbolicName) {
        properties.put(EventConstants.BUNDLE_SYMBOLICNAME, symbolicName);
    }
    properties.put("bundle", event.getBundle());
    final StringBuffer topic = new StringBuffer(BundleEvent.class.getName().replace('.', '/')).append('/');
    switch(event.getType()) {
        case BundleEvent.INSTALLED:
            topic.append("INSTALLED");
            break;
        case BundleEvent.STARTING:
            topic.append("STARTING");
            break;
        case BundleEvent.STARTED:
            topic.append("STARTED");
            break;
        case BundleEvent.STOPPING:
            topic.append("STOPPING");
            break;
        case BundleEvent.STOPPED:
            topic.append("STOPPED");
            break;
        case BundleEvent.UPDATED:
            topic.append("UPDATED");
            break;
        case BundleEvent.UNINSTALLED:
            topic.append("UNINSTALLED");
            break;
        case BundleEvent.RESOLVED:
            topic.append("RESOLVED");
            break;
        case BundleEvent.UNRESOLVED:
            topic.append("UNRESOLVED");
            break;
        default:
            // IGNORE EVENT
            return;
    }
    try {
        getEventAdmin().postEvent(new Event(topic.toString(), properties));
    } catch (IllegalStateException e) {
    // This is o.k. - indicates that we are stopped.
    }
}
Also used : Hashtable(java.util.Hashtable) BundleEvent(org.osgi.framework.BundleEvent) Event(org.osgi.service.event.Event) BundleEvent(org.osgi.framework.BundleEvent)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)83 Bundle (org.osgi.framework.Bundle)49 Test (org.junit.Test)24 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)17 BundleListener (org.osgi.framework.BundleListener)15 FeatureDTO (com.amitinside.featureflags.dto.FeatureDTO)11 File (java.io.File)10 IOException (java.io.IOException)10 BundleException (org.osgi.framework.BundleException)10 BundleContext (org.osgi.framework.BundleContext)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BundleTracker (org.osgi.util.tracker.BundleTracker)5 Map (java.util.Map)4 ServiceReference (org.osgi.framework.ServiceReference)4 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)4 FileInputStream (java.io.FileInputStream)3