Search in sources :

Example 11 with Framework

use of org.osgi.framework.launch.Framework 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 12 with Framework

use of org.osgi.framework.launch.Framework in project sling by apache.

the class Sling method destroy.

/**
     * Destroys this servlet by shutting down the OSGi framework and hence the
     * delegatee servlet if one is set at all.
     */
public final void destroy() {
    if (framework != null) {
        // get a private copy of the reference and remove the class ref
        Framework myFramework;
        synchronized (this) {
            myFramework = framework;
            framework = null;
        }
        // shutdown the Felix container
        if (myFramework != null) {
            logger.log(Logger.LOG_INFO, "Shutting down Apache Sling");
            try {
                myFramework.stop();
                myFramework.waitForStop(0);
            } catch (BundleException be) {
                // may be thrown by stop, log but continue
                logger.log(Logger.LOG_ERROR, "Failure initiating Framework Shutdown", be);
            } catch (InterruptedException ie) {
                // may be thrown by waitForStop, log but continue
                logger.log(Logger.LOG_ERROR, "Interrupted while waiting for the Framework Termination", ie);
            }
            logger.log(Logger.LOG_INFO, "Apache Sling stopped");
        }
    }
}
Also used : BundleException(org.osgi.framework.BundleException) Framework(org.osgi.framework.launch.Framework)

Example 13 with Framework

use of org.osgi.framework.launch.Framework in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testSystemCapabilitiesBug522125.

public void testSystemCapabilitiesBug522125() throws URISyntaxException, FileNotFoundException, IOException, BundleException, InterruptedException {
    String frameworkLocation = OSGiTestsActivator.getContext().getProperty(EquinoxConfiguration.PROP_FRAMEWORK);
    URI uri = new URI(frameworkLocation);
    File f = new File(uri);
    if (!f.isFile()) {
        Assert.fail("Cannot test when framework location is a directory: " + f.getAbsolutePath());
    }
    File testDestination = OSGiTestsActivator.getContext().getDataFile(getName() + ".framework.jar");
    BaseSecurityTest.copy(new FileInputStream(f), testDestination);
    FilePath userDir = new FilePath(System.getProperty("user.dir"));
    FilePath testPath = new FilePath(testDestination);
    String relative = userDir.makeRelative(testPath);
    System.out.println(relative);
    URL relativeURL = new URL("file:" + relative);
    relativeURL.openStream().close();
    final ClassLoader osgiClassLoader = getClass().getClassLoader();
    URLClassLoader cl = new URLClassLoader(new URL[] { relativeURL }) {

        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            if (name.startsWith("org.osgi.")) {
                return osgiClassLoader.loadClass(name);
            }
            return super.loadClass(name, resolve);
        }
    };
    ServiceLoader<FrameworkFactory> sLoader = ServiceLoader.load(FrameworkFactory.class, cl);
    FrameworkFactory factory = sLoader.iterator().next();
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, String> configuration = new HashMap<String, String>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    configuration.put(EquinoxConfiguration.PROP_FRAMEWORK, relativeURL.toExternalForm());
    Framework framework = factory.newFramework(configuration);
    framework.init();
    framework.stop();
    framework.waitForStop(5000);
    BundleRevision systemRevision1 = framework.adapt(BundleRevision.class);
    int capCount1 = systemRevision1.getCapabilities(null).size();
    framework = factory.newFramework(configuration);
    framework.init();
    framework.stop();
    framework.waitForStop(5000);
    BundleRevision systemRevision2 = framework.adapt(BundleRevision.class);
    int capCount2 = systemRevision2.getCapabilities(null).size();
    Assert.assertEquals("Wrong number of capabilities", capCount1, capCount2);
}
Also used : FilePath(org.eclipse.osgi.framework.util.FilePath) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URI(java.net.URI) FileInputStream(java.io.FileInputStream) URL(java.net.URL) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) URLClassLoader(java.net.URLClassLoader) BundleRevision(org.osgi.framework.wiring.BundleRevision) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Framework(org.osgi.framework.launch.Framework)

Example 14 with Framework

use of org.osgi.framework.launch.Framework in project rt.equinox.framework by eclipse.

the class AbstractFrameworkHookTests method createFramework.

protected Framework createFramework(Map<String, String> configuration) throws Exception {
    FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(EquinoxFactory.class.getName()).newInstance();
    Framework framework = factory.newFramework(configuration);
    return framework;
}
Also used : EquinoxFactory(org.eclipse.osgi.launch.EquinoxFactory) Framework(org.osgi.framework.launch.Framework) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory)

Example 15 with Framework

use of org.osgi.framework.launch.Framework in project rt.equinox.framework by eclipse.

the class CascadeConfigTests method testCascadeConfigIni.

public void testCascadeConfigIni() throws Exception {
    // First create a framework with the 'parent' configuration
    File configParent = OSGiTestsActivator.getContext().getDataFile(getName() + "_parent");
    configParent.mkdirs();
    File parentConfigIni = new File(configParent, "config.ini");
    Properties parentProps = new Properties();
    parentProps.put("parent.key", "parent");
    parentProps.put("parent.child.key", "parent");
    parentProps.store(new FileOutputStream(parentConfigIni), "Parent config.ini");
    // Now create a child framework and make sure bundle is there
    File configChild = OSGiTestsActivator.getContext().getDataFile(getName() + "_child");
    configChild.mkdirs();
    File childConfigIni = new File(configChild, "config.ini");
    Properties childProps = new Properties();
    childProps.put("parent.child.key", "child");
    childProps.put("child.key", "child");
    childProps.store(new FileOutputStream(childConfigIni), "Parent config.ini");
    Map<String, Object> childMap = new HashMap<String, Object>();
    childMap.put(Constants.FRAMEWORK_STORAGE, configChild.getAbsolutePath());
    childMap.put("osgi.sharedConfiguration.area", configParent.getCanonicalPath());
    Framework equinox = new Equinox(childMap);
    equinox.init();
    BundleContext systemContext = equinox.getBundleContext();
    assertEquals("Wrong value for parent.key", "parent", systemContext.getProperty("parent.key"));
    assertEquals("Wrong value for parent.child.key", "child", systemContext.getProperty("parent.child.key"));
    assertEquals("Wrong value for child.key", "child", systemContext.getProperty("child.key"));
    equinox.stop();
    equinox.waitForStop(10000);
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Framework(org.osgi.framework.launch.Framework)

Aggregations

Framework (org.osgi.framework.launch.Framework)16 FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)9 File (java.io.File)8 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 Properties (java.util.Properties)4 Bundle (org.osgi.framework.Bundle)4 BundleContext (org.osgi.framework.BundleContext)4 BundleException (org.osgi.framework.BundleException)4 Test (org.junit.Test)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 PojoServiceRegistryFactoryImpl (de.kalpatec.pojosr.framework.PojoServiceRegistryFactoryImpl)2 ClasspathScanner (de.kalpatec.pojosr.framework.launch.ClasspathScanner)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 FrameworkEvent (org.osgi.framework.FrameworkEvent)2 ResourceIndexer (org.osgi.service.indexer.ResourceIndexer)2