Search in sources :

Example 1 with BundleEvent

use of org.osgi.framework.BundleEvent in project atlas by alibaba.

the class Framework method notifyBundleListeners.

/**
     * notify all bundle listeners.
     *
     * @param state the new state.
     * @param bundle the bundle.
     */
static void notifyBundleListeners(final int state, final Bundle bundle) {
    if (syncBundleListeners.isEmpty() && bundleListeners.isEmpty()) {
        return;
    }
    final BundleEvent event = new BundleEvent(state, bundle);
    // inform the synchrounous bundle listeners first ...
    final BundleListener[] syncs = (BundleListener[]) syncBundleListeners.toArray(new BundleListener[syncBundleListeners.size()]);
    for (int i = 0; i < syncs.length; i++) {
        syncs[i].bundleChanged(event);
    }
    if (bundleListeners.isEmpty()) {
        return;
    }
    final BundleListener[] asyncs = (BundleListener[]) bundleListeners.toArray(new BundleListener[bundleListeners.size()]);
    for (int i = 0; i < asyncs.length; i++) {
        asyncs[i].bundleChanged(event);
    }
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener)

Example 2 with BundleEvent

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

the class ScriptEngineManagerFactoryTest method checkAddingScriptBundle.

@Test
public void checkAddingScriptBundle() throws Exception {
    context.checking(new Expectations() {

        {
            exactly(1).of(bundleCtx).registerService(with(equal(new String[] { "javax.script.ScriptEngineManager", "org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager" })), with(any(Object.class)), with(any(Dictionary.class)));
            will(returnValue(new MockServiceRegistration()));
        }
    });
    ScriptEngineManagerFactory factory = new ScriptEngineManagerFactory();
    factory.activate(componentCtx);
    ScriptEngineManager first = factory.getScriptEngineManager();
    assertNull(first.getEngineByName("dummy"));
    final Bundle bundle = context.mock(Bundle.class);
    final File factoryFile = createFactoryFile();
    context.checking(new Expectations() {

        {
            atLeast(1).of(bundle).getEntry("META-INF/services/javax.script.ScriptEngineFactory");
            will(returnValue(factoryFile.toURI().toURL()));
            atLeast(1).of(bundle).loadClass(SCRIPT_ENGINE_FACTORY.getName());
            will(returnValue(SCRIPT_ENGINE_FACTORY));
        }
    });
    factory.bundleChanged(new BundleEvent(BundleEvent.STARTED, bundle));
    ScriptEngineManager second = factory.getScriptEngineManager();
    assertNotNull(second.getEngineByName("dummy"));
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) Bundle(org.osgi.framework.Bundle) ScriptEngineManager(javax.script.ScriptEngineManager) BundleEvent(org.osgi.framework.BundleEvent) File(java.io.File) Test(org.junit.Test)

Example 3 with BundleEvent

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

the class QuartzSchedulerTest method testBundleChangedWithStoppedBundle.

@Test
public void testBundleChangedWithStoppedBundle() throws SchedulerException {
    String firstJob = "testName1";
    String secondJob = "testName2";
    when(bundle.getBundleId()).thenReturn(2L);
    quartzScheduler.addJob(1L, 1L, firstJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    quartzScheduler.addJob(2L, 2L, secondJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    BundleEvent event = new BundleEvent(BundleEvent.STOPPED, bundle);
    quartzScheduler.bundleChanged(event);
    assertTrue(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(firstJob)));
    assertFalse(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(secondJob)));
}
Also used : Serializable(java.io.Serializable) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 4 with BundleEvent

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

the class QuartzSchedulerTest method testBundleChangedWithoutScheduler.

@Test
public void testBundleChangedWithoutScheduler() throws Exception {
    String firstJob = "testName1";
    String secondJob = "testName2";
    Long bundleIdToRemove = 2L;
    when(bundle.getBundleId()).thenReturn(bundleIdToRemove);
    quartzScheduler.addJob(1L, 1L, firstJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    quartzScheduler.addJob(bundleIdToRemove, 2L, secondJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    setInternalSchedulerToNull();
    BundleEvent event = new BundleEvent(BundleEvent.STOPPED, bundle);
    quartzScheduler.bundleChanged(event);
    assertTrue(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(firstJob)));
    assertTrue(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(secondJob)));
    returnInternalSchedulerBack();
}
Also used : Serializable(java.io.Serializable) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 5 with BundleEvent

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

the class QuartzSchedulerTest method testBundleChangedWithStartedBundle.

@Test
public void testBundleChangedWithStartedBundle() throws SchedulerException {
    String firstJob = "testName1";
    String secondJob = "testName2";
    when(bundle.getBundleId()).thenReturn(2L);
    quartzScheduler.addJob(1L, 1L, firstJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    quartzScheduler.addJob(2L, 2L, secondJob, new Thread(), new HashMap<String, Serializable>(), "0 * * * * ?", true);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    quartzScheduler.bundleChanged(event);
    assertTrue(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(firstJob)));
    assertTrue(proxies.get("testName").getScheduler().checkExists(JobKey.jobKey(secondJob)));
}
Also used : Serializable(java.io.Serializable) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

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