我目前正在尝试使用iPOJO为Felix实现一个自定义的外壳命令。我的示例实现如下所示:
import java.io.PrintStream;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.shell.Command;
@Component(immediate = true)
@Provides
public class SampleCommand implements Command {
@Override
public String getName() {
return "testcmd";
}
@Override
public String getUsage() {
return "testcmd";
}
@Override
public String getShortDescription() {
return "test command";
}
@Override
public void execute(String line, PrintStream out, PrintStream err) {
out.println("execute testcmd!");
}
}当我在Felix上部署包时,我的SampleCommand被实例化并且getName()被调用。但是当我尝试在shell上执行"testcmd“时,我得到:
gogo: CommandNotFoundException: Command not found: testcmd还有什么需要我进一步考虑的吗?
发布于 2012-03-07 18:17:55
基于上面user1231484和earcam给出的反馈,这里有一个最小的工作示例:
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.service.command.Descriptor;
@Component(immediate = true)
@Instantiate
@Provides(specifications = ListComponentsCommand.class)
public class ListComponentsCommand {
@ServiceProperty(name = "osgi.command.scope", value = "test")
String scope;
@ServiceProperty(name = "osgi.command.function", value = "{}")
String[] function = new String[] { "test" };
@Descriptor("test")
public void test() {
System.out.println("test!");
}
}发布于 2012-03-07 01:37:15
我不确定你是否需要子类化命令(this page看起来很旧),我认为只需要注册一个有两个特定属性的服务就可以了:
这样,您的命令就不需要知道任何关于OSGi的信息。以通常的方式使用打印流(这些流由shell重定向)
例如。
@ServiceProperty(name = "osgi.command.scope", value = "mycommands")
@ServiceProperty(name = "osgi.command.function", value = {"execute", "add"})
@Component(immediate = true)
@Provides
public class SampleCommand implements MyOwnCommand {
@Override
public void execute(String line) {
System.out.println("execute testcmd! with line: " + line);
}
@Override
public void add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
}
}你为此付出的唯一代价就是失去帮助和使用功能。
发布于 2012-03-07 17:52:40
您正在为Felix Shell (旧的)开发一个命令。然而,由于相当长的一段时间,Felix现在使用Gogo (实现OSGi标准)。因此,您应该检查http://felix.apache.org/site/rfc-147-overview.html以提供一个新命令。
此外,你还可以看看the iPOJO Arch command for Gogo。它使用的是iPOJO本身。
https://stackoverflow.com/questions/9588309
复制相似问题