-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineHandler.java
185 lines (138 loc) · 5.92 KB
/
CommandLineHandler.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
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
package marcolino.elio.mpj;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import marcolino.elio.mpj.artifactory.ArtifactoryClient;
import marcolino.elio.mpj.worker.dto.ArtifactDownloadCount;
/**
* Handles executions of this app through command line
* @author elio
*
*/
public class CommandLineHandler {
private static final Logger logger = Logger.getLogger(CommandLineHandler.class.getName());
private static final int EXIT_SUCCESS = 0;
private static final int EXIT_ERROR = 1;
private static final int EXIT_INVALID_ARGUMENTS_ERROR = 2;
private String url;
private String auth;
private String repo;
private int size = 2;
private int workers = 1;
private int threads = 1;
private int page = 0;
private HelpFormatter helpFormatter;
public CommandLineHandler(HelpFormatter helpFormatter) {
this.helpFormatter = helpFormatter;
}
public void printHelp(Options options) {
helpFormatter.printHelp(" ", options );
}
public Options configureOptions() {
Options options = new Options();
options.addOption("u", "url", true, "Artifactory url in format http|s://host:port/artifactory");
options.addOption("r", "repo", true, "Repository to search for the most popular jar files");
options.addOption("a", "auth", true, "Artifactory authentication token");
options.addOption("s", "size", true, "(Optional) Ranking size. Default: 2");
options.addOption("w", "workers", true, "(Optional) Max number of concurrent workers. Default: 1");
options.addOption("t", "threads", true, "(Optional) Max number of concurrent threads per worker. Default: 1");
options.addOption("p", "page", true, "Page size per worker. Optional if authentication token provided has admin privileges. In this case will use Artifactory instance artifactory.search.userQueryLimit property value");
options.addOption("h", "help", false, "Print this message");
return options;
}
public boolean parseArguments(String args[]) {
CommandLineParser parser = new DefaultParser();
Options options = configureOptions();
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("help")){
printHelp(options);
return false;
}
if(!line.hasOption("url") ||
!line.hasOption("repo") ||
!line.hasOption("auth") ){
System.out.println("Invalid arguments: showing -h option");
printHelp(options);
throw new IllegalArgumentException();
}
url = line.getOptionValue("url");
auth = line.getOptionValue("auth");
repo = line.getOptionValue("repo");
if (line.hasOption("size")) {
size = Integer.parseInt(line.getOptionValue("size"));
}
if (line.hasOption("workers")) {
workers = Integer.parseInt(line.getOptionValue("workers"));
}
if (line.hasOption("threads")) {
threads = Integer.parseInt(line.getOptionValue("threads"));
}
if (line.hasOption("page")) {
page = Integer.parseInt(line.getOptionValue("page"));
}
} catch (NumberFormatException | ParseException e) {
System.out.println("Invalid arguments: showing -h option");
printHelp(options);
throw new IllegalArgumentException();
}
return true;
}
public int execute(String args[]) {
try {
if (parseArguments(args)) {
long initialTime = System.currentTimeMillis();
ArtifactoryClient artifactoryClient = new ArtifactoryClient(url, auth);
ArtifactoryFacade artifactoryFacade = new ArtifactoryFacade(artifactoryClient);
List<ArtifactDownloadCount> ranking = artifactoryFacade.getMostPopularJar(repo, size, workers, threads, page);
System.out.println("Results:");
for (ArtifactDownloadCount item : ranking) {
System.out.println(item.getDownloadCount() + ": " + item.getArtifact().getRepositoryPath());
}
System.out.println("Time: " + (System.currentTimeMillis() - initialTime) + " ms");
}
}
catch (IllegalArgumentException e) {
return EXIT_INVALID_ARGUMENTS_ERROR;
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
System.out.println("FAILED: " + e.getMessage());
return EXIT_ERROR;
}
return EXIT_SUCCESS;
}
public static void main(String args[]) {
CommandLineHandler handler = new CommandLineHandler(new HelpFormatter());
int result = handler.execute(args);
if (result != 0) {
System.exit(result);
}
}
public String getUrl() {
return url;
}
public String getAuth() {
return auth;
}
public String getRepo() {
return repo;
}
public int getSize() {
return size;
}
public int getWorkers() {
return workers;
}
public int getThreads() {
return threads;
}
public int getPage() {
return page;
}
}