如何在OSGI中的Activator类中使用Eclipse接口命令提供程序

nil*_*han 3 java eclipse osgi osgi-bundle

我是Eclipse接口命令提供程序的新代码.我在网站上看到了一个例子

public void _say(CommandInterpreter ci) {
    ci.print("You said:" + ci.nextArgument());
}

@Override
public String getHelp() {
    return "\tsay - repeats what you say\n";
}
Run Code Online (Sandbox Code Playgroud)

它用于将命令作为String并再次打印.

现在这是执行命令的另一个

String command = intcp.nextArgument();
if (command != null) {
    intcp.execute(command);
}
Run Code Online (Sandbox Code Playgroud)

为什么我们使用这种execute(command)方法?以及如何使用它?它有什么例子吗?

Pio*_*zmo 5

OSGI控制台主要用于调试OSGI应用程序.实现计算器并不是一种非常方便的方法.普通的控制台应用程序会更好.无论如何,这是熟悉API的好方法.

首先,创建一个实现CommandProvider的类:

public class Calculator implements CommandProvider {

    // add prints sum of its two arguments
    public void _add(CommandInterpreter ci) {
        int a = Integer.parseInt(ci.nextArgument());
        int b = Integer.parseInt(ci.nextArgument());
        ci.println(a+b);
    }

    // quit just calls "exit"
    public void _quit(CommandInterpreter ci) {
        ci.execute("exit");
    }

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

}
Run Code Online (Sandbox Code Playgroud)

您需要在捆绑包的激活器中注册它:

public class Activator implements BundleActivator {

    public void start(BundleContext bundleContext) throws Exception {
        bundleContext.registerService(CommandProvider.class.getName(),
                new Calculator(), null);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }
}
Run Code Online (Sandbox Code Playgroud)

要实际使用这些命令,您需要使用-console命令行参数启动RCP应用程序.如果您的软件包是懒惰加载的,则需要在命令可用之前启动它:

osgi> start com.example.mybundle
osgi> add 2 2
4
osgi> quit
Really want to stop Equinox? (y/n; default=y)  y
Run Code Online (Sandbox Code Playgroud)