/
ServerConfigCommand.java
150 lines (111 loc) · 4.78 KB
/
ServerConfigCommand.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.hyperic.hq.hqapi1.tools;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import java.util.Arrays;
import java.util.List;
import java.io.InputStream;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.ServerConfigApi;
import org.hyperic.hq.hqapi1.types.ServerConfigResponse;
import org.hyperic.hq.hqapi1.types.ServerConfig;
import org.hyperic.hq.hqapi1.types.StatusResponse;
public class ServerConfigCommand extends Command {
private static String CMD_GET = "get";
private static String CMD_GET_PARAMETER = "getParameter";
private static String CMD_SET = "set";
private static String CMD_SET_PARAMETER = "setParameter";
private static String[] COMMANDS = { CMD_GET, CMD_GET_PARAMETER,
CMD_SET, CMD_SET_PARAMETER};
private static String OPT_KEY = "key";
private static String OPT_VALUE = "value";
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}
protected void handleCommand(String[] args) throws Exception {
if (args.length == 0) {
printUsage();
System.exit(-1);
}
if (args[0].equals(CMD_GET)) {
get(trim(args));
} else if (args[0].equals(CMD_GET_PARAMETER)) {
getParameter(trim(args));
} else if (args[0].equals(CMD_SET)) {
set(trim(args));
} else if (args[0].equals(CMD_SET_PARAMETER)) {
setParameter(trim(args));
} else {
printUsage();
System.exit(-1);
}
}
private void get(String[] args) throws Exception {
OptionParser p = getOptionParser();
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();
ServerConfigResponse response = configApi.getConfig();
checkSuccess(response);
XmlUtil.serialize(response, System.out, Boolean.TRUE);
}
private void getParameter(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_KEY, "The configuration parameter to retreive").
withRequiredArg().ofType(String.class);
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();
String key = (String)getRequired(options, OPT_KEY);
ServerConfigResponse response = configApi.getConfig();
checkSuccess(response);
for (ServerConfig c : response.getServerConfig()) {
if (c.getKey().equals(key)) {
System.out.println("Current value for " + key + " = " + c.getValue());
return;
}
}
System.err.print("Unknown configuration parameter " + key);
}
private void set(String[] args) throws Exception {
OptionParser p = getOptionParser();
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
ServerConfigApi serverConfigApi = api.getServerConfigApi();
InputStream is = getInputStream(options);
ServerConfigResponse resp = XmlUtil.deserialize(ServerConfigResponse.class, is);
List<ServerConfig> config = resp.getServerConfig();
StatusResponse response = serverConfigApi.setConfig(config);
checkSuccess(response);
System.out.println("Successfully updated HQ configuration.");
}
private void setParameter(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_KEY, "The configuration parameter to set").
withRequiredArg().ofType(String.class);
p.accepts(OPT_VALUE, "The new value for the specified key").
withRequiredArg().ofType(String.class);
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();
String key = (String)getRequired(options, OPT_KEY);
String value = (String)getRequired(options, OPT_VALUE);
ServerConfigResponse response = configApi.getConfig();
checkSuccess(response);
List<ServerConfig> config = response.getServerConfig();
boolean found = false;
for (ServerConfig c : config) {
if (c.getKey().equals(key)) {
c.setValue(value);
found = true;
}
}
if (!found) {
System.err.print("Unknown configuration parameter " + key);
} else {
StatusResponse setResponse = configApi.setConfig(config);
checkSuccess(setResponse);
System.out.println("Successfully updated HQ configuration.");
}
}
}