Search code examples
javaosgicommand-line-argumentsosgi-bundle

OSGI service component dependency couldn't resolved


I need to implement a calculator program in OSGI. For that I have created three bundles of jar called calculator api, calculator implementation and calculator bundle named as cala-1.0.0.jar, cali-1.0.0.jar, and calb-1.0.0.jar.

Within the calculator bundle class like this:

package org.cal.bun;

            import org.cal.api.calapi;

            //import org.osgi.service.component.ComponentContext;
            import org.eclipse.osgi.framework.console.CommandInterpreter;
            import org.eclipse.osgi.framework.console.CommandProvider;

            /**
             * The Declarative Service Component for Hello Service
             *
             * @scr.component name="org.cal.bun.calculatorservice"
             * immediate="true"
             * @scr.reference name="org.wso2.cal"
             * interface="org.cal.api.calapi"
             * cardinality="1..1"
             * policy="static"
             * bind="bindcalapi"
             * unbind="unbindcalapi"
             */

            public class calculatorservice implements CommandProvider{

              private calapi s;


                public synchronized void bindcalapi(calapi s) {
                    this.s = s;
                }

                public synchronized void unbindcalapi(calapi s) {
                    this.s = null;
                }

                public synchronized void _run(CommandInterpreter ci) {
                    if (s != null) {
                        System.out.println("hi");
                    } else {
                        ci.println("Error, No Service of type Sayable available");
                    }
                }

                @Override
                public String getHelp() {
                    return null;
                }

and the pom.xml file is like this:

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd http://www.osgi.org/xmlns/scr/v1.0.0">
        <parent>
            <artifactId>cal</artifactId>
            <groupId>org.wso2.com</groupId>
            <version>1.0.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>calb</artifactId>
        <packaging>bundle</packaging>

        <dependencies>
            <dependency>
                <groupId>org.eclipse.osgi</groupId>
                <artifactId>org.eclipse.osgi</artifactId>
                <version>3.9.1.v20130814-1242</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.osgi</groupId>
                <artifactId>org.eclipse.osgi.services</artifactId>
                <version>3.3.100.v20130513-1956</version>
            </dependency>

            <dependency>
                <groupId>org.wso2.com</groupId>
                <artifactId>cala</artifactId>
                <version>1.0.0</version>
            </dependency>


        </dependencies>

        <repositories>
            <repository>
                <id>wso2-nexus</id>
                <name>WSO2 internal Repository</name>
                <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
                <releases>
                    <enabled>true</enabled>
                    <updatePolicy>daily</updatePolicy>
                    <checksumPolicy>ignore</checksumPolicy>
                </releases>
            </repository>
        </repositories>


        <build>

            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-scr-plugin</artifactId>
                    <version>1.7.2</version>
                    <executions>
                        <execution>
                            <id>generate-scr-scrdescriptor</id>
                            <goals>
                                <goal>scr</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>

                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>2.3.5</version>
                    <extensions>true</extensions>

                    <configuration>

                        <instructions>

                            <Bundle-Vendor>Sample Inck</Bundle-Vendor>
                            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>

                            <Import-Package>
                                org.cal.api.*;version=1.0.0,
                                org.osgi.*;,org.eclipse.osgi.framework.*;
                               org.osgi.service.component.*;

                            </Import-Package>




                        </instructions>
                    </configuration>
                </plugin>
            </plugins>

        </build>




    </project>

When I run this program in OSGI console the following error appears on the console:

45  ACTIVE      cala_1.0.0
46  ACTIVE      cali_1.0.0
52  INSTALLED   calb_1.0.0
osgi> stop 45 46 52
osgi> start 45 46 52
gogo: BundleException: The bundle "calb_1.0.0 [52]" could not be resolved. Reason:     Missing Constraint: Import-Package: org.osgi.service.component; version="[1.2.0,2.0.0)"

What can be the reason for this?


Solution

  • You define an Import-Package for this package in your maven bundle plugin config. So you have to install a bundle that exports this package.

    Try to install this bundle: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html