AKIWEB
AKIWEB

Reputation: 19612

Which package for FrameworkFactory I am supposed to choose in OSGi?

I recently started using OSGi framework.. I am trying to launch an OSGi container from a maven based project..

I have added this dependency in my pom.xml file in the above maven project-

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.framework</artifactId>
    <version>4.2.1</version>
</dependency>

Below is my main application-

public static void main(String[] args) throws BundleException {

    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);
    framework.start();
}

Now when I try to import the packages for the FrameworkFactory, I get two options to choose from.. Below is the image. Can anyone help me out which one I need to choose here?

enter image description here

I was choosing these two?

import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

Is this right?

Upvotes: 0

Views: 952

Answers (1)

Peter Kriens
Peter Kriens

Reputation: 15372

It is the org.osgi.framework.launch.FrameworkFactory since this gives you access to all OSGi frameworks, not just Apache Felix. The ServiceLoader uses the name of this class to look in all your class path entries (jars) META-INF/services directory for an org.osgi.framework.launch.FrameworkFactory file. If you look in the Felix framework bundle, you will find this file contains 'org.apache.felix.framework.FrameworkFactory', which will then be instantiated and returned.

So if you only want to use Apache Felix, you can also just do new org.apache.felix.framework.FrameworkFactory() instead of using the indirection of the Java Service Loader.

Upvotes: 4

Related Questions