This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCommandFactory.java
More file actions
316 lines (271 loc) · 9.67 KB
/
CommandFactory.java
File metadata and controls
316 lines (271 loc) · 9.67 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.gbase.cmdline;
import java.net.MalformedURLException;
import java.util.Arrays;
/**
* Creates and initializes commands given command-line
* arguments.
*
* This class is totally useless if you want to understand
* how the API works. All it does is parse command-line
* arguments and call some setters on the different <code>*Command</code>
* objects. Have a look at the <code>*Command</code> classes instead.
*/
class CommandFactory {
/**
* All commands available in this system, for producing error messages.
*/
private static final String ALL_COMMANDS =
"query, get, update, insert, delete, batch";
/**
* Creates a {@link Command} object and initializes it using
* command-line arguments.
*
* @param args command-line arguments
* @return a new Command object, properly initialized and ready to use
*/
public static Command createCommand(String[] args) {
if (args.length == 0) {
throw error("Please give first the command you want to run (" +
ALL_COMMANDS + ") then the parameters for that " +
"command.");
}
String commandName = args[0];
if ("query".equals(commandName)) {
return createQueryCommand(args);
} else if("insert".equals(commandName)) {
return createInsertCommand(args);
} else if("update".equals(commandName)) {
return createUpdateCommand(args);
} else if("delete".equals(commandName)) {
return createDeleteCommand(args);
} else if("get".equals(commandName)) {
return createGetCommand(args);
} else if("batch".equals(commandName)) {
return createBatchCommand(args);
} else if("query-media".equals(commandName)) {
return createQueryMediaCommand(args);
} else if("insert-media".equals(commandName)) {
return createInsertMediaCommand(args);
} else if("update-media".equals(commandName)) {
return createUpdateMediaCommand(args);
} else if("delete-media".equals(commandName)) {
return createDeleteMediaCommand(args);
} else if("get-media".equals(commandName)) {
return createGetMediaCommand(args);
} else {
throw error("Unknown command: " + commandName +
". Available commands: " + ALL_COMMANDS);
}
}
/**
* Creates and initializes a {@link QueryCommand}.
*/
private static Command createQueryCommand(String[] args) {
QueryCommand command = new QueryCommand();
args = parseAndSetCommonArguments(command, args);
if (args.length == 1) {
command.setQuery(args[0]);
} else if (args.length > 1) {
throw error("Expected at most one query argument, got " + args.length);
}
return command;
}
/**
* Creates and initializes a {@link InsertCommand}.
*/
private static Command createInsertCommand(String[] args) {
InsertCommand command = new InsertCommand();
args = parseAndSetCommonArguments(command, args);
expectNoMoreArguments(args);
return command;
}
/**
* Creates and initializes a {@link BatchCommand}.
*/
private static Command createBatchCommand(String[] args) {
BatchCommand command = new BatchCommand();
args = parseAndSetCommonArguments(command, args);
expectNoMoreArguments(args);
return command;
}
/**
* Creates and initializes a {@link UpdateCommand}.
*/
private static Command createUpdateCommand(String[] args) {
UpdateCommand command = new UpdateCommand();
args = parseAndSetCommonArguments(command, args);
command.setItemId(getItemId(args));
return command;
}
/**
* Creates and initializes a {@link DeleteCommand}.
*/
private static Command createDeleteCommand(String[] args) {
DeleteCommand command = new DeleteCommand();
args = parseAndSetCommonArguments(command, args);
command.setItemId(getItemId(args));
return command;
}
/**
* Creates and initializes a {@link GetCommand}.
*/
private static Command createGetCommand(String[] args) {
GetCommand command = new GetCommand();
args = parseAndSetCommonArguments(command, args);
command.setItemId(getItemId(args));
return command;
}
/**
* Creates and initializes a {@link QueryMediaCommand}.
*/
private static Command createQueryMediaCommand(String[] args) {
QueryMediaCommand command = new QueryMediaCommand();
args = parseAndSetCommonArguments(command, args);
if (args.length == 1) {
command.setItemMediaUrl(args[0]);
} else {
throw error("Expected [item_media_feed_url], got " + Arrays.toString(args));
}
return command;
}
/**
* Creates and initializes a {@link InsertMediaCommand}.
*/
private static Command createInsertMediaCommand(String[] args) {
InsertMediaCommand command = new InsertMediaCommand();
args = parseAndSetCommonArguments(command, args);
if (args.length < 3 || args.length > 4) {
throw error("Expected four arguments: [item_media_feed_url attachment_path_to_file " +
"attachment_mime_type caption?], got " + Arrays.toString(args));
}
command.setItemMediaUrl(args[0]);
command.setAttachmentFile(args[1]);
command.setAttachmentMimeType(args[2]);
if (args.length == 4) {
command.setCaption(args[3]);
}
return command;
}
/**
* Creates and initializes a {@link UpdateMediaCommand}.
*/
private static Command createUpdateMediaCommand(String[] args) {
UpdateMediaCommand command = new UpdateMediaCommand();
args = parseAndSetCommonArguments(command, args);
command.setAttachmentId(getItemId(args));
return command;
}
/**
* Creates and initializes a {@link DeleteMediaCommand}.
*/
private static Command createDeleteMediaCommand(String[] args) {
DeleteMediaCommand command = new DeleteMediaCommand();
args = parseAndSetCommonArguments(command, args);
command.setAttachmentId(getItemId(args));
return command;
}
/**
* Creates and initializes a {@link GetMediaCommand).
*/
private static Command createGetMediaCommand(String[] args) {
GetMediaCommand command = new GetMediaCommand();
args = parseAndSetCommonArguments(command, args);
command.setAttachmentId(getItemId(args));
return command;
}
/**
* Get the item id from the command line and make sure it's the
* only argument left.
*
* @param args command line arguments left over from
* {@link #parseAndSetCommonArguments(Command, String[])}
* @return the item ID (an url)
*/
private static String getItemId(String[] args) {
if (args.length != 1) {
throw error("Expected one argument after the command name: " +
"the ID (url) of the item to delete.");
}
return args[0];
}
/**
* Parse the command line and initialize things that are common
* to all {@link Command}s.
*
* @param command the command object
* @param args command-line arguments (0 is the command name)
* @return the arguments that are left that haven't been parsed
* by this method, or an empty array if there's nothing left
*/
private static String[] parseAndSetCommonArguments(Command command,
String[] args) {
// Start at the first argument after the command name
int current = 1;
while (current < args.length && args[current].startsWith("-")) {
String option = args[current];
current++;
if ( current >= args.length) {
throw error("expected an argument after option " + option);
}
if ("--dry_run".equals(option)) {
command.setDryRun(true);
continue;
}
String value = args[current];
current++;
if ("--user".equals(option)) {
command.setUsername(value);
} else if("--password".equals(option)) {
command.setPassword(value);
} else if("--url".equals(option)) {
try {
command.setGoogleBaseServerUrl(value);
} catch(MalformedURLException e) {
throw error("Value for --url should be a valid URL: " +
e.getMessage());
}
} else if("--auth".equals(option)) {
try {
command.setAuthenticationServerUrl(value);
} catch(MalformedURLException e) {
throw error("Value for --auth should be a valid http or https " +
"URL: " + e.getMessage());
}
} else if("--key".equals(option)) {
command.setKey(value);
} else {
throw error("unexpected option: " + option);
}
}
if (!command.hasAllIdentificationInformation()) {
throw error("You must input all two required parameters: " +
"--user email --password password ");
}
// leave the rest for the command
String[] retval = new String[args.length-current];
System.arraycopy(args, current, retval, 0, retval.length);
return retval;
}
private static void expectNoMoreArguments(String[] args) {
if (args.length > 0) {
throw error("Expected no more arguments, got instead " + args[0]);
}
}
private static IllegalArgumentException error(String message) {
return new IllegalArgumentException("Error: wrong arguments. " + message);
}
}