OSGi自定义shell命令

时间:2016-06-27 10:44:11

标签: eclipse osgi equinox

我正在尝试为OSGI控制台(Equinox)创建一个自定义命令,但我似乎无法正确注册或使用该命令。捆绑包启动并尝试呼叫mock:commandcommand无效。使用的eclipse相当陈旧:3.6.2.R36x_v20110210并且手动启动包含的bundle。有什么想法吗?

public class Activator extends Plugin
{
    private static Activator plugin;
    private MockCommand service;

    @Override
    public void start(BundleContext context) throws Exception{
        plugin = this;  
        Dictionary<String, Object> properties = new Hashtable<String, Object>();
        properties.put("osgi.command.scope", "mock");
        properties.put("osgi.command.function", new String[] {MockCommand.COMMAND});
        service =  new MockCommand();
        context.registerService(MockCommand.class.getName(),service, null);
        super.start(context);
    }

    @Override
    public void stop(BundleContext context) throws Exception{
        plugin = null;
        service = null;
        super.stop(context);
    }

    public static Activator getDefault(){
        return plugin;
    }
}

CommandProvider:

public class MockCommand implements CommandProvider{

    public static String COMMAND ="command";

    public void _command(CommandInterpreter ci) throws Exception {
        String commandID = "com.sample.project.fetchMySampleDataCommandId";
        ((IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class)).executeCommand(commandID, null);
    }

    @Override
    public String getHelp() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("--- Available commands to call by ID ---\n\t");
        buffer.append("command --> com.sample.project.fetchMySampleDataCommandId\n\t");
        return buffer.toString();
    }
}

1 个答案:

答案 0 :(得分:1)

显然缺少OSGI服务组件定义。为此,我创建了一个/OSGI-INF/ServiceFacade.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="serviceFacade">
    <implementation class="com.sample.project.MockCommand"/>
    <service>
        <provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
    </service>
</scr:component>

并添加到我的/META-INF/MANIFEST.MF

Service-Component: OSGI-INF/ServiceFacade.xml