سؤال

I am trying to embed an OSGI bundle that uses in my java application.

Below is my only java file in the bundle (I am doing it simple now):

package it.eng.test.ds.consumer;

public class Consumer {


    public void activate(){
        System.out.println("I'm here, in the activate ds automatic method ;)");
    }

    public void deactivate()
    {
        System.out.println("Bye Bye!");
    }

}

Below is consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="it.eng.test.ds.consumer"
activate="activate" 
deactivate="deactivate"
modified="modified"
enabled="true" 
immediate="true">
   <implementation class="it.eng.test.ds.consumer.Consumer"/>

</scr:component>

Below is MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ds-bundle
Bundle-SymbolicName: it.eng.test.ds.consumer
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
-Component: OSGI-INF/consumer.xml

Next, I embedded equinox in my application, started the required bundles (org.eclipse.equinox.ds and its dependencies), and finally started my bundle which is called it.eng.test.ds.consumer_1.0.0.201401071018.jar.

Below is my application 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.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;


public class MainEmbedder {


    public static void main(String[] args)
    {


        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);
        try {
            framework.start();
        } catch (BundleException e) {

            e.printStackTrace();
        }


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

        try {



            installedBundles.add(context.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.osgi.services_3.3.100.v20120522-1822.jar"));


            installedBundles.add(context.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.equinox.util_1.0.200.v20100503.jar"));

            installedBundles.add(context.installBundle(
                                 "file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.eclipse.equinox.ds_1.4.1.v20120926-201320.jar"));


            installedBundles.add(context.installBundle(
                    "file:C:\\Users\\student\\Desktop\\DS-Plugins\\plugins\\it.eng.test.ds.consumer_1.0.0.201401071018.jar"));




        } catch (BundleException e) {
            e.printStackTrace();
        }


        for (Bundle bundle : installedBundles) {

            try {
                bundle.start();


            } catch (BundleException e) {

                e.printStackTrace();
            }

        }System.out.println("after starting bundles");





    }
}

When I run my app, I can see the message "after starting bundles", but I do not see the message "I'm here, in the activate ds automatic method ;)". This means that my bundle did not enter the activate method. Why is that? How can I solve this problem? Thanks.

Update:
I tried to start the bundle with Felix framework, using the below code:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;

public class HostApplication
{
    private HostActivator m_activator = null;
    private Felix m_felix = null;


    public HostApplication()
    {
        // Create a configuration property map.
        Map config = new HashMap();
        config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
        // Create host activator;
        m_activator = new HostActivator();
        List list = new ArrayList();
        list.add(m_activator);

        config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);

        try
        {
            // Now create an instance of the framework with
            // our configuration properties.
            m_felix = new Felix(config);
            // Now start Felix instance.
            m_felix.start();
        }
        catch (Exception ex)
        {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
        }




        // Register the application's context as an OSGi service!
        BundleContext bundleContext1 = m_felix.getBundleContext();



                System.out.println("6");

                try {
                    Bundle dsBundle = bundleContext1.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\org.apache.felix.scr-1.8.0.jar");
                    dsBundle.start();
                    if(dsBundle.getState()== dsBundle.ACTIVE)
                        System.out.println("DS Bundle is Active!");

                    Bundle consumerBundle = bundleContext1.installBundle("file:C:\\Users\\student\\Desktop\\DS-Plugins\\plugins\\it.eng.test.ds.consumer_1.0.0.201401071018.jar");
                    consumerBundle.start();
                    if(consumerBundle.getState()== consumerBundle.ACTIVE)
                        System.out.println("Consumer Bundle is Active!");


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


                } catch (BundleException e) {

                    e.printStackTrace();

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





                shutdownApplication();


    }

    public Bundle[] getInstalledBundles()
    {
        // Use the system bundle activator to gain external
        // access to the set of installed bundles.
        return m_activator.getBundles();
    }

    public void shutdownApplication()
    {
        // Shut down the felix framework when stopping the
        // host application.
        try {
            m_felix.stop();
        } catch (BundleException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            m_felix.waitForStop(0);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Now I get the following error:

ERROR: it.eng.test.ds.consumer (2): Component descriptor entry 'OSGI-INF/consumer.xml' not found. 

I guess there is something wrong with my bundle, but it is very simple and just as I descried it above. Any ideas?

هل كانت مفيدة؟

المحلول

I've replayed your setup, and I've got it to work.

(A github repo is here, if all else fails)

Check if OSGI-INF/ is actually in your bundle, did you add it to build.properties (if you use PDE)

regards, Frank

نصائح أخرى

I am not sure what your exact intention is but embedding osgi bundles into a standard java application is maybe not the best thing to do. I would recommend to convert your application to osgi (if possible and it's not too big). If you cannot do to that consider to emulate the bundle with the ds component, e.g. just create the component instance manually and set it's dependencies on foot. But don't mix both worlds.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top