Search in sources :

Example 11 with FrameworkFactory

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

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

the class AgentTest method setUp.

@Override
protected void setUp() throws Exception {
    tmp = IO.getFile("generated/tmp");
    tmp.mkdirs();
    t1 = create("bsn-1", new Version(1, 0, 0));
    t2 = create("bsn-2", new Version(2, 0, 0));
    t3 = create("bsn-3", new Version(3, 0, 0));
    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();
    context = framework.getBundleContext();
    String location = "reference:" + t1.toURI().toString();
    context.installBundle(location);
    location = "reference:" + t2.toURI().toString();
    context.installBundle(location);
    location = "reference:" + IO.getFile("generated/biz.aQute.remote.agent.jar").toURI().toString();
    agent = context.installBundle(location);
    agent.start();
    supervisor = new TestSupervisor();
    supervisor.connect("localhost", Agent.DEFAULT_PORT);
    super.setUp();
}
Also used : Version(aQute.bnd.version.Version) HashMap(java.util.HashMap) File(java.io.File) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory)

Aggregations

FrameworkFactory (org.osgi.framework.launch.FrameworkFactory)12 File (java.io.File)7 Framework (org.osgi.framework.launch.Framework)6 BundleContext (org.osgi.framework.BundleContext)5 HashMap (java.util.HashMap)4 Bundle (org.osgi.framework.Bundle)4 BundleException (org.osgi.framework.BundleException)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 FrameworkListener (org.osgi.framework.FrameworkListener)3 ArrayList (java.util.ArrayList)2 FrameworkEvent (org.osgi.framework.FrameworkEvent)2 Version (aQute.bnd.version.Version)1 MiniFramework (aQute.launcher.minifw.MiniFramework)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URLClassLoader (java.net.URLClassLoader)1