访问OSGi包中的命令行参数 [英] accessing command-line arguments from OSGi bundle

查看:408
本文介绍了访问OSGi包中的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为OSGi包集合运行的应用程序。我开始使用嵌入Felix框架的一个非常小的包装。这个包装器的必要性使我有点恐惧,因为它依赖于Felix(而应用程序本身也可以运行在Equinox中),所以我想摆脱它,并使用默认的Felix启动器。

I have an application that runs as a collection of OSGi bundles. I start it using a very small wrapper that embeds the Felix framework. The necessity of that wrapper irks me a little, as does the fact that it depends on Felix (whereas the application itself could run just as well in, say, Equinox), so I want to get rid of it, and use the default Felix launcher.

包装器真正做的只是将命令行参数传递到已启动的OSGi框架中,以便在那里的bundle可以对它们做出反应。注意,它实际上不解析参数,只是将String []推送到我的应用程序中。

The only thing that the wrapper really does is passing the command line arguments into the launched OSGi framework, so that a bundle there can react upon them. Note that it does not actually parse the arguments, just pushes the String[] into my application.

是否有标准方法(或至少是Felix标准方法)

Is there a standard way (or at least a Felix-standard way) to access command line parameters from a bundle, so that I can do away with the custom launcher?

推荐答案

如果使用bnd()方法,工具),你可以使用它的启动器。它将命令行参数注册为服务属性launcher.arguments。

If you use bnd(tools) you can use its launcher. It registers the command line arguments as a service property 'launcher.arguments'.

当它与bnd package命令结合使用时效果非常好。此命令采用bnd项目或bndrun文件描述运行环境(bundle,属性,框架),并变成一个独立的主jar。所以你在bndtools中开发和调试,当你很高兴你把它变成一个可执行的jar。示例:

This works extremely well when you combine it with the bnd package command. This command takes a bnd project or a bndrun file describing the running environment (bundles, properties, framework) and turns into into a standalone main jar. So you develop and debug in bndtools and when you're happy you turn it into a single executable jar. Example:

@Component
public class MyApp {
   String args;

   @Activate
   void activate() { 
      System.out.println("Args: " + args);
   }

   @Reference(target="(launcher.arguments=*)")
   void args( Object object, Map<String,Object> map) {
       args = (String) map.get("launcher.arguments");
   }
}

# to turn into an executable
bnd package myapp.bnd
java -jar myapp.jar -a somearg *.file

这篇关于访问OSGi包中的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆