【问题标题】:OSGi custom shell commandOSGi 自定义 shell 命令
【发布时间】:2016-06-27 10:44:11
【问题描述】:

我正在尝试为 OSGI 控制台 (Equinox) 创建自定义命令,但我似乎无法正确注册或使用该命令。捆绑包启动并尝试调用mock:commandcommand 无济于事。使用的 Eclipse 相当旧:3.6.2.R36x_v20110210 并且包含的​​包是手动启动的。有什么想法吗?

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();
    }
}

【问题讨论】:

    标签: eclipse osgi equinox


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 2014-11-29
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多