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.Logger;
28 import org.apache.commons.cli.ParseException;
29 import org.owasp.dependencycheck.reporting.ReportGenerator;
30 import org.owasp.dependencycheck.dependency.Dependency;
31 import org.owasp.dependencycheck.cli.CliParser;
32 import org.owasp.dependencycheck.utils.LogUtils;
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 final App app = new App();
71 app.run(args);
72 }
73
74
75
76
77
78
79 public void run(String[] args) {
80
81 final CliParser cli = new CliParser();
82 try {
83 cli.parse(args);
84 } catch (FileNotFoundException ex) {
85 System.err.println(ex.getMessage());
86 cli.printHelp();
87 return;
88 } catch (ParseException ex) {
89 System.err.println(ex.getMessage());
90 cli.printHelp();
91 return;
92 }
93
94 final InputStream in = App.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
95 LogUtils.prepareLogger(in, cli.getVerboseLog());
96
97 if (cli.isGetVersion()) {
98 cli.printVersionInfo();
99 } else if (cli.isRunScan()) {
100 updateSettings(cli.isAutoUpdate(), cli.getConnectionTimeout(), cli.getProxyUrl(),
101 cli.getProxyPort(), cli.getDataDirectory(), cli.getPropertiesFile());
102 runScan(cli.getReportDirectory(), cli.getReportFormat(), cli.getApplicationName(), cli.getScanFiles());
103 } else {
104 cli.printHelp();
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118 private void runScan(String reportDirectory, String outputFormat, String applicationName, String[] files) {
119 final Engine scanner = new Engine();
120
121 for (String file : files) {
122 scanner.scan(file);
123 }
124
125 scanner.analyzeDependencies();
126 final List<Dependency> dependencies = scanner.getDependencies();
127
128 final ReportGenerator report = new ReportGenerator(applicationName, dependencies, scanner.getAnalyzers());
129 try {
130 report.generateReports(reportDirectory, outputFormat);
131 } catch (IOException ex) {
132 Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an IO error while attempting to generate the report.");
133 Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
134 } catch (Exception ex) {
135 Logger.getLogger(App.class.getName()).log(Level.SEVERE, "There was an error while attempting to generate the report.");
136 Logger.getLogger(App.class.getName()).log(Level.INFO, null, ex);
137 }
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152 private void updateSettings(boolean autoUpdate, String connectionTimeout, String proxyUrl,
153 String proxyPort, String dataDirectory, File propertiesFile) {
154
155 if (propertiesFile != null) {
156 try {
157 Settings.mergeProperties(propertiesFile);
158 } catch (FileNotFoundException ex) {
159 final String msg = String.format("Unable to load properties file '%s'", propertiesFile.getPath());
160 Logger.getLogger(App.class.getName()).log(Level.SEVERE, msg);
161 Logger.getLogger(App.class.getName()).log(Level.FINE, null, ex);
162 } catch (IOException ex) {
163 final String msg = String.format("Unable to find properties file '%s'", propertiesFile.getPath());
164 Logger.getLogger(App.class.getName()).log(Level.SEVERE, msg);
165 Logger.getLogger(App.class.getName()).log(Level.FINE, null, ex);
166 }
167 }
168 if (dataDirectory != null) {
169 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
170 } else if (System.getProperty("basedir") != null) {
171 final File dataDir = new File(System.getProperty("basedir"), "data");
172 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
173 } else {
174 final File jarPath = new File(App.class.getProtectionDomain().getCodeSource().getLocation().getPath());
175 final File base = jarPath.getParentFile();
176 final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
177 final File dataDir = new File(base, sub);
178 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
179 }
180 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
181 if (proxyUrl != null && !proxyUrl.isEmpty()) {
182 Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
183 }
184 if (proxyPort != null && !proxyPort.isEmpty()) {
185 Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
186 }
187 if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
188 Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
189 }
190 }
191 }