Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelloWorldUtilities |
|
| 4.0;4 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.examples.simple; | |
11 | ||
12 | /** | |
13 | * Helper functions used by the sample. | |
14 | * <p>The helper functions have been moved here to prevent that the reader of the hello world | |
15 | * gets distracted by auxiliary helper functions.</p> | |
16 | * @since 0.1.0 | |
17 | */ | |
18 | public final class HelloWorldUtilities { | |
19 | ||
20 | //---- Static | |
21 | ||
22 | /** | |
23 | * Array of valid command line arguments. | |
24 | * @since 0.1.0 | |
25 | */ | |
26 | 0 | private static final String[] VALID_ARGS = new String[] { |
27 | "cn=john,o=post,c=ch", | |
28 | "cn=sara,o=post,c=ch", | |
29 | }; | |
30 | ||
31 | /** | |
32 | * Simple helper function to validate the command line arguments. | |
33 | * @param args the arguments to validate. | |
34 | * @return the input arguments to run the hello world with. | |
35 | * @since 0.1.0 | |
36 | */ | |
37 | public static String[] validateInput (String[] args) { | |
38 | 0 | if (args.length == 0) { |
39 | 0 | System.out.println("Starting helloworld with default arguments:"); |
40 | 0 | for (String arg : VALID_ARGS) { |
41 | 0 | System.out.println("- '" + arg + "'"); |
42 | } | |
43 | 0 | return VALID_ARGS; |
44 | } | |
45 | 0 | for (String arg : args) { |
46 | 0 | boolean isValid = false; |
47 | 0 | for (String test : VALID_ARGS) { |
48 | 0 | if (test.equals(arg)) { |
49 | 0 | isValid = true; |
50 | 0 | break; |
51 | } | |
52 | } | |
53 | 0 | if (!isValid) { |
54 | 0 | System.out.println("Invalid argument '" + arg + "', use one of:"); |
55 | 0 | for (String info : VALID_ARGS) { |
56 | 0 | System.out.println("- '" + info + "'"); |
57 | } | |
58 | 0 | System.exit(0); |
59 | } | |
60 | } | |
61 | 0 | return args; |
62 | } | |
63 | ||
64 | /** | |
65 | * Logs a fatal error and terminates the application. | |
66 | * @param message the message to log. | |
67 | * @param cause the cause of the fatal error. | |
68 | * @since 0.3.0 | |
69 | */ | |
70 | public static final void fatalError (String message, Throwable cause) { | |
71 | 0 | System.err.println(message); |
72 | 0 | if (cause != null) { |
73 | 0 | cause.printStackTrace(System.err); |
74 | } | |
75 | 0 | System.exit(1); |
76 | 0 | } |
77 | ||
78 | ||
79 | //---- Constructors | |
80 | ||
81 | /** | |
82 | * Objects of this class cannot be instantiated. | |
83 | * @since 0.1.0 | |
84 | */ | |
85 | private HelloWorldUtilities () { | |
86 | 0 | super(); |
87 | 0 | } |
88 | ||
89 | } |