Question

I am trying to learn OSGI. (Mainly, the dynamic loading and unloading of bundles).

Following Neil Bartlett's tutorial for How To Embed OSGi, I added The Equinox OSGi framework implementation to the class path and started the game.

Here's the code:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;


public class BundleManager {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        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();



        BundleContext context = framework.getBundleContext();
        List<Bundle> installedBundles = new LinkedList<Bundle>();

        installedBundles.add(context.installBundle(
                              "file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar"));


        for (Bundle bundle : installedBundles) {

       if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
                 bundle.start();

         }


        System.out.println("done!!");



    }

}

Yes, it works. No errors at all. However, the bundle that I installed which is a jar file in the path:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar contains a "HelloWorld" in its start method. I don't see that "HelloWold" in my eclipse console. Why I don't see that message although the bundle was started? I appreciate any simple help.

Note: HellowService.jar is a plugin project that i created earlier, implemented BundleActivator in one of its classes to add "HelloWorld" message in the start method, and finally exported it as a jar file to the directory C:/Users/student/Documents/eclipse/myPlugins/

Edit: Here's my Activator class in the bundle I am installing and starting:

package com.javaworld.sample.service.impl;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.javaworld.sample.service.HelloService;

public class HelloServiceActivator implements BundleActivator {

    ServiceRegistration helloServiceRegistration;


        public void start(BundleContext context) throws Exception {

            HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
            helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null);

            System.out.println("Hello World!");
        }
        public void stop(BundleContext context) throws Exception {
            helloServiceRegistration.unregister();
        }


}

And here's the MANIFEST.MF file of the bundle:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService
Bundle-SymbolicName: com.javaworld.sample.HelloService
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.javaworld.sample.service.impl.HelloServiceActivator
Bundle-Vendor: JAVAWORLD
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: com.javaworld.sample.service

The way i export the bundle is by Right Click on the bundle project->Export->Runnable Jar File->then I select the Launch Configuration to be BundleManager (Which is the class installing the bundle).

I still do not see "Hello World!" message when I start the bundle from my application.

Was it helpful?

Solution 2

It turned out that I was exporting the bundle incorrectly. That's because I tried to do it by myself. Here's how the bundle should be exported as a jar file:

   Open the plugin export wizard File > Export... > Plug-in Development >
   Deployable plug-ins and fragments . 
   Then select the bundle you want to export and the destination directory. Done!

You can now use the path of the jar file to install the bundle. In my case, it is:

 installedBundles.add(context.installBundle(
                              "file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar"));

Source: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fexport_wizards%2Fexport_plugins.htm

And for sure thanks to @Neil Bartlett

OTHER TIPS

Your launcher does not wait for the OSGi Framework to stop. I would expect this program to start everything but then immediately shut down, because we reach the end of the main method. Refer back to my tutorial where I show how to use the Framework.waitForStop method.

Having said that, I would expect the output from your HelloWorld bundle to actually appear before the shutdown. So it seems likely there is an error in that bundle also. Perhaps you haven't declared the activator? I can only guess, because you haven't given any details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top