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

以编程方式执行OSGI控制台命令,osgi,apache-felix,gogo-shell,Osgi,Apache Felix,Gogo Shell,有人能提供一个如何以编程方式执行OSGI控制台命令的工作示例吗 我是通过代码加载OSGI的,我想执行OSGI控制台命令(我通过另一个系统接收命令)。这是我做的一个简单测试: ServiceLoader<FrameworkFactory> frameworkFactoryService = ServiceLoader.load(FrameworkFactory.class); FrameworkFactory frameworkFactory = frameworkFactorySer

有人能提供一个如何以编程方式执行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");
ServiceLoader frameworkFactoryService=ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory=frameworkFactoryService.iterator().next();
Map config=newhashmap();
config.put(“org.osgi.framework.storage”,“./workspace/.config”);
config.put(“org.osgi.framework.storage.clean”、“onfirstini”);
frameworkFactory.newFramework(配置);
framework.init();
framework.start();
//安装所需的捆绑包
字符串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=新CommandProcessorImpl();
CommandSession CommandSession=commandProcessor.createSession(System.in、System.out、System.err);
commandSession.execute(“ss”);

所有内容都正确加载,如果我以编程方式循环所有捆绑包,我可以看到所有内容都已加载并启动。不幸的是,我在“execute”行中遇到异常“Command not found:ss”。我做错了什么?有没有人有一个简单的工作示例?

您正在自己启动
命令processimpl
。您应该获得
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");
BundleContext=framework.getBundleContext();
服务参考cpr=
getServiceReference(CommandProcessor.class);
CommandProcessor cp=context.getService(cpr);
CommandSession会话=cp.createSession(System.in、System.out、System.err);
会话。执行(“lsb”);
显然,该代码没有受到保护。获得一个服务引用,然后再获得一个服务是非常糟糕的,因为服务可以来来往往


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

您正在自己启动
命令processimpl
。您应该获得
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");
BundleContext=framework.getBundleContext();
服务参考cpr=
getServiceReference(CommandProcessor.class);
CommandProcessor cp=context.getService(cpr);
CommandSession会话=cp.createSession(System.in、System.out、System.err);
会话。执行(“lsb”);
显然,该代码没有受到保护。获得一个服务引用,然后再获得一个服务是非常糟糕的,因为服务可以来来往往


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

谢谢。这是一个很好的指导。正如这篇文章所建议的,我错过了框架和CommandProcessor之间的连接。我还必须使用反射,因为我的代码在OSGI环境之外运行,并且在运行时必须使用捆绑包中的包中的类。我还将看一看bnd。如果您需要命令,请通过框架注册服务。它会被捡起来的。你想做的很多事情已经在许多不同的环境中做了很多次。除非这是一个爱好项目,否则请获得一些帮助,不要重新发明轮子,更糟糕的是,在这个容易出错的领域中犯许多错误。发射器很复杂!谢谢这是一个很好的指导。正如这篇文章所建议的,我错过了框架和CommandProcessor之间的连接。我还必须使用反射,因为我的代码在OSGI环境之外运行,并且在运行时必须使用捆绑包中的包中的类。我还将看一看bnd。如果您需要命令,请通过框架注册服务。它会被捡起来的。你想做的很多事情已经在许多不同的环境中做了很多次。除非这是一个爱好项目,否则请获得一些帮助,不要重新发明轮子,更糟糕的是,在这个容易出错的领域中犯许多错误。发射器很复杂!