Search in sources :

Example 1 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project webservices-axiom by apache.

the class UsesConstraintsTest method test.

@Test
public void test() throws Exception {
    System.setProperty("java.protocol.handler.pkgs", "org.ops4j.pax.url");
    Map<String, String> p = new HashMap<String, String>();
    p.put(FRAMEWORK_STORAGE, new File("target/felix").getAbsolutePath());
    p.put(FRAMEWORK_STORAGE_CLEAN, FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
    FrameworkFactory frameworkFactory = new org.apache.felix.framework.FrameworkFactory();
    Framework framework = frameworkFactory.newFramework(p);
    framework.init();
    BundleContext context = framework.getBundleContext();
    Listener listener = new Listener();
    context.addFrameworkListener(listener);
    List<Bundle> bundles = new ArrayList<Bundle>();
    bundles.add(context.installBundle("link:classpath:META-INF/links/org.ops4j.pax.logging.api.link"));
    bundles.add(context.installBundle("link:classpath:org.apache.servicemix.specs.stax-api-1.0.link"));
    bundles.add(context.installBundle("link:classpath:stax2-api.link"));
    bundles.add(context.installBundle("link:classpath:woodstox-core-asl.link"));
    bundles.add(context.installBundle("link:classpath:org.apache.james.apache-mime4j-core.link"));
    bundles.add(context.installBundle("link:classpath:org.apache.ws.commons.axiom.axiom-api.link"));
    // This bundle will be wired to the javax.xml.stream package exported by the ServiceMix stax-api bundle.
    bundles.add(context.installBundle(streamBundle(bundle().set(BUNDLE_SYMBOLICNAME, "testbundle1").set(IMPORT_PACKAGE, "org.apache.axiom.om, javax.xml.stream; version=1.0").build()).getURL()));
    // This bundle will be wired to the javax.xml.stream package exported by the framework bundle
    bundles.add(context.installBundle(streamBundle(bundle().set(BUNDLE_SYMBOLICNAME, "testbundle2").set(IMPORT_PACKAGE, "org.apache.axiom.om, javax.xml.stream; version=\"[0.0.0,1.0)\"").build()).getURL()));
    for (Bundle bundle : bundles) {
        bundle.start();
    }
    framework.start();
    try {
        listener.awaitStart();
        assertTrue("Uses constraint violation expected", listener.gotExpectedError());
    } finally {
        framework.stop();
    }
}
Also used : FrameworkListener(org.osgi.framework.FrameworkListener) HashMap(java.util.HashMap) CoreOptions.streamBundle(org.ops4j.pax.exam.CoreOptions.streamBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) File(java.io.File) Framework(org.osgi.framework.launch.Framework) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 2 with FrameworkFactory

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

the class FrameworkSetup method call.

public Object call() throws Exception {
    final Model model = require(Launcher.MODEL_KEY, Model.class);
    final LauncherListener listener = (LauncherListener) get(Launcher.LISTENER_KEY);
    log.info("Setting OSGi framework properties");
    final Map<String, String> fprops = new FrameworkProperties(model).getProperties(null);
    log.info("Starting the OSGi framework");
    final FrameworkFactory factory = (FrameworkFactory) getClass().getClassLoader().loadClass("org.apache.felix.framework.FrameworkFactory").newInstance();
    final Framework framework = factory.newFramework(fprops);
    framework.start();
    final RunModeFilter rmFilter = new RunModeFilter();
    final Configurations cfg = new Configurations(framework.getBundleContext(), model, rmFilter);
    setShutdownHook(framework, new Closeable[] { cfg });
    log.info("OSGi framework started");
    log.info("Installing bundles from provisioning model");
    final BundlesInstaller bi = new BundlesInstaller(model, rmFilter);
    final BundleContext bc = framework.getBundleContext();
    bi.installBundles(bc, Launcher.NOT_CRANKSTART_FILTER);
    cfg.maybeConfigure();
    // TODO shall we gradually increase start levels like the launchpad does?? Reuse that DefaultStartupHandler code?
    final Bundle[] bundles = bc.getBundles();
    log.info("Starting all bundles ({} bundles installed)", bundles.length);
    int started = 0;
    int failed = 0;
    for (Bundle b : bundles) {
        if (isFragment(b)) {
            started++;
        } else {
            try {
                b.start();
                started++;
            } catch (BundleException be) {
                failed++;
                log.warn("Error starting bundle " + b.getSymbolicName(), be);
            }
        }
        cfg.maybeConfigure();
    }
    if (failed == 0) {
        log.info("All {} bundles started.", started);
    } else {
        log.info("{} bundles started, {} failed to start, total {}", started, failed, bundles.length);
    }
    log.info("OSGi setup done, waiting for framework to stop");
    if (listener != null) {
        listener.onStartup(started, failed, bundles.length);
    }
    framework.waitForStop(0);
    if (listener != null) {
        listener.onShutdown();
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) Model(org.apache.sling.provisioning.model.Model) BundleException(org.osgi.framework.BundleException) Framework(org.osgi.framework.launch.Framework) BundleContext(org.osgi.framework.BundleContext)

Example 3 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project bnd by bndtools.

the class DistroCommandTest method setUp.

@Override
protected void setUp() throws Exception {
    tmp = IO.getFile("generated/tmp");
    tmp.mkdirs();
    ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class, this.getClass().getClassLoader());
    FrameworkFactory ff = sl.iterator().next();
    Map<String, String> configuration = new HashMap<String, String>();
    configuration.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
    configuration.put(Constants.FRAMEWORK_STORAGE, new File(tmp, "fwstorage").getAbsolutePath());
    framework = ff.newFramework(configuration);
    framework.init();
    framework.start();
    BundleContext context = framework.getBundleContext();
    String[] bundles = { "../biz.aQute.remote/generated/biz.aQute.remote.agent.jar", "testdata/bundles/com.liferay.dynamic.data.mapping.taglib.jar", "testdata/bundles/com.liferay.item.selector.taglib.jar" };
    for (String bundle : bundles) {
        String location = "reference:" + IO.getFile(bundle).toURI().toString();
        Bundle b = context.installBundle(location);
        b.start();
    }
    super.setUp();
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) File(java.io.File) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) BundleContext(org.osgi.framework.BundleContext)

Example 4 with FrameworkFactory

use of org.osgi.framework.launch.FrameworkFactory in project jPOS by jpos.

the class Q2 method startOSGIFramework.

private void startOSGIFramework() throws BundleException {
    Iterator<FrameworkFactory> iter = ServiceLoader.load(FrameworkFactory.class, loader).iterator();
    if (iter.hasNext()) {
        FrameworkFactory frameworkFactory = iter.next();
        Map<String, String> config = new HashMap<String, String>();
        osgiFramework = frameworkFactory.newFramework(config);
        osgiFramework.start();
    } else {
        getLog().warn("OSGI framework not found");
    }
}
Also used : FrameworkFactory(org.osgi.framework.launch.FrameworkFactory)

Example 5 with FrameworkFactory

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

Aggregations

FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)21 Framework (org.osgi.framework.launch.Framework)9 File (java.io.File)8 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 BundleContext (org.osgi.framework.BundleContext)5 BundleException (org.osgi.framework.BundleException)5 Properties (java.util.Properties)4 Bundle (org.osgi.framework.Bundle)4 FrameworkEvent (org.osgi.framework.FrameworkEvent)4 ArrayList (java.util.ArrayList)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 FileInputStream (java.io.FileInputStream)2 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 Hashtable (java.util.Hashtable)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 Version (aQute.bnd.version.Version)1 MiniFramework (aQute.launcher.minifw.MiniFramework)1