Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

java - accessing command-line arguments from OSGi bundle

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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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: " + Arrays.toString(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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...