Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelloWorldUtilities |
|
| 5.333333333333333;5.333 |
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 | import java.util.ArrayList; | |
13 | import java.util.List; | |
14 | ||
15 | /** | |
16 | * Helper functions used by the sample. | |
17 | * <p>The helper functions have been moved here to prevent that the reader of the hello world | |
18 | * gets distracted by auxiliary helper functions.</p> | |
19 | * @since 0.1.0 | |
20 | */ | |
21 | public final class HelloWorldUtilities { | |
22 | ||
23 | //---- Static | |
24 | ||
25 | /** | |
26 | * @since 0.4.0 | |
27 | */ | |
28 | public static final String SARA = "cn=sara,o=post,c=ch"; | |
29 | ||
30 | /** | |
31 | * @since 0.4.0 | |
32 | */ | |
33 | public static final String JOHN = "cn=john,o=post,c=ch"; | |
34 | ||
35 | /** | |
36 | * @since 0.4.0 | |
37 | */ | |
38 | public static final String INSERT_LETTER = "insertLetter"; | |
39 | ||
40 | /** | |
41 | * @since 0.4.0 | |
42 | */ | |
43 | public static final String COLLECT_LETTERS = "collectLetters"; | |
44 | ||
45 | /** | |
46 | * List of valid command line arguments. | |
47 | */ | |
48 | private static final List<String[]> VALID_ARGS_LIST; | |
49 | static { | |
50 | 0 | VALID_ARGS_LIST = new ArrayList<String[]>(); |
51 | 0 | VALID_ARGS_LIST.add(new String[] {SARA, INSERT_LETTER}); |
52 | 0 | VALID_ARGS_LIST.add(new String[] {SARA, COLLECT_LETTERS}); |
53 | 0 | VALID_ARGS_LIST.add(new String[] {JOHN, INSERT_LETTER}); |
54 | 0 | VALID_ARGS_LIST.add(new String[] {JOHN, COLLECT_LETTERS}); |
55 | } | |
56 | ||
57 | 0 | private static final String[] VALID_PRINCIPALS = {JOHN, SARA}; |
58 | ||
59 | 0 | private static final String[] VALID_ACTIONS = {INSERT_LETTER, COLLECT_LETTERS}; |
60 | ||
61 | /** | |
62 | * Simple helper function to validate the command line arguments. | |
63 | * @param args the arguments to validate. | |
64 | * @return the input arguments to run the hello world with. | |
65 | * @since 0.1.0 | |
66 | */ | |
67 | public static List<String[]> validateInput (String[] args) { | |
68 | 0 | if (args.length == 0) { |
69 | 0 | System.out.println("Starting helloworld 4 times with default arguments:\n"); |
70 | 0 | return VALID_ARGS_LIST; |
71 | } | |
72 | ||
73 | 0 | if (args.length == 2) { |
74 | // Principal. | |
75 | 0 | final String principal = args[0]; |
76 | 0 | boolean isPrincipalValid = false; |
77 | 0 | for (String test : VALID_PRINCIPALS) { |
78 | 0 | if (test.equals(principal)) { |
79 | 0 | isPrincipalValid = true; |
80 | 0 | break; |
81 | } | |
82 | } | |
83 | 0 | if (!isPrincipalValid) { |
84 | 0 | System.out.println("Invalid principal argument '" + principal + "'"); |
85 | } | |
86 | ||
87 | // Action. | |
88 | 0 | final String action = args[1]; |
89 | 0 | boolean isActionValid = false; |
90 | 0 | for (String test : VALID_ACTIONS) { |
91 | 0 | if (test.equals(action)) { |
92 | 0 | isActionValid = true; |
93 | 0 | break; |
94 | } | |
95 | } | |
96 | 0 | if (!isActionValid) { |
97 | 0 | System.out.println("Invalid action argument '" + action + "'"); |
98 | } | |
99 | ||
100 | 0 | if (isActionValid && isPrincipalValid) { |
101 | 0 | final List<String[]> result = new ArrayList<String[]>(); |
102 | 0 | result.add(args); |
103 | 0 | return result; |
104 | } | |
105 | } | |
106 | ||
107 | 0 | System.out.println("Invalid number of arguments."); |
108 | 0 | System.exit(0); |
109 | 0 | return null; |
110 | } | |
111 | ||
112 | /** | |
113 | * Logs a fatal error and terminates the application. | |
114 | * @param message the message to log. | |
115 | * @param cause the cause of the fatal error. | |
116 | * @since 0.3.0 | |
117 | */ | |
118 | public static final void fatalError (String message, Throwable cause) { | |
119 | 0 | System.err.println(message); |
120 | 0 | if (cause != null) { |
121 | 0 | cause.printStackTrace(System.err); |
122 | } | |
123 | 0 | System.exit(1); |
124 | 0 | } |
125 | ||
126 | ||
127 | //---- Constructors | |
128 | ||
129 | /** | |
130 | * Objects of this class cannot be instantiated. | |
131 | * @since 0.1.0 | |
132 | */ | |
133 | private HelloWorldUtilities () { | |
134 | 0 | super(); |
135 | 0 | } |
136 | ||
137 | } |