【问题标题】:OSGI service component dependency couldn't resolved无法解决 OSGI 服务组件依赖关系
【发布时间】:2014-05-27 05:07:08
【问题描述】:

我需要在 OSGI 中实现一个计算器程序。为此,我创建了三个 jar 包,称为计算器 api、计算器实现和计算器包,分别命名为 cala-1.0.0.jarcali-1.0.0.jarcalb-1.0.0.jar

在这样的计算器包类中:

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;
                }

pom.xml 文件是这样的:

<?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>

当我在 OSGI 控制台中运行这个程序时,控制台上会出现以下错误:

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)"

这可能是什么原因?

【问题讨论】:

    标签: java osgi command-line-arguments osgi-bundle


    【解决方案1】:

    你在你的 maven bundle 插件配置中为这个包定义了一个 Import-Package。所以你必须安装一个包来导出这个包。

    尝试安装此捆绑包: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html

    【讨论】:

    • 你安装的bundle真的导出了这个包吗?如果是,则错误不会发生。可能导出包的包版本与你指定的导入范围不匹配。
    猜你喜欢
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2014-05-06
    • 2016-02-16
    • 2017-02-20
    相关资源
    最近更新 更多