【问题标题】:Custom Felix command with iPOJO annotations带有 iPOJO 注释的自定义 Felix 命令
【发布时间】:2012-03-06 16:59:45
【问题描述】:

我目前正在尝试使用 iPOJO 为 Felix 实现自定义 shell 命令。我的示例实现如下所示:

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 上部署 Bundle 时,我的 SampleCommand 被实例化并且 getName() 被调用。但是当我尝试在 shell 上执行“testcmd”时,我得到:

gogo: CommandNotFoundException: Command not found: testcmd

我还有什么需要考虑的吗?

【问题讨论】:

    标签: annotations osgi apache-felix


    【解决方案1】:

    根据上面 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!");
        }
    
    }
    

    【讨论】:

    • +1,你应该有足够的分数来接受你自己的答案=)
    【解决方案2】:

    我不确定您是否需要再将 Command 子类化(this page 看起来很老了),我认为只需注册具有两个特定属性的服务即可:

    1. 范围“osgi.command.scope”属性(本质上提供了一个命名空间,以防您的命令名称与其他命令名称相同。
    2. 一个字符串数组“osgi.command.function”,其中您的方法名称用作命令

    这样你的命令就不需要知道任何关于 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));
        }
    
    }
    

    您为此付出的唯一代价就是失去帮助和使用功能。

    【讨论】:

    • 感谢您的建议,很遗憾我无法为类设置@Property 注释(编译器错误“不允许用于此位置”)。有什么我可能误解了吗?
    • @earcam 到目前为止,注释是正确的,唯一的问题是,@ServiceProperty 需要放在字段级别。无论如何,谢谢,好指针:)
    【解决方案3】:

    您正在为 Felix Shell(旧)开发一个命令。但是,Felix 现在使用 Gogo(实现 OSGi 标准)已经有很长一段时间了。所以你应该检查http://felix.apache.org/site/rfc-147-overview.html来提供一个新的命令。

    此外,您可以查看the iPOJO Arch command for Gogo。它正在使用 iPOJO 本身。

    【讨论】:

    • 谢谢,昨天晚上我偶然发现了 Arch 代码被剪断,这实际上帮助我现在想出了一个工作版本。
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多