3

I am interested in adding an OSGI container into my WAR but I can't find a tutorial or a documentation on how to do this. I found some things that are not useful at all. I am interested in Felix implementation and Atlassian implementation.

I am willing to do this so that my war accepts plug-ins and I can dynamically extend my Web app and also deploy it to any Web server.

Any links to a documentation or something? Any help is appreciated.

3 Answers 3

6

Adding an OSGi Framework launcher to a web application is not a big deal.

You need to add a listener to start the framework launcher in your web.xml

<listener>
  <listener-class>at.badgateway.StartupListener</listener-class>
</listener>

The startuplistener could look like this

public class StartupListener implements ServletContextListener {

//vars

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // set props
    Map<String, String> config = new HashMap<String, String>();
    config.put(Constants.FRAMEWORK_STORAGE, "path to cache");
    config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");

        try {
                    // get framework and start it
            FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
            framework = frameworkFactory.newFramework(config);
            framework.start();

                    // start existing bundles
            bundleContext = framework.getBundleContext();
            starter = new MyBundleStarter(servletContext, bundleContext);
            starter.launch();

        } catch (Exception ex)  
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
         // stop framework
    }
}

Take care of MyBundlestarter class in the upper quote, it is the class which activates all bundles contained in your war. (e.g. /WEB-INF/Osgi-Bundles)

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class MyBundleStarter{

    private BundleContext bundleContext = null;

    public void launch() throws Exception {

        ArrayList<Bundle> availableBundles= new ArrayList<Bundle>();
        //get and open available bundles
        for (URL url : getBundlesInWar()) {
            Bundle bundle = bundleContext.installBundle(url.getFile(), url.openStream());
            availableBundles.add(bundle);
        }

        //start the bundles
        for (Bundle bundle : availableBundles) {
            try{
            bundle.start();
            }catch()
        }

    private List<URL> getBundlesInWar() throws Exception {
        // returns a list of URLs located at destination
    }
}

Last but not least, you have to add an osgi framework to your project.

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

or

    <dependency>
        <groupId>org.eclipse.osgi</groupId>
        <artifactId>org.eclipse.osgi</artifactId>
    </dependency>
4
  • Martin.. can you give me some documentation? some links to an OSGI Implementation that allows you to do this? etc
    – Alex
    Apr 1, 2014 at 12:28
  • This code works with equinox and felix. Just add org.eclipse.osgi or org.apache.felix.framework to your pom and it will be detected because there they contain a class implementing the FrameworkFactory-Interface. Apr 1, 2014 at 12:30
  • Great. I will try it when I get home. Thank you
    – Alex
    Apr 1, 2014 at 12:31
  • Note: It might be useful to add felix file-install as bundle, which adds the possibility to add bundles in test-environments. Apr 2, 2014 at 9:25
1

I can see that this is an old post, but maybe it can be useful to someone: this writing contains a lot of useful things in this topic, at least for me it was a really great help. And it's worth it to give a look to other posts on that page.

0
0

If you are using WebLogic to host your application you can embed OSGi bundles in your WAR and have them deployed to a system defined OSGi server. This is nice because log messages from the OSGi log service can automatically be seen in the WebLogic logs. Also your bundles will be removed from the target OSGi server when you undeploy your application.

For more information see Configuration OSGi containers or developing OSGi apps or this blog post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.