我需要在OSGI中实现一个计算器程序。为此我创建了三个jar包,称为计算器api,计算器实现和计算器包,命名为cala-1.0.0.jar
,cali-1.0.0.jar
和calb-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)"
这可能是什么原因?
答案 0 :(得分:1)
您可以在maven包插件配置中为此包定义Import-Package。所以你必须安装一个导出这个包的包。
尝试安装此捆绑包: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html