7

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

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

是否有标准方式(或至少是 Felix 标准方式)从包中访问命令行参数,以便我可以取消自定义启动器?

4

4 回答 4

9

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

当您将它与 bnd package 命令结合使用时,它的效果非常好。此命令采用描述运行环境(包、属性、框架)的 bnd 项目或 bndrun 文件并变成独立的主 jar。因此,您在 bndtools 中进行开发和调试,当您满意时,您可以将其转换为单个可执行 jar。例子:

@Component
public class MyApp {
   String[] args;

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

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

变成可执行文件:

bnd package myapp.bnd
java -jar myapp.jar -a somearg *.file
于 2012-11-30T07:50:16.267 回答
2

Late answer but perhaps someone finds it useful.

I was having quite the same issue. My application runs in OSGi but I have external interfaces that I need to comply with which implies reading the command line arguments.

The key to this is something defined in the new OSGi specification 4.2, namely Framework Launching. You can read about it in the Draft spec (found under Draft on www.osgi.org) in the Life Cycle Layer section.

It's a standard way of launching an OSGi framework (any implementation that supports OSGi 4.2) from a stand-alone java application. The nifty thing is that you don't need to know which implementation you start (Felix, Equinox, ...) as long as it is found in the CLASSPATH.

This way, the your launcher application reads command line arguments, instantiates and starts an OSGi framework and pass the arguments to your bundle (any way you want). What you get in the launcher application is a Context to the framework from which you can communicate with your bundles.

As of Equinox 3.5M6 (I think, well at least M6) this is supported. The latest version of Apache Felix does also support this.

于 2009-05-20T14:20:49.770 回答
1

我知道您只搜索了 Felix。那么,这个 Equinox-only 解决方案可能没有用。我把它留在这里,因为其他人可能会偶然发现这个问题并让 Equinox 运行。

从任何捆绑包和任何框架中,这可能都很困难。如果您使用 org.eclipse.core.runtime.applications 扩展点,应该很容易。前提条件:您不要将 -console 作为参数传递。

public class Application implements IApplication {

    @Override
    public Object start(IApplicationContext context) throws Exception {
        String[] args = (String[])context.getArguments().get("application.args");
        // args.length == 0 if no arguments have been passed
    }
}

plugin.xml 中的引用

 <plugin>
    <extension
          id="myApp"
          point="org.eclipse.core.runtime.applications">
        <application>
          <run class="package.Application" />
        </application>  
    </extension>
 </plugin>
于 2013-01-05T01:16:20.547 回答
1

可能不是。我认为标准的 Felix 启动器会进行一些命令行验证,并且只接受捆绑缓存目录作为参数。不止一个参数,启动器退出。

您可以使用系统属性在命令行中传递信息,我认为它不仅适用于 felix,还适用于其他 osgi 容器,但它可能会使您的应用程序对用户不太友好。

于 2009-01-19T14:50:08.793 回答