Access command line arguments from the OSGi package

I have an application that works as a set of OSGi packages. I start it with a very small wrapper that implements the failex framework. The need for this wrapper annoys me a bit, as well as the fact that it depends on Felix (while the application itself can work just like, say, Equinox), so I want to get rid of it and use the default Felix launcher.

The only thing the shell really does is pass command line arguments to the running OSGi infrastructure so that the package can respond to them. Note that it does not actually parse the arguments, it just pushes String [] in my application.

Is there a standard way (or at least standard Felix) to access command line options from a package so that I can end the custom launcher?

+7
java command-line osgi apache-felix
source share
4 answers

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

This works very well when you combine it with the bnd package command. This command accepts a bnd project or a bndrun file that describes the current environment (packages, properties, framework) and turns into a separate main jar. Thus, you develop and debug in bndtools, and when you are happy, you turn it into one 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 
+7
source share

Late answer, but maybe someone finds this helpful.

I had the same problem. My application runs on OSGi, but I have external interfaces that I need to execute, which involves reading command line arguments.

The key to this is what is defined in the new OSGi 4.2 specification, namely the launch of the Framework. You can read about this in the draft project (see Project at www.osgi.org) in the Life Cycle section.

This is the standard way to run the OSGi platform (any implementation that supports OSGi 4.2) from a stand-alone Java application. The main thing is that you do not need to know which implementation you are starting (Felix, Equinox, ...) if it is found in CLASSPATH.

This way, your application starts reading command line arguments, instantiates and runs the OSGi framework, and passes the arguments to your bundle (in whatever way you want). What you get in the launch application is the context for the structure from which you can communicate with your packages.

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

+2
source share

Probably not. I think the standard Felix launcher does some command line validation and only accepts the package cache argument as an argument. More than one argument and start disabled.

You can use system properties to pass information on the command line, and I think that this works not only in felix, but also in other osgi containers, but this probably makes your application not very user friendly.

+1
source share

I know that you were looking only for Felix. Then this solution only for the equinox may be unsuitable. I leave it here because someone might stumble on this issue and run Equinox.

From any package and any structure, this can be difficult. If you use the org.eclipse.core.runtime.applications extension point, this should be easy. Condition: You do not pass -console as a parameter.

 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 } } 

Link in plugin.xml

  <plugin> <extension id="myApp" point="org.eclipse.core.runtime.applications"> <application> <run class="package.Application" /> </application> </extension> </plugin> 
+1
source share

All Articles