Search in sources :

Example 6 with FrameworkEvent

use of org.osgi.framework.FrameworkEvent 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 7 with FrameworkEvent

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

the class Launcher method doTimeoutHandler.

private void doTimeoutHandler() {
    // Ensure we properly close in a separate thread so that
    // we can leverage the main thread, which is required for macs
    Thread wait = new Thread("FrameworkWaiter") {

        @Override
        public void run() {
            try {
                FrameworkEvent result = systemBundle.waitForStop(parms.timeout);
                if (!active.get()) {
                    trace("ignoring timeout handler because framework is already no longer active, shutdown is orderly handled");
                    return;
                }
                trace("framework event " + result + " " + result.getType());
                switch(result.getType()) {
                    case FrameworkEvent.STOPPED:
                        trace("framework event stopped");
                        System.exit(LauncherConstants.STOPPED);
                        break;
                    case FrameworkEvent.WAIT_TIMEDOUT:
                        trace("framework event timedout");
                        System.exit(LauncherConstants.TIMEDOUT);
                        break;
                    case FrameworkEvent.ERROR:
                        System.exit(ERROR);
                        break;
                    case FrameworkEvent.WARNING:
                        System.exit(WARNING);
                        break;
                    case FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED:
                    case FrameworkEvent.STOPPED_UPDATE:
                        trace("framework event update");
                        System.exit(UPDATE_NEEDED);
                        break;
                }
            } catch (InterruptedException e) {
                System.exit(CANCELED);
            }
        }
    };
    wait.start();
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent)

Example 8 with FrameworkEvent

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

Example 9 with FrameworkEvent

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

the class BundleFrameworkImpl method increaseStartLevel.

private void increaseStartLevel(BundleContext context) {
    /*
       * Algorithm for doing this
       * 
       * 1. Set up a framework listener that will tell us when the start level has been set.
       * 
       * 2. Change the start level. This is asynchronous so by the time the method returned the event 
       *    could have been sent. This is why we set up the listener in step 1.
       * 
       * 3. Wait until the start level has been set appropriately. At this stage all the bundles are startable
       *    and some have been started (most notably lazy activated bundles it appears). Other bundles are still
       *    in resolved state.
       */
    ServiceReference ref = context.getServiceReference(StartLevel.class.getName());
    if (ref != null) {
        StartLevel sl = (StartLevel) context.getService(ref);
        if (sl != null) {
            final Semaphore waitForStartLevelChangedEventToOccur = new Semaphore(0);
            // step 1
            FrameworkListener listener = new FrameworkListener() {

                public void frameworkEvent(FrameworkEvent event) {
                    if (event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) {
                        waitForStartLevelChangedEventToOccur.release();
                    }
                }
            };
            context.addFrameworkListener(listener);
            // step 2
            sl.setStartLevel(sl.getStartLevel() + 1);
            // step 3
            try {
                if (!!!waitForStartLevelChangedEventToOccur.tryAcquire(60, TimeUnit.SECONDS)) {
                    LOGGER.debug("Starting CBA child bundles took longer than 60 seconds");
                }
            } catch (InterruptedException e) {
                // restore the interrupted status
                Thread.currentThread().interrupt();
            }
            context.removeFrameworkListener(listener);
        }
        context.ungetService(ref);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) StartLevel(org.osgi.service.startlevel.StartLevel) Semaphore(java.util.concurrent.Semaphore) FrameworkListener(org.osgi.framework.FrameworkListener) ServiceReference(org.osgi.framework.ServiceReference)

Example 10 with FrameworkEvent

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

the class BundleFrameworkManagerImpl method uninstallBundle.

public void uninstallBundle(Bundle b) throws BundleException {
    synchronized (BundleFrameworkManager.SHARED_FRAMEWORK_LOCK) {
        BundleFramework framework = getBundleFramework(b);
        if (framework != null) {
            for (Bundle bundle : new ArrayList<Bundle>(framework.getBundles())) {
                framework.uninstall(bundle);
            }
            BundleContext ctx = framework.getIsolatedBundleContext();
            ServiceReference ref = ctx.getServiceReference(PackageAdmin.class.getName());
            if (ref != null) {
                try {
                    PackageAdmin pa = (PackageAdmin) ctx.getService(ref);
                    if (pa != null) {
                        final Semaphore sem = new Semaphore(0);
                        FrameworkListener listener = new FrameworkListener() {

                            public void frameworkEvent(FrameworkEvent event) {
                                if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
                                    sem.release();
                                }
                            }
                        };
                        ctx.addFrameworkListener(listener);
                        pa.refreshPackages(null);
                        try {
                            sem.tryAcquire(60, TimeUnit.SECONDS);
                        } catch (InterruptedException ie) {
                        }
                        ctx.removeFrameworkListener(listener);
                    }
                } finally {
                    ctx.ungetService(ref);
                }
            }
            framework.close();
            // clean up our maps so we don't leak memory
            _frameworks.remove(b);
            Iterator<BundleFramework> it = _frameworksByAppScope.values().iterator();
            while (it.hasNext()) {
                if (it.next().equals(framework))
                    it.remove();
            }
        }
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleFramework(org.apache.aries.application.management.spi.framework.BundleFramework) Semaphore(java.util.concurrent.Semaphore) FrameworkListener(org.osgi.framework.FrameworkListener) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

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