3

I am working on converting a core java application into OSGI bundles. I searched a lot but could not find the standard or the best way to accomplish the following when the application is converted to bundles.

  1. How to read command line arguments in OSGI bundles ( Currently the application is invoked from a script that passes arguments which is read inside the main function of one of the class that is part of the jar file)

  2. In non - OSGI deployment we have fine tuned the GC parameters for the JVM using -X options. How to accomplish the same when it comes to OSGI ?. When I split the current application into bundles I need to have different GC tuning for different bundles. How it is done ?

  3. In the code many places Sytem.getProperty is used to read parameters passed in -D option. How this can be done in OSGI.

It will be of great help if anyone can guide me on how I should proceed.

Thanks JK

JK_007
  • 125
  • 3
  • 10

2 Answers2

2

bnd(tools) has standard support for this. It will register an Object service with the launcher's argument as the launcher.arguments the argument service property. The following code demonstrates its usage:

@Component
public class ShowArgs {
  String[] args;

  @Activate
  void start() { System.out.println(Arrays.toString(args); }

  @Reference
  void setDone( Object done, Map<String,Object> map) {
     this.args = (String[]) map.get("launcher.arguments")
  }

}

bnd(tools) is also great to turn an OSGi framework into an executable jar. You can turn any bndrun file (contains the info about the framework, the run bundles, etc) into an executable JAR. Either via the Run pane in bndtools or via the bnd package x.bndrun command.

Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
1

1: I do not think there is way to read command line arguments. You should specify the parameters as system properties with "-Dxxx" as they can be read in the code of the bundles

2: OSGi runs on one JVM. The JVM can be parameterized with "-X" attributes. You cannot specify these attributes per bundle. You cannot specify different GC settings for separate bundles as you cannot define different GC settings for different parts of your program. Your program must be really, really (and "really" about ten more times :)) special if you want to do something like this. What is your motivation here?

3: You can get the system properties in the same way: System.getProperty(...)

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • The application is deployed as single JAR file with one class as entry point (say Start.java).From the single jar 4 different components (JVMs) will be started based on the command line argument passed to Start.java from a script.Each of these components has different GC settings (currently it is taken care in the script. The script passes the GC settings along with -D arguments while invoking Start.java based on which component it is starting). Example : if A then invoke Start.java with -XParallelGc; if B then invoke with other GC etc.. Now the plan is to split these components as 4 bundles. – JK_007 Oct 22 '13 at 11:31
  • Regarding point 1 & 3 could you please give more explanation on how to achieve it (i.e passing and reading -D parameters) Currently I just do bundle:start() so I dont know how to pass arguments. – JK_007 Oct 22 '13 at 11:32
  • The question is, how you start your OSGi container. e.g.: "java -Dxxx=yyy -jar equinox.osgi.jar". In this case you can do at any place (even if you are in the class of a bundle) System.getProperty("xxx"). Probably you over-mistify OSGi. In case of OSGi, every bundle runs under the same JVM. OSGi may not be the answer for your problem if you want different JVMs. – Balazs Zsoldos Oct 22 '13 at 11:42
  • I uses karaf and as of now I just uses ./karaf from bin dir.. Thanks for the pointer about GC. – JK_007 Oct 22 '13 at 11:57
  • In this case it is a Karaf related question, how to set system properties. Like in Tomcat you can set special environment variables, I guess there are some options for Karaf as well. – Balazs Zsoldos Oct 22 '13 at 12:32