-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathApplication.java
More file actions
103 lines (89 loc) · 3.91 KB
/
Copy pathApplication.java
File metadata and controls
103 lines (89 loc) · 3.91 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;
public class Application {
public Application() {
}
protected static AtomicBoolean shouldRun;
protected static Logger logger;
static class ApplicationArguments {
public static AtomicInteger domainId = new AtomicInteger(0);
public static AtomicInteger sampleCount = new AtomicInteger(0);
public static AtomicInteger verbosity = new AtomicInteger(0);
public static AtomicBoolean runApplication = new AtomicBoolean(true);
}
public static void parseArguments(
String[] args) throws Exception {
int argProcessing = 0;
boolean showUsage = false;
ParseReturn parseResult = ParseReturn.PARSE_RETURN_SUCCESS;
while (argProcessing < args.length) {
if (args[argProcessing] == "-d"
|| args[argProcessing] == "--domain") {
ApplicationArguments.domainId.set(
Integer.parseInt(args[argProcessing + 1]));
argProcessing += 2;
} else if (args[argProcessing] == "-s"
|| args[argProcessing] == "--sample-count") {
ApplicationArguments.sampleCount.set(
Integer.parseInt(args[argProcessing + 1]));
argProcessing += 2;
} else if (args[argProcessing] == "-v"
|| args[argProcessing] == "--verbosity") {
ApplicationArguments.verbosity.set(
Integer.parseInt(args[argProcessing + 1]));
argProcessing += 2;
} else if (args[argProcessing] == "-h"
|| args[argProcessing] == "--help") {
logger.info("Example application.");
showUsage = true;
parseResult = ParseReturn.PARSE_RETURN_EXIT;
break;
} else {
logger.severe("Bad parameter.");
showUsage = true;
parseResult = ParseReturn.PARSE_RETURN_FAILURE;
break;
}
}
if (showUsage) {
logger.info("Usage:\n" +
" -d, --domain <int> Domain ID this application will\n" +
" subscribe in. \n" +
" Default: 0\n" +
" -s, --sample_count <int> Number of samples to receive before\n" +
" cleanly shutting down. \n" +
" Default: infinite\n" +
" -v, --verbosity <int> How much debugging output to show.\n" +
" Range: 0-5 \n" +
" Default: 0");
}
if (parseResult == ParseReturn.PARSE_RETURN_FAILURE) {
throw new Exception("Bad parameter passed to applicaiton");
} else {
ApplicationArguments.runApplication.set(true);
}
}
public static void handleShutdown() {
shouldRun = new AtomicBoolean(true);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
logger.info("Shutting down...");
shouldRun.set(false);
}
});
}
public static void setUpLogging() {
logger = Logger.getLogger(HelloMessagePublisher.class.getName());
logger.setUseParentHandlers(false);
logger.addHandler(new ConsoleHandler() {
{setOutputStream(System.out);}
});
}
enum ParseReturn {
PARSE_RETURN_FAILURE,
PARSE_RETURN_SUCCESS,
PARSE_RETURN_EXIT
}
}