以编程方式执行 OSGI 控制台命令

Execute OSGI console commands programmatically

有人可以提供一个关于如何以编程方式执行 OSGI 控制台命令的工作示例吗?

我正在通过代码加载 OSGI,我想执行 OSGI 控制台命令(我通过不同的系统接收命令)。这是我正在做的一个简单测试:

ServiceLoader<FrameworkFactory> frameworkFactoryService = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory frameworkFactory = frameworkFactoryService.iterator().next();

Map<String, String> config = new HashMap<String,String>();
config.put("org.osgi.framework.storage", "../workspace/.config");
config.put("org.osgi.framework.storage.clean", "onFirstInit");

framework = frameworkFactory.newFramework(config);

framework.init();
framework.start();  

// install required bundles
String bundleLocation = "org.eclipse.equinox.common_3.8.0.20181108-1144.jar";
Bundle bundle = framework.getBundleContext().installBundle(bundleLocation);

bundleLocation = "org.eclipse.update.configurator_3.4.2.M20090103-1001-RCP20181108-1144.jar";
bundle = framework.getBundleContext().installBundle(bundleLocation);
bundle.start();

bundleLocation = "org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar";
bundle = framework.getBundleContext().installBundle(bundleLocation);
bundle.start();

bundleLocation = "org.apache.felix.gogo.command_0.10.0.v201209301215.jar";
bundle = framework.getBundleContext().installBundle(bundleLocation);
bundle.start();

bundleLocation = "org.apache.felix.gogo.shell_0.10.0.v201212101605.jar";
bundle = framework.getBundleContext().installBundle(bundleLocation);
bundle.start();

bundleLocation = "org.eclipse.equinox.console_1.1.200.v20150929-1405.jar";
bundle = framework.getBundleContext().installBundle(bundleLocation);
bundle.start();

CommandProcessorImpl commandProcessor = new CommandProcessorImpl();
CommandSession commandSession = commandProcessor.createSession(System.in, System.out, System.err);

commandSession.execute("ss");

一切都正确加载,如果我以编程方式遍历所有包,我可以看到一切都已加载并启动。不幸的是,我在 "execute" 行收到异常 "Command not found: ss"。我究竟做错了什么?谁有简单的工作示例?

您将自己启动 CommandProcessImpl。您应该改用 CommandProcessor 服务。您创建的实例没有连接到框架,因此找不到任何注册为服务的命令。

 BundleContext context = framework.getBundleContext();
 ServiceReference<CommandProcessor> cpr = 
     context.getServiceReference(CommandProcessor.class);
 CommandProcessor cp = context.getService(cpr);

 CommandSession session = cp.createSession(System.in, System.out, System.err);

 session.execute("lsb");

显然这段代码没有受到保护。获得服务参考然后获得服务真的很糟糕,因为服务可以来来去去。

bnd 有一个远程代理 (biz.aQute.bnd.remote),您可以轻松地从外部进程调用它。它还有一个 bndremote 程序,你可以在任何机器上 运行 然后你可以在该机器上下载框架 + 包。这可能比自己构建更容易?