Amdatu And DependencyManager error injecting service

214 Views Asked by At

I installed Amdatu Bootstrap and created a project.

I tried to do a simple injection of service with dependecyManager but an error shows up when running.

The code and my error.

This is the implementation of my Service:

package cco.bgen.scanner.XmlParser.implService;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;

public class XmlParserService implements XmlParserContrat {

    private DocumentBuilderFactory fabrique;
    private DocumentBuilder constructeur;
    private Document document;
    private Element rootElement;

    public XmlParserService() {
        super();
    }

    @Override
    public Element parserFichier(String filePath) {

        // Fabrique pour l'obtention d'une instance du documentBuilder : constructeur
        this.fabrique = DocumentBuilderFactory.newInstance();

        try {

            //l'obtention du constructeur
            this.constructeur = fabrique.newDocumentBuilder();

            try {

                //document à partir d'une path
                this.document = constructeur.parse(new File(filePath));

                //element root du document
                this.rootElement = document.getDocumentElement();

            } catch (SAXException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

        } catch (ParserConfigurationException e) {

            e.printStackTrace();

        }

        //Resultat
        return this.rootElement;
    }

}

This is the interface:

package cco.bgen.scanner.XmlParser.contratService;

import org.w3c.dom.Element;

public interface XmlParserContrat {

    public Element parserFichier(String fichierPath);
}

The Activator of this Service:

package cco.bgen.scanner.XmlParser.implService;

import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;

import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;

public class XmlParserActivator extends DependencyActivatorBase {

    @Override
    public void init(BundleContext arg0, DependencyManager manager)
            throws Exception {

        manager.add(createComponent().setInterface(
                XmlParserContrat.class.getName(), null).
                setImplementation(XmlParserService.class));
    }

    @Override
    public void destroy(BundleContext arg0, DependencyManager arg1)
            throws Exception {
        // TODO Auto-generated method stub

    }

}

After that I create a Test bundle for consuming the xmlParserService:

package cco.bgen.test;

import org.w3c.dom.Element;

import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;

public class Test {

    private volatile XmlParserContrat xmlParser;

    public void start(){

        Element rootE = xmlParser.parserFichier("processus.xml");

        System.out.println(rootE.toString());
    }
}

And the Activator for the test Service:

package cco.bgen.test;

import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;

import cco.bgen.scanner.XmlParser.contratService.XmlParserContrat;

public class TestActivator extends DependencyActivatorBase {

    @Override
    public void init(BundleContext arg0, DependencyManager manager)
            throws Exception {

        manager.add(createComponent()
                .setInterface(Object.class.getName(), null)
                .setImplementation(Test.class)
                .add(createServiceDependency().setService(
                        XmlParserContrat.class)));
    }

    @Override
    public void destroy(BundleContext arg0, DependencyManager manager)
            throws Exception {

    }

}

So when I run this in Run Descriptor, I have this Error ==>

! Failed to start bundle cco.bgen.scanner.Test-0.0.0, exception activator error org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component; from: cco.bgen.test.TestActivator:init#18
____________________________
Welcome to Apache Felix Gogo

So I tape lb command to show Active bundles and I start the bundle again. A detailed Error Show up ====>>

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.2.1)
    1|Resolved   |    1|cco.bgen.scanner.Test (0.0.0)
    2|Active     |    1|cco.bgen.scanner.XmlParser (1.0.0)
    3|Active     |    1|org.amdatu.template (1.0.0)
    4|Active     |    1|Apache Felix Configuration Admin Service (1.8.0)
    5|Active     |    1|Apache Felix Dependency Manager (3.1.0)
    6|Active     |    1|Apache Felix Dependency Manager (4.0.1)
    7|Active     |    1|Apache Felix Gogo Command (0.12.0)
    8|Active     |    1|Apache Felix Gogo Runtime (0.10.0)
    9|Active     |    1|Apache Felix Gogo Shell (0.10.0)
   10|Active     |    1|Apache Felix Servlet API (1.0.0)
   11|Active     |    1|Apache Felix Metatype Service (1.0.6)
g! start 1
g! org.osgi.framework.BundleException: Activator start error in bundle cco.bgen.scanner.Test [1].
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
    at org.apache.felix.gogo.command.Basic.start(Basic.java:729)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
    at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
    at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
    at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:403)
    at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
    at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)
    at org.apache.felix.gogo.shell.Console.run(Console.java:62)
    at org.apache.felix.gogo.shell.Shell.console(Shell.java:203)
    at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
    at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
    at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
    at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:403)
    at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
    at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)
    at org.apache.felix.gogo.shell.Activator.run(Activator.java:75)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodError: org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component;
    at cco.bgen.test.TestActivator.init(TestActivator.java:18)
    at org.apache.felix.dm.DependencyActivatorBase.start(DependencyActivatorBase.java:75)
    at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
    ... 32 more
java.lang.NoSuchMethodError: org.apache.felix.dm.Component.add(Lorg/apache/felix/dm/Dependency;)Lorg/apache/felix/dm/Component;

This is the Line 18 montioned in the error :: =>

.add(createServiceDependency().setService(

in TestActivator.java

1

There are 1 best solutions below

3
On

I see that you have both Dependency Manager 3 and 4 in your runtime. Make sure your bundle imports the correct one, for example by explicitly setting the version for the build path.