23
23
import java .util .HashMap ;
24
24
import java .util .Map ;
25
25
import java .util .Optional ;
26
+ import java .util .logging .Level ;
27
+ import java .util .logging .Logger ;
26
28
27
29
public final class CommandManager {
28
30
private final ArrayList <Command > mCommandMgr = new ArrayList <Command >();
29
31
private final Map <String , ArrayList <Command >> mClusters =
30
32
new HashMap <String , ArrayList <Command >>();
33
+ private static Logger logger = Logger .getLogger (CommandManager .class .getName ());
31
34
32
35
public final void register (String clusterName , ArrayList <Command > commandsList ) {
33
36
mClusters .put (clusterName , commandsList );
@@ -37,20 +40,20 @@ public final void run(String[] args) {
37
40
Command command ;
38
41
39
42
if (args .length < 1 ) {
40
- System . out . println ( "Missing cluster name" );
43
+ logger . log ( Level . INFO , "Missing cluster name" );
41
44
showClusters ();
42
45
return ;
43
46
}
44
47
45
48
ArrayList <Command > commands = mClusters .get (args [0 ]);
46
49
if (commands == null ) {
47
- System . out . println ( "Unknown cluster: " + args [0 ]);
50
+ logger . log ( Level . INFO , "Unknown cluster: " + args [0 ]);
48
51
showClusters ();
49
52
return ;
50
53
}
51
54
52
55
if (args .length < 2 ) {
53
- System . out . println ( "Missing command name" );
56
+ logger . log ( Level . INFO , "Missing command name" );
54
57
showCluster (args [0 ], commands );
55
58
return ;
56
59
}
@@ -64,27 +67,27 @@ public final void run(String[] args) {
64
67
}
65
68
} else if (isEventCommand (args [1 ])) {
66
69
if (args .length < 3 ) {
67
- System . out . println ( "Missing event name" );
70
+ logger . log ( Level . INFO , "Missing event name" );
68
71
showClusterEvents (args [0 ], args [1 ], commands );
69
72
throw new IllegalArgumentException ();
70
73
}
71
74
72
75
command = getGlobalCommand (commands , args [1 ], args [2 ]);
73
76
if (command == null ) {
74
- System . out . println ( "Unknown event: " + args [2 ]);
77
+ logger . log ( Level . INFO , "Unknown event: " + args [2 ]);
75
78
showClusterEvents (args [0 ], args [1 ], commands );
76
79
throw new IllegalArgumentException ();
77
80
}
78
81
} else {
79
82
if (args .length < 3 ) {
80
- System . out . println ( "Missing attribute name" );
83
+ logger . log ( Level . INFO , "Missing attribute name" );
81
84
showClusterAttributes (args [0 ], args [1 ], commands );
82
85
throw new IllegalArgumentException ();
83
86
}
84
87
85
88
command = getGlobalCommand (commands , args [1 ], args [2 ]);
86
89
if (command == null ) {
87
- System . out . println ( "Unknown attribute: " + args [2 ]);
90
+ logger . log ( Level . INFO , "Unknown attribute: " + args [2 ]);
88
91
showClusterAttributes (args [0 ], args [1 ], commands );
89
92
throw new IllegalArgumentException ();
90
93
}
@@ -100,7 +103,7 @@ public final void run(String[] args) {
100
103
System .out .println ("Run command failed with exception: " + e .getMessage ());
101
104
showCommand (args [0 ], command );
102
105
} catch (Exception e ) {
103
- System . out . println ( "Run command failed with exception: " + e .getMessage ());
106
+ logger . log ( Level . INFO , "Run command failed with exception: " + e .getMessage ());
104
107
}
105
108
}
106
109
@@ -140,34 +143,43 @@ private Command getGlobalCommand(
140
143
}
141
144
142
145
private void showClusters () {
143
- System .out .println ("Usage:" );
144
- System .out .println (" java-matter-controller cluster_name command_name [param1 param2 ...]" );
145
- System .out .println ("\n " );
146
- System .out .println (
146
+ logger .log (Level .INFO , "Usage:" );
147
+ logger .log (
148
+ Level .INFO , " java-matter-controller cluster_name command_name [param1 param2 ...]" );
149
+ logger .log (Level .INFO , "\n " );
150
+ logger .log (
151
+ Level .INFO ,
147
152
" +-------------------------------------------------------------------------------------+" );
148
- System .out .println (
153
+ logger .log (
154
+ Level .INFO ,
149
155
" | Clusters: |" );
150
- System .out .println (
156
+ logger .log (
157
+ Level .INFO ,
151
158
" +-------------------------------------------------------------------------------------+" );
152
159
153
160
for (String key : mClusters .keySet ()) {
154
161
System .out .printf (" | * %-82s|\n " , key .toLowerCase ());
155
162
}
156
163
157
- System .out .println (
164
+ logger .log (
165
+ Level .INFO ,
158
166
" +-------------------------------------------------------------------------------------+" );
159
167
}
160
168
161
169
private void showCluster (String clusterName , ArrayList <Command > commands ) {
162
- System .out .println ("Usage:" );
163
- System .out .println (
170
+ logger .log (Level .INFO , "Usage:" );
171
+ logger .log (
172
+ Level .INFO ,
164
173
" java-matter-controller " + clusterName + " command_name [param1 param2 ...]" );
165
- System .out .println ("\n " );
166
- System .out .println (
174
+ logger .log (Level .INFO , "\n " );
175
+ logger .log (
176
+ Level .INFO ,
167
177
" +-------------------------------------------------------------------------------------+" );
168
- System .out .println (
178
+ logger .log (
179
+ Level .INFO ,
169
180
" | Commands: |" );
170
- System .out .println (
181
+ logger .log (
182
+ Level .INFO ,
171
183
" +-------------------------------------------------------------------------------------+" );
172
184
boolean readCommand = false ;
173
185
boolean writeCommand = false ;
@@ -198,57 +210,66 @@ private void showCluster(String clusterName, ArrayList<Command> commands) {
198
210
System .out .printf (" | * %-82s|\n " , cmdName );
199
211
}
200
212
}
201
- System .out .println (
213
+ logger .log (
214
+ Level .INFO ,
202
215
" +-------------------------------------------------------------------------------------+\n " );
203
216
}
204
217
205
218
private void showClusterAttributes (
206
219
String clusterName , String commandName , ArrayList <Command > commands ) {
207
- System . out . println ( "Usage:" );
220
+ logger . log ( Level . INFO , "Usage:" );
208
221
System .out .printf (
209
222
" java-matter-controller %s %s attribute-name [param1 param2 ...]\n " ,
210
223
clusterName , commandName );
211
- System .out .println ("\n " );
212
- System .out .println (
224
+ logger .log (Level .INFO , "\n " );
225
+ logger .log (
226
+ Level .INFO ,
213
227
" +-------------------------------------------------------------------------------------+" );
214
- System .out .println (
228
+ logger .log (
229
+ Level .INFO ,
215
230
" | Attributes: |" );
216
- System .out .println (
231
+ logger .log (
232
+ Level .INFO ,
217
233
" +-------------------------------------------------------------------------------------+" );
218
234
for (Command command : commands ) {
219
235
if (commandName .equals (command .getName ())) {
220
236
System .out .printf (" | * %-82s|\n " , command .getAttribute ().get ());
221
237
}
222
238
}
223
- System .out .println (
239
+ logger .log (
240
+ Level .INFO ,
224
241
" +-------------------------------------------------------------------------------------+" );
225
242
}
226
243
227
244
private void showClusterEvents (
228
245
String clusterName , String commandName , ArrayList <Command > commands ) {
229
- System . out . println ( "Usage:" );
246
+ logger . log ( Level . INFO , "Usage:" );
230
247
System .out .printf (
231
248
" java-matter-controller %s %s event-name [param1 param2 ...]\n " ,
232
249
clusterName , commandName );
233
- System .out .println ("\n " );
234
- System .out .println (
250
+ logger .log (Level .INFO , "\n " );
251
+ logger .log (
252
+ Level .INFO ,
235
253
" +-------------------------------------------------------------------------------------+" );
236
- System .out .println (
254
+ logger .log (
255
+ Level .INFO ,
237
256
" | Events: |" );
238
- System .out .println (
257
+ logger .log (
258
+ Level .INFO ,
239
259
" +-------------------------------------------------------------------------------------+" );
240
260
241
261
for (Command command : commands ) {
242
262
if (commandName .equals (command .getName ())) {
243
263
System .out .printf (" | * %-82s|\n " , command .getAttribute ().get ());
244
264
}
245
265
}
246
- System .out .println (
266
+ logger .log (
267
+ Level .INFO ,
247
268
" +-------------------------------------------------------------------------------------+" );
248
269
}
249
270
250
271
private void showCommand (String clusterName , Command command ) {
251
- System . out . println ( "Usage:" );
272
+ logger . log ( Level . INFO , "Usage:" );
252
273
253
274
String arguments = command .getName ();
254
275
String description = "" ;
@@ -284,7 +305,7 @@ private void showCommand(String clusterName, Command command) {
284
305
}
285
306
286
307
if (!description .isEmpty ()) {
287
- System . out . println ( description );
308
+ logger . log ( Level . INFO , description );
288
309
}
289
310
}
290
311
}
0 commit comments