Search in sources :

Example 6 with BundleEvent

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

the class FeatureDeploymentListener method init.

public void init() throws Exception {
    bundleContext.addBundleListener(this);
    loadProperties();
    // Scan bundles
    for (Bundle bundle : bundleContext.getBundles()) {
        if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING || bundle.getState() == Bundle.ACTIVE)
            bundleChanged(new BundleEvent(BundleEvent.RESOLVED, bundle));
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 7 with BundleEvent

use of org.osgi.framework.BundleEvent in project jackrabbit-oak by apache.

the class OakOSGiRepositoryFactory method shutdown.

static void shutdown(PojoServiceRegistry registry, int timeoutInSecs) throws BundleException {
    if (registry == null) {
        return;
    }
    final Bundle systemBundle = registry.getBundleContext().getBundle();
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    //Logic here is similar to org.apache.felix.connect.PojoServiceRegistryFactoryImpl.FrameworkImpl.waitForStop()
    systemBundle.getBundleContext().addBundleListener(new SynchronousBundleListener() {

        public void bundleChanged(BundleEvent event) {
            if (event.getBundle() == systemBundle && event.getType() == BundleEvent.STOPPED) {
                shutdownLatch.countDown();
            }
        }
    });
    //Initiate shutdown
    systemBundle.stop();
    //Wait for framework shutdown to complete
    try {
        boolean shutdownWithinTimeout = shutdownLatch.await(timeoutInSecs, TimeUnit.SECONDS);
        if (!shutdownWithinTimeout) {
            throw new BundleException("Timed out while waiting for repository " + "shutdown for " + timeoutInSecs + " secs");
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new BundleException("Timed out while waiting for repository " + "shutdown for " + timeoutInSecs + " secs", e);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleException(org.osgi.framework.BundleException) CountDownLatch(java.util.concurrent.CountDownLatch) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 8 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 9 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 10 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)

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