1 /***
2 * DPTool.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 20.01.2003 pko modification
9 * 31.10.2002 pko creation
10 *
11 * Copyright 2003 ELCA Informatique SA
12 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
13 * www.elca.ch
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public License
17 * as published by the Free Software Foundation; either version 2.1 of
18 * the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 * USA
29 */
30
31 package ch.elca.dependency;
32
33 import ch.elca.dependency.core.DependencyContext;
34 import ch.elca.dependency.core.Filter;
35 import ch.elca.dependency.gui.MainFrame;
36 import ch.elca.dependency.report.ReportException;
37 import ch.elca.dependency.report.ReportManager;
38
39 import org.apache.log4j.Logger;
40 import org.apache.log4j.PropertyConfigurator;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.Hashtable;
45
46 import bry.techbase.configuration.CommandLineConfiguration;
47 import bry.techbase.configuration.Configuration;
48
49 /***
50 * Class <code>DPTool</code> provides a user-friendly entry point to
51 * the Dependency Tool.
52 *
53 * @author Pawel Kowalski
54 * @version 1.0-beta
55 */
56 public class DPTool {
57
58 /***
59 * Log4J Logger
60 */
61 private static final Logger LOG = Logger.getLogger(DPTool.class);
62
63 /***
64 * A <code>String[][]</code> representing all the commandline
65 * arguments and whether they are optional or mandatory.
66 */
67 public static final String OPTIONS[][]
68 = new String[][] {{DPT_CONST.ROOT, CommandLineConfiguration.OPTIONAL},
69 {DPT_CONST.REPORT, CommandLineConfiguration.OPTIONAL},
70 {DPT_CONST.FILTER, CommandLineConfiguration.OPTIONAL},
71 {DPT_CONST.PERSPECTIVE, CommandLineConfiguration.OPTIONAL},
72 {DPT_CONST.NOWIN, CommandLineConfiguration.OPTIONAL},
73 {DPT_CONST.PROXY, CommandLineConfiguration.OPTIONAL},
74 {DPT_CONST.HELP, CommandLineConfiguration.OPTIONAL}};
75
76
77 //*********************************************************************************//
78 // dpt entry point
79 //*********************************************************************************/
80
81 /***
82 * A new, user friendly entry point to the Dependency Tool. The
83 * user needs not specify any commandline arguments. The
84 * neccessary information will be requesteb throug a GUI.
85 *
86 * @param argv a <code>String[]</code> value
87 * @exception IOException if an error occurs
88 */
89 public static void main(String[] argv) throws IOException {
90
91 // set up the configuration properies file for log4j
92 //
93 PropertyConfigurator.configure(Thread.currentThread()
94 .getContextClassLoader()
95 .getResource("ch/elca/dependency/log4j.properties"));
96
97 DependencyContext dependencyCtx = null;
98
99 for (boolean notCreated = true; notCreated; ) {
100 try {
101 dependencyCtx = createDependencyContext(argv);
102 notCreated = false;
103 } catch (Exception e) {
104 if (DPToolErrorDialog.showErrorDialog(e) == DPToolErrorDialog.NO_CHIOCE) {
105 System.exit(0);
106 } else {
107 argv = ArgvDialog.showArgvDialog(argv);
108 }
109 }
110
111 }
112
113 // print usage and exit
114 //
115 if (dependencyCtx.containsKey(DependencyContext.HELP_KEY)) {
116 printUsage();
117 System.exit(0);
118 }
119
120 // run dpt in batch mode
121 //
122 if (dependencyCtx.containsKey(DependencyContext.REPORT_CONFIG_KEY)) {
123
124 // perform reporting
125 //
126 try {
127 ReportManager.performReporting(dependencyCtx);
128 } catch (ReportException e) {
129 e.printStackTrace();
130 }
131
132 return;
133 }
134
135 // run dpt in interactive mode
136 //
137 if (dependencyCtx.containsKey(DependencyContext.ROOT_FILE_KEY)) {
138
139 // run dpt in interactive mode
140 //
141 boolean done = false;
142 do {
143 try {
144 MainFrame.execute(dependencyCtx);
145 done = true;
146 } catch (Exception e) {
147 if (DPToolErrorDialog.showErrorDialog(e) == DPToolErrorDialog.NO_CHIOCE) {
148 System.exit(0);
149 } else {
150 argv = ArgvDialog.showArgvDialog(argv);
151 if (argv == null) {
152 System.exit(0);
153 }
154 // try {
155 dependencyCtx = createDependencyContext(argv);
156 // } catch (Exception ex) {
157 // if (DPToolErrorDialog.showErrorDialog(e) == DPToolErrorDialog.NO_CHIOCE) {
158 // System.exit(0);
159 // } else {
160 // continue;
161 // }
162 // }
163 }
164 }
165 } while (!done);
166 return;
167 }
168
169 // repeat until execution was successful
170 //
171 boolean done = false;
172 do {
173
174 // root not specified, show argv dialog
175 //
176 argv = ArgvDialog.showArgvDialog(argv);
177 if (argv == null) {
178 System.exit(0);
179 }
180 try {
181 dependencyCtx = createDependencyContext(argv);
182 MainFrame.execute(dependencyCtx);
183 done = true;
184 } catch (Exception e) {
185 e.printStackTrace();
186 if (DPToolErrorDialog.showErrorDialog(e) == DPToolErrorDialog.NO_CHIOCE) {
187 System.exit(0);
188 }
189 }
190 } while (!done);
191
192 return;
193 }
194
195 /***
196 * Creates a Dependency Context (which is a Hashtable). This
197 * context will be used later by the Analyzer to create a
198 * DependencyModel.
199 *
200 * @param argv a <code>String[]</code> value
201 * @return a <code>DependencyContext</code> value
202 *
203 * @exception IOException if an error occurs
204 */
205 private static DependencyContext createDependencyContext(String[] argv) throws IOException {
206
207 // configure the command line tool
208 //
209 Configuration config = new Configuration();
210 CommandLineConfiguration commandLineConfig = new CommandLineConfiguration(DPT_CONST.CMDLINE_SEPARATOR,
211 OPTIONS, false);
212
213 try {
214 commandLineConfig.parse(argv, config);
215 } catch (Exception e) {
216 printUsage();
217 System.exit(-1);
218 }
219
220 DependencyContext dependencyCtx = new DependencyContext();
221
222 // root project file
223 //
224 String rootRequest
225 = config.getString(DPT_CONST.SECTION, DPT_CONST.ROOT, "").trim();
226 File rootFile = new File(rootRequest);
227
228 if (rootFile.exists()) {
229 if (rootFile.isFile() && !rootRequest.endsWith(".jar")) {
230 throw new IOException("Only Directories and jar files are accepted for analysis");
231 } else {
232 LOG.info("Root File to be analyzed: " + rootFile.getCanonicalPath().toString());
233 dependencyCtx.put(DependencyContext.ROOT_FILE_KEY, rootFile);
234 }
235 } else if (!rootRequest.equals("")) {
236 IOException ioex = new IOException("The file specified by the abstract pathname "
237 + rootFile.getCanonicalPath().toString()
238 + " does not exist");
239 throw ioex;
240 }
241
242 // report config
243 //
244 String reportRequest
245 = config.getString(DPT_CONST.SECTION, DPT_CONST.REPORT, "").trim();
246 File reportConfigFile = new File(reportRequest);
247 if (reportConfigFile.isFile()) {
248 LOG.info("Using report config: " + reportRequest);
249 dependencyCtx.put(DependencyContext.REPORT_CONFIG_KEY, reportConfigFile);
250 } else if (!reportRequest.equals("")) {
251 LOG.warn("Report config file: " + reportRequest + " does not exist");
252 printUsage();
253 System.exit(0);
254 }
255
256 // basic analysis filter
257 //
258 String basicFilter = config.getString(DPT_CONST.SECTION, DPT_CONST.FILTER, "").trim();
259 LOG.info("Basic Filter used: " + basicFilter);
260 dependencyCtx.put(DependencyContext.BASIC_FILTER_KEY, new Filter("basicFilter", basicFilter));
261
262 // perspectives
263 //
264 String perspectiveRequest
265 = config.getString(DPT_CONST.SECTION, DPT_CONST.PERSPECTIVE, "").trim();
266 File perspectiveFile = new File(perspectiveRequest);
267 if (perspectiveFile.isFile()) {
268 dependencyCtx.put(DependencyContext.PERSPECTIVES_KEY, perspectiveFile);
269 LOG.info("Using perspectives config: " + perspectiveRequest);
270 } else if (!perspectiveRequest.equals("")) {
271 LOG.warn("Perspectives config file: " + reportRequest + " does not exist");
272 printUsage();
273 System.exit(0);
274 }
275
276 // os is win?
277 //
278 String osString = config.getString(DPT_CONST.SECTION, DPT_CONST.NOWIN, "---").trim();
279 Boolean noWin = (osString.equals("---") ? Boolean.FALSE : Boolean.TRUE);
280 dependencyCtx.put(DependencyContext.NOWIN_KEY, noWin);
281
282 // proxy for layouting
283 //
284 String proxyString = config.getString(DPT_CONST.SECTION, DPT_CONST.PROXY, "").trim();
285 if (!proxyString.equals("")) {
286 dependencyCtx.put(DependencyContext.PROXY_KEY, proxyString);
287 }
288
289 // help only requested?
290 //
291 try {
292 String helpRequest = config.getString(DPT_CONST.SECTION, DPT_CONST.HELP).trim();
293 dependencyCtx.put(DependencyContext.HELP_KEY, DependencyContext.HELP_KEY);
294 } catch (Exception e){
295 //
296 // deliberately left empty, if no help requested, just continue
297 //
298 }
299
300 return dependencyCtx;
301 }
302
303 /***
304 * Prints the usage information for this class to <code>System.out</code>.
305 */
306 private static void printUsage() {
307 String lSep = System.getProperty("line.separator");
308 StringBuffer msg = new StringBuffer();
309 msg.append(lSep);
310 msg.append("Usage:\tjava -jar dpt.jar [options] " + lSep);
311 msg.append("Options: " + lSep);
312 msg.append(lSep);
313 msg.append(" -root <file>|<path> root of the project to be analyzed" + lSep);
314 msg.append(" may be either a dir containing class files" + lSep);
315 msg.append(" or a jar containig class files" + lSep);
316 msg.append(" -report <file> start dptool in batch-mode and specify" + lSep);
317 msg.append(" the file with report configuration" + lSep);
318 msg.append(" -filter <regexp> specify filter to exclude packages from analysis" + lSep);
319 msg.append(" -perspective <file> specify file containing perspective definitions" + lSep);
320 msg.append(" -nowindows if the used os is not windows" + lSep);
321 msg.append(" -proxy <proxyHost>:<port>,<user>,<password> ");
322 msg.append(lSep + " to layout the graph" + lSep);
323 msg.append(" from behind a firewall over the web" + lSep);
324 msg.append(lSep);
325 msg.append(" -help print this message and exit" + lSep);
326 msg.append(lSep);
327 msg.append("IMPORTANT: ");
328 msg.append("The \"-report <file>\" option requires the \"-root <file>|<path>\" option");
329 msg.append(lSep);
330 System.out.println(msg.toString());
331 }
332 }
This page was automatically generated by Maven