有人可以提供有关如何以编程方式执行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");
一切正常加载,如果我以编程方式遍历所有捆绑软件,则可以看到一切都已加载并启动。不幸的是,我在“执行”行上遇到异常“找不到命令:ss”。我究竟做错了什么?有人有一个简单的工作示例吗?
答案 0 :(得分:2)
您正在自己启动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程序,您可以在任何计算机上运行该程序,然后可以在该计算机上下载框架+捆绑软件。这可能比自己构建它更容易?