1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.owasp.dependencycheck;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.LogManager;
28 import java.util.logging.Logger;
29 import org.apache.commons.cli.ParseException;
30 import org.owasp.dependencycheck.reporting.ReportGenerator;
31 import org.owasp.dependencycheck.dependency.Dependency;
32 import org.owasp.dependencycheck.cli.CliParser;
33 import org.owasp.dependencycheck.utils.Settings;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class App {
58
59
60
61
62 private static final String LOG_PROPERTIES_FILE = "log.properties";
63
64
65
66
67
68
69 public static void main(String[] args) {
70 prepareLogger();
71 final App app = new App();
72 app.run(args);
73 }
74
75
76
77
78 private static void prepareLogger() {
79 InputStream in = null;
80 try {
81 in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
82 LogManager.getLogManager().reset();
83 LogManager.getLogManager().readConfiguration(in);
84 } catch (IOException ex) {
85 Logger.getLogger(App.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex);
86 } catch (SecurityException ex) {
87 Logger.getLogger(App.class.getName()).log(Level.FINE, "Error preparing the logger", ex);
88 } finally {
89 if (in != null) {
90 try {
91 in.close();
92 } catch (Exception ex) {
93 Logger.getLogger(App.class.getName()).log(Level.FINEST, "Error closing resource stream", ex);
94 }
95 }
96 }
97 }
98
99
100
101
102
103
104 public void run(String[] args) {
105
106 final CliParser cli = new CliParser();
107 try {
108 cli.parse(args);
109 } catch (FileNotFoundException ex) {
110 System.err.println(ex.getMessage());
111 cli.printHelp();
112 return;
113 } catch (ParseException ex) {
114 System.err.println(ex.getMessage());
115 cli.printHelp();
116 return;
117 }
118
119 if (cli.isGetVersion()) {
120 cli.printVersionInfo();
121 } else if (cli.isRunScan()) {
122 updateSettings(cli.isAutoUpdate(), cli.getConnectionTimeout(), cli.getProxyUrl(), cli.getProxyPort(), cli.getDataDirectory());
123 runScan(cli.getReportDirectory(), cli.getReportFormat(), cli.getApplicationName(), cli.getScanFiles());
124 } else {
125 cli.printHelp();
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139 private void runScan(String reportDirectory, String outputFormat, String applicationName, String[] files) {
140 final Engine scanner = new Engine();
141
142 for (String file : files) {
143 scanner.scan(file);
144 }
145
146 scanner.analyzeDependencies();
147 final List<Dependency> dependencies = scanner.getDependencies();
148
149 final ReportGenerator report = new ReportGenerator(applicationName, dependencies, scanner.getAnalyzers());
150 try {
151 report.generateReports(reportDirectory, outputFormat);
152 } catch (IOException ex) {
153 Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an IO error while attempting to generate the report.");
154 Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
155 } catch (Exception ex) {
156 Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an error while attempting to generate the report.");
157 Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 private void updateSettings(boolean autoUpdate, String connectionTimeout, String proxyUrl, String proxyPort, String dataDirectory) {
173 if (dataDirectory != null) {
174 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
175 } else if (System.getProperty("basedir") != null) {
176 final File dataDir = new File(System.getProperty("basedir"), "data");
177 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
178 } else {
179 final File jarPath = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath());
180 final File base = jarPath.getParentFile();
181 final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
182 final File dataDir = new File(base, sub);
183 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
184 }
185
186
187 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
188 if (proxyUrl != null && !proxyUrl.isEmpty()) {
189 Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
190 }
191 if (proxyPort != null && !proxyPort.isEmpty()) {
192 Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
193 }
194 if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
195 Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
196 }
197 }
198 }