-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDriver.java
More file actions
executable file
·71 lines (53 loc) · 1.7 KB
/
ClientDriver.java
File metadata and controls
executable file
·71 lines (53 loc) · 1.7 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
/**
* A driver class for WebClient
*
* One command line argument is required:
* -u: the URL of the object to be downloaded
*
*/
import java.io.*;
import java.util.*;
import java.util.logging.*;
public class ClientDriver {
private static final Logger logger = Logger.getLogger("WebClient");
public static void main(String[] args) {
// input URL is required
if (args.length == 0) {
System.out.println("incorrect usage, input URL is required");
System.out.println("try again");
System.exit(0);
}
// parse command line args
HashMap<String, String> params = parseCommandLine(args);
// set the parameters
String url = params.getOrDefault("-u", args[0]); // object url
Level logLevel = Level.parse( params.getOrDefault("-v", "all").toUpperCase() ); // log levels: all, info, off
// set log level
setLogLevel(logLevel);
WebClient client = new WebClient();
System.out.printf("downloading %s...\n", url);
client.getObject(url);
System.out.println("download completed.");
// get rid of any lingering threads/timers
System.exit(0);
}
// parse command line arguments
private static HashMap<String, String> parseCommandLine(String[] args) {
HashMap<String, String> params = new HashMap<String, String>();
int i = 0;
while ((i + 1) < args.length) {
params.put(args[i], args[i+1]);
i += 2;
}
return params;
}
// set the global log level and format
private static void setLogLevel(Level level) {
System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n");
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(level);
logger.addHandler(handler);
logger.setLevel(level);
logger.setUseParentHandlers(false);
}
}