Java Option HandlingΒΆ

In Java generally two different ways of getting command line parameters from the caller of a program exist. On the one hand, the usual list of arguments is passed to the main function. On the other hand, the virtual machine provides a property system. These properties can be set using -D flags. We recommend the use of these flags, as they are available even if you do not have the main method under your control. Unfortunately, no standard library exists to support handling these properties. Hence, some manual work is required, as e.g. shown in Runner.java in the example project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Runner {

    private static rsb.Scope scope;
    private static long magicNumber;

    private static void parseArguments() {
        scope = new Scope(System.getProperty("buildsystemessentials.scope", "/default/scope"));
        magicNumber = Long.parseLong(System.getProperty("buildsystemessentials.magicnumber", "42"));
    }

    public static void main(String[] args) throws Throwable {

        // extract values of global variables form system properties.
        parseArguments();

        // get a factory instance to create new RSB domain objects
        Factory factory = Factory.getInstance();

        // create an informer on scope "/example/informer" to send event
        // notifications. This informer is capable of sending Strings.
        Informer<Long> informer = factory.createInformer(scope);

        // activate the informer to be ready for work
        informer.activate();

        // send some data
        informer.send(new MagicNumberTransformer().transform(42));

        // as there is no explicit removal model in java, always manually
        // deactivate the informer if it is not needed anymore
        informer.deactivate();

    };

}