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
|
package main;
import database.ConnectionBuilder;
import java.util.Arrays;
/**
* The main class containing the main method.
*/
public class Main {
private final ConnectionBuilder cb;
public static void main(String[] args) {
Main main;
try {
main = new Main(args);
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
System.exit(1);
return;
}
main.run();
}
private String[] leftover_params;
public Main(String[] args) {
cb = new ConnectionBuilder()
.setServerName("localhost")
.setUsername("twitter")
.setPassword("2IOC02")
.setDbName("twitter");
parseGlobalOptions(args);
}
public void run() {
try {
FarmShell shell = new FarmShell(cb);
if (leftover_params != null && leftover_params.length > 0) {
shell.execute(leftover_params);
}
shell.process_forever();
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
private void parseGlobalOptions(String[] args)
throws IllegalArgumentException {
/* parse global options */
for (int i = 0; i < args.length; i++) {
if ("--dbhost".equals(args[i])) {
cb.setServerName(getArg(args, ++i, "--dbhost"));
} else if ("--dbuser".equals(args[i])) {
cb.setUsername(getArg(args, ++i, "--dbuser"));
} else if ("--dbpass".equals(args[i])) {
cb.setPassword(getArg(args, ++i, "--dbpass"));
} else if ("--dbport".equals(args[i])) {
cb.setPort(Integer.valueOf(getArg(args, ++i, "--dbport")));
} else if ("--dbname".equals(args[i])) {
cb.setDbName(getArg(args, ++i, "--dbname"));
} else if (args[i].startsWith("-")) {
throw new IllegalArgumentException("Invalid option: " + args[i]);
} else {
leftover_params = Arrays.copyOfRange(args, i, args.length);
break;
}
}
}
private String getArg(String[] params, int index, String name) {
if (index >= params.length) {
throw new IllegalArgumentException("Missing argument for parameter " + name);
}
return params[index];
}
}
|