embedded karaf

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

embedded karaf



I am a newbie to karaf, and I am trying to figure out how to embed it in another program. My understanding is that something like the following code should work (taken from http://njbartlett.name/2011/03/07/embedding-osgi.html):

FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
// TODO: add some config properties
Framework framework = frameworkFactory.newFramework(config);

Should this work for karaf? And if so what jar files need to be included to obtain the framework factory as in above?

On the other hand, the karaf Main (2.3.1) does something different:

        String factoryClass = configProps.getProperty(KARAF_FRAMEWORK_FACTORY);
        if (factoryClass == null) {
            InputStream is = classLoader.getResourceAsStream("META-INF/services/" + FrameworkFactory.class.getName());
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            factoryClass = br.readLine();
        FrameworkFactory factory = (FrameworkFactory) classLoader.loadClass(factoryClass).newInstance();
        framework = factory.newFramework(new StringMap(configProps, false));

Would this be the correct way of embedding karaf? If so, what jar file should include the FrameworkFactory services resource, and what jar file should include the actual factoryClass implementation?

Thanks in advance for any help in clarifying the right way to embed karaf.