Search in sources :

Example 1 with FrameworkListener

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

the class Aries1429Test method testMissingParentChildEdgeTolerated.

@Test
public void testMissingParentChildEdgeTolerated() throws Exception {
    final AtomicBoolean weavingHookCalled = new AtomicBoolean();
    final AtomicReference<FrameworkEvent> frameworkEvent = new AtomicReference<FrameworkEvent>();
    bundleContext.registerService(WeavingHook.class, new WeavingHook() {

        @Override
        public void weave(WovenClass wovenClass) {
            Bundle bundle = wovenClass.getBundleWiring().getBundle();
            if (BUNDLE_A.equals(bundle.getSymbolicName())) {
                wovenClass.getDynamicImports().add("com.acme.tnt");
                weavingHookCalled.set(true);
            }
        }
    }, null);
    Subsystem applicationA = installSubsystemFromFile(APPLICATION_A);
    try {
        removeConnectionWithParent(applicationA);
        BundleContext context = applicationA.getBundleContext();
        Bundle bundleA = context.installBundle(BUNDLE_A, TinyBundles.bundle().add(getClass().getClassLoader().loadClass("a.A"), InnerClassStrategy.NONE).set(Constants.BUNDLE_SYMBOLICNAME, BUNDLE_A).build(TinyBundles.withBnd()));
        bundleContext.addFrameworkListener(new FrameworkListener() {

            @Override
            public void frameworkEvent(FrameworkEvent event) {
                if (FrameworkEvent.ERROR == event.getType() && getSubsystemCoreBundle().equals(event.getBundle())) {
                    frameworkEvent.set(event);
                    if (event.getThrowable() != null) {
                        event.getThrowable().printStackTrace();
                    }
                }
            }
        });
        bundleA.loadClass("a.A");
        assertTrue("Weaving hook not called", weavingHookCalled.get());
        Thread.sleep(1000);
        assertNull("An exception was thrown", frameworkEvent.get());
    } finally {
        uninstallSubsystemSilently(applicationA);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) AriesSubsystem(org.apache.aries.subsystem.AriesSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) AtomicReference(java.util.concurrent.atomic.AtomicReference) FrameworkListener(org.osgi.framework.FrameworkListener) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) BundleContext(org.osgi.framework.BundleContext) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest) Test(org.junit.Test)

Example 2 with FrameworkListener

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

the class SubsystemTest method assertRefresh.

protected void assertRefresh(Collection<Bundle> bundles) throws InterruptedException {
    FrameworkWiring wiring = getSystemBundleAsFrameworkWiring();
    final AtomicBoolean refreshed = new AtomicBoolean(false);
    wiring.refreshBundles(bundles, new FrameworkListener[] { new FrameworkListener() {

        @Override
        public void frameworkEvent(FrameworkEvent event) {
            if (FrameworkEvent.PACKAGES_REFRESHED == event.getType()) {
                synchronized (refreshed) {
                    refreshed.set(true);
                    refreshed.notify();
                }
            }
        }
    } });
    synchronized (refreshed) {
        refreshed.wait(5000);
    }
    assertTrue("Bundles not refreshed", refreshed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameworkEvent(org.osgi.framework.FrameworkEvent) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 3 with FrameworkListener

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

the class AgentServer method refresh.

public void refresh(boolean async) throws InterruptedException {
    FrameworkWiring f = context.getBundle(0).adapt(FrameworkWiring.class);
    if (f != null) {
        refresh = new CountDownLatch(1);
        f.refreshBundles(null, new FrameworkListener() {

            @Override
            public void frameworkEvent(FrameworkEvent event) {
                refresh.countDown();
            }
        });
        if (async)
            return;
        refresh.await();
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) CountDownLatch(java.util.concurrent.CountDownLatch) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 4 with FrameworkListener

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

the class Launcher method createFramework.

private Framework createFramework() throws Exception {
    Properties p = new Properties();
    p.putAll(properties);
    File workingdir = null;
    if (parms.storageDir != null)
        workingdir = parms.storageDir;
    else if (parms.keep && parms.name != null) {
        workingdir = new File(bnd, parms.name);
    }
    if (workingdir == null) {
        workingdir = File.createTempFile("osgi.", ".fw");
        final File wd = workingdir;
        Runtime.getRuntime().addShutdownHook(new Thread("launcher::delete temp working dir") {

            public void run() {
                deleteFiles(wd);
            }
        });
    }
    trace("using working dir: %s with keeping=%s", workingdir, parms.keep);
    if (!parms.keep && workingdir.exists()) {
        trace("deleting working dir %s because not kept", workingdir);
        delete(workingdir);
        p.setProperty(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
    }
    IO.mkdirs(workingdir);
    if (!workingdir.isDirectory())
        throw new IllegalArgumentException("Cannot create a working dir: " + workingdir);
    if (System.getProperty(Constants.FRAMEWORK_STORAGE) == null)
        p.setProperty(Constants.FRAMEWORK_STORAGE, workingdir.getAbsolutePath());
    else
        p.setProperty(Constants.FRAMEWORK_STORAGE, System.getProperty(Constants.FRAMEWORK_STORAGE));
    if (parms.systemPackages != null) {
        p.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, parms.systemPackages);
        trace("system packages used: %s", parms.systemPackages);
    }
    if (parms.systemCapabilities != null) {
        p.setProperty(FRAMEWORK_SYSTEM_CAPABILITIES_EXTRA, parms.systemCapabilities);
        trace("system capabilities used: %s", parms.systemCapabilities);
    }
    Framework systemBundle;
    if (parms.services) {
        trace("using META-INF/services");
        // 3) framework = null, lookup in META-INF/services
        ClassLoader loader = getClass().getClassLoader();
        // 3) Lookup in META-INF/services
        List<String> implementations = getMetaInfServices(loader, FrameworkFactory.class.getName());
        if (implementations.size() == 0)
            error("Found no fw implementation");
        if (implementations.size() > 1)
            error("Found more than one framework implementations: %s", implementations);
        String implementation = implementations.get(0);
        Class<?> clazz = loader.loadClass(implementation);
        FrameworkFactory factory = (FrameworkFactory) clazz.getConstructor().newInstance();
        trace("Framework factory %s", factory);
        @SuppressWarnings({ "unchecked", "rawtypes" }) Map<String, String> configuration = (Map) p;
        systemBundle = factory.newFramework(configuration);
        trace("framework instance %s", systemBundle);
    } else {
        trace("using embedded mini framework because we were told not to use META-INF/services");
        // we have to use our own dummy framework
        systemBundle = new MiniFramework(p);
    }
    systemBundle.init();
    try {
        systemBundle.getBundleContext().addFrameworkListener(new FrameworkListener() {

            public void frameworkEvent(FrameworkEvent event) {
                switch(event.getType()) {
                    case FrameworkEvent.ERROR:
                    case FrameworkEvent.WAIT_TIMEDOUT:
                        trace("Refresh will end due to error or timeout %s", event.toString());
                    case FrameworkEvent.PACKAGES_REFRESHED:
                        inrefresh = false;
                        trace("refresh ended");
                        break;
                }
            }
        });
    } catch (Exception e) {
        trace("could not register a framework listener: %s", e);
    }
    trace("inited system bundle %s", systemBundle);
    return systemBundle;
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Properties(java.util.Properties) MiniFramework(aQute.launcher.minifw.MiniFramework) BundleException(org.osgi.framework.BundleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) FrameworkListener(org.osgi.framework.FrameworkListener) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Framework(org.osgi.framework.launch.Framework) MiniFramework(aQute.launcher.minifw.MiniFramework)

Example 5 with FrameworkListener

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

the class Framework method notifyFrameworkListeners.

/**
     * notify all framework listeners.
     *
     * @param state the new state.
     * @param bundle the bundle.
     * @param throwable a throwable.
     */
static void notifyFrameworkListeners(final int state, final Bundle bundle, final Throwable throwable) {
    if (frameworkListeners.isEmpty()) {
        return;
    }
    final FrameworkEvent event = new FrameworkEvent(state, bundle, throwable);
    final FrameworkListener[] listeners = (FrameworkListener[]) frameworkListeners.toArray(new FrameworkListener[frameworkListeners.size()]);
    for (int i = 0; i < listeners.length; i++) {
        final FrameworkListener listener = listeners[i];
        listener.frameworkEvent(event);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) FrameworkListener(org.osgi.framework.FrameworkListener)

Aggregations

FrameworkListener (org.osgi.framework.FrameworkListener)12 FrameworkEvent (org.osgi.framework.FrameworkEvent)11 IOException (java.io.IOException)5 Bundle (org.osgi.framework.Bundle)5 FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 BundleContext (org.osgi.framework.BundleContext)4 BundleException (org.osgi.framework.BundleException)4 Semaphore (java.util.concurrent.Semaphore)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)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 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1