java - How to write an OSGI command-line application -


i'm getting feet wet osgi , decided go atypical osgi use-case. i'd use in command-line application. want main(..) method takes flags , arguments, , shuts down again. don't want start apache karaf (or similar) , run commands within osgi console (this become optional feature though).

why osgi command-line application in first place? application supposed use different versions of same library (elasticsearch is). , because it's bad-ass of course.

should consume service within bundle or outside? how 1 that? problems arise?

there easy way write command line apps when use bnd. bnd has function create executable jar package command:

 $ bnd run xyz.bnd  .... whatever app  $ bnd package xyz.bnd  $ ls    xyz.jar  xyz.bnd .....  $ java -jar xyz.jar ...  .... whatever app 

note jar complete, contains bundles, framework, launcher, , properties run it. there no external dependencies.

the trick main thread (where static main called in). thing have register runnable service property main.thread=true. launcher call run() on service , exit (you can stay in run long want).

to command line arguments, can object service launcher.arguments property. property have command arguments. or ds component:

 @component(immediate=true, property="main.thread=true")  public class main implements runnable {      string[] args;       public void run(){ ... }       @reference(target="(launcher.arguments=*)")      void setargs(object service, map<string,object> props) {         this.args = (string[]) props.get("launcher.arguments");      }  } 

the best way bndtools since makes easy test/debug code. want use bndrun files then.

p.s. in latest bnd can use callable<integer> instead of runnable. return value exit code of process. might, however, not yet present in bndtools.


Comments