Search in sources :

Example 11 with FrameworkEvent

use of org.osgi.framework.FrameworkEvent in project aries by apache.

the class Framework method refreshBundlesAndWait.

private void refreshBundlesAndWait(long[] bundleIdentifiers, Bundle[] bundles) throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    FrameworkListener listener = new FrameworkListener() {

        public void frameworkEvent(FrameworkEvent event) {
            if (FrameworkEvent.PACKAGES_REFRESHED == event.getType()) {
                latch.countDown();
            }
        }
    };
    try {
        context.addFrameworkListener(listener);
        try {
            if (bundles != null) {
                for (int i = 0; i < bundleIdentifiers.length; i++) {
                    bundles[i] = FrameworkUtils.resolveBundle(context, bundleIdentifiers[i]);
                }
            }
            packageAdmin.refreshPackages(bundles);
            if (latch.await(30, TimeUnit.SECONDS))
                return;
            else
                throw new IOException("Refresh operation timed out");
        } catch (InterruptedException e) {
            IOException ex = new IOException();
            ex.initCause(e);
            throw ex;
        }
    } finally {
        context.removeFrameworkListener(listener);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 12 with FrameworkEvent

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

the class LogSupportTest method testFrameworkEventError.

@Test
public void testFrameworkEventError() throws Exception {
    BundleException bundleException = new BundleException("my bundle exception", BundleException.ACTIVATOR_ERROR);
    FrameworkEvent frameworkEvent = new FrameworkEvent(FrameworkEvent.ERROR, bundle, bundleException);
    logSupport.frameworkEvent(frameworkEvent);
    Mockito.verify(testLogger).error("FrameworkEvent ERROR (org.osgi.framework.BundleException: my bundle exception)", bundleException);
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) BundleException(org.osgi.framework.BundleException) Test(org.junit.Test)

Example 13 with FrameworkEvent

use of org.osgi.framework.FrameworkEvent in project bnd by bndtools.

the class EmbeddedActivator method refresh.

/*
	 * Refresh the stopped bundles
	 */
void refresh(BundleContext context, List<Bundle> toStop) throws InterruptedException {
    Bundle bundle = context.getBundle(0);
    FrameworkWiring framework = bundle.adapt(FrameworkWiring.class);
    final Semaphore s = new Semaphore(0);
    framework.refreshBundles(toStop, new FrameworkListener() {

        @Override
        public void frameworkEvent(FrameworkEvent event) {
            s.release();
        }
    });
    s.tryAcquire(10, TimeUnit.SECONDS);
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) Semaphore(java.util.concurrent.Semaphore) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 14 with FrameworkEvent

use of org.osgi.framework.FrameworkEvent in project bnd by bndtools.

the class AgentDispatcher method createFramework.

/**
	 * Create a new framework. This is reflectively called from the Envoy
	 */
public static Descriptor createFramework(String name, Map<String, Object> configuration, final File storage, final File shacache) throws Exception {
    //
    // Use the service loader for loading a framework
    //
    ClassLoader loader = AgentServer.class.getClassLoader();
    ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class, loader);
    FrameworkFactory ff = null;
    for (FrameworkFactory fff : sl) {
        ff = fff;
    // break;
    }
    if (ff == null)
        throw new IllegalArgumentException("No framework on runpath");
    //
    // Create the framework
    //
    @SuppressWarnings({ "unchecked", "rawtypes" }) Framework framework = ff.newFramework((Map) configuration);
    framework.init();
    framework.getBundleContext().addFrameworkListener(new FrameworkListener() {

        @Override
        public void frameworkEvent(FrameworkEvent event) {
        // System.err.println("FW Event " + event);
        }
    });
    framework.start();
    Descriptor d = new Descriptor();
    //
    // create a new descriptor. This is returned
    // to the envoy side as an Object and we will
    // get this back later in toAgent. The envoy
    // maintains a list of name -> framework
    //
    d.framework = framework;
    d.shaCache = shacache;
    d.storage = storage;
    d.configuration = configuration;
    d.name = name;
    String embedded = (String) configuration.get("biz.aQute.remote.embedded");
    if (embedded != null && !(embedded = embedded.trim()).isEmpty()) {
        String[] activators = embedded.trim().split("\\s*,\\s*");
        for (String activator : activators) try {
            Class<?> activatorClass = loader.loadClass(activator);
            if (BundleActivator.class.isAssignableFrom(activatorClass)) {
                // TODO check immediate
                BundleActivator ba = (BundleActivator) activatorClass.getConstructor().newInstance();
                ba.start(framework.getBundleContext());
                d.activators.add(ba);
            }
        } catch (Exception e) {
            // TODO
            System.out.println("IGNORED");
            e.printStackTrace();
        }
    }
    return d;
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) BundleActivator(org.osgi.framework.BundleActivator) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) FrameworkListener(org.osgi.framework.FrameworkListener) Framework(org.osgi.framework.launch.Framework)

Example 15 with FrameworkEvent

use of org.osgi.framework.FrameworkEvent in project bnd by bndtools.

the class AgentServer method frameworkEvent.

@Override
public void frameworkEvent(FrameworkEvent event) {
    try {
        Event e = new Event();
        e.type = Type.framework;
        e.code = event.getType();
        remote.event(e);
    } catch (Exception e1) {
        printStack(e1);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Event(aQute.remote.api.Event) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException)

Aggregations

FrameworkEvent (org.osgi.framework.FrameworkEvent)15 FrameworkListener (org.osgi.framework.FrameworkListener)10 BundleException (org.osgi.framework.BundleException)5 IOException (java.io.IOException)4 Semaphore (java.util.concurrent.Semaphore)3 Test (org.junit.Test)3 Bundle (org.osgi.framework.Bundle)3 FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 Framework (org.osgi.framework.launch.Framework)2 FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)2 MiniFramework (aQute.launcher.minifw.MiniFramework)1 Event (aQute.remote.api.Event)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1