View Javadoc

1   /*
2       StatCvs - CVS statistics generation
3       Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4       http://statcvs.sf.net/
5   
6       This library is free software; you can redistribute it and/or
7       modify it under the terms of the GNU Lesser General Public
8       License as published by the Free Software Foundation; either
9       version 2.1 of the License, or (at your option) any later version.
10  
11      This library is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      Lesser General Public License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public
17      License along with this library; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  	$RCSfile: Main.java,v $
21  	Created on $Date: 2008/04/17 15:02:01 $
22  */
23  package net.sf.statcvs;
24  
25  import java.io.FileReader;
26  import java.io.IOException;
27  import java.io.Reader;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.logging.LogManager;
31  import java.util.logging.Logger;
32  
33  import net.sf.statcvs.input.Builder;
34  import net.sf.statcvs.input.CvsLogfileParser;
35  import net.sf.statcvs.input.LogSyntaxException;
36  import net.sf.statcvs.input.RepositoryFileManager;
37  import net.sf.statcvs.model.Repository;
38  import net.sf.statcvs.output.CommandLineParser;
39  import net.sf.statcvs.output.ConfigurationException;
40  import net.sf.statcvs.output.ConfigurationOptions;
41  import net.sf.statcvs.output.ReportConfig;
42  import net.sf.statcvs.pages.ReportSuiteMaker;
43  
44  /**
45   * StatCvs Main Class; it starts the application and controls command-line
46   * related stuff
47   * @author Lukasz Pekacki
48   * @author Richard Cyganiak
49   * @version $Id: Main.java,v 1.62 2008/04/17 15:02:01 benoitx Exp $
50   */
51  public class Main {
52      private static Logger logger = Logger.getLogger("net.sf.statcvs");
53      private static LogManager lm = LogManager.getLogManager();
54  
55      /**
56       * Main method of StatCvs
57       * @param args command line options
58       */
59      public static void main(final String[] args) {
60          System.out.println(Messages.getString("PROJECT_NAME") + Messages.NL);
61  
62          if (args.length == 0) {
63              printProperUsageAndExit();
64          }
65          if (args.length == 1) {
66              final String arg = args[0].toLowerCase();
67              if (arg.equals("-h") || arg.equals("-help")) {
68                  printProperUsageAndExit();
69              } else if (arg.equals("-version")) {
70                  printVersionAndExit();
71              }
72          }
73  
74          try {
75              new CommandLineParser(args).parse();
76              generateDefaultHTMLSuite();
77          } catch (final ConfigurationException cex) {
78              System.err.println(cex.getMessage());
79              System.exit(1);
80          } catch (final LogSyntaxException lex) {
81              printLogErrorMessageAndExit(lex.getMessage());
82          } catch (final IOException ioex) {
83              printIoErrorMessageAndExit(ioex.getMessage());
84          } catch (final OutOfMemoryError oome) {
85              printOutOfMemMessageAndExit();
86          }
87          System.exit(0);
88      }
89  
90      private static void initLogManager(final String loggingProperties) {
91          try {
92              lm.readConfiguration(Main.class.getResourceAsStream(loggingProperties));
93          } catch (final IOException e) {
94              System.err.println("ERROR: Logging could not be initialized!");
95          }
96      }
97  
98      private static void printProperUsageAndExit() {
99          System.out.println(
100         //max. 80 chars
101                 //         12345678901234567890123456789012345678901234567890123456789012345678901234567890
102                 "Usage: java -jar statcvs.jar [options] <logfile> <directory>\n" + "\n" + "Required parameters:\n"
103                         + "  <logfile>          path to the cvs logfile of the module\n"
104                         + "  <directory>        path to the directory of the checked out module\n" + "\n" + "Some options:\n"
105                         + "  -version           print version information and exit\n" + "  -output-dir <dir>  set directory where HTML suite will be saved\n"
106                         + "  -include <pattern> include only files matching pattern, e.g. **/*.c;**/*.h\n"
107                         + "  -exclude <pattern> exclude matching files, e.g. tests/**;docs/**\n"
108                         + "  -tags <regexp>     show matching tags in lines of code chart, e.g. version-.*\n"
109                         + "  -title <title>     set project title to be used in reports\n" + "  -xdoc              generate Maven XDoc instead of HTML\n"
110                         + "  -trac <url>        integrate with Trac at <url>\n" + "  -xml               generate XML instead of HTML\n"
111                         + "  -verbose           print extra progress information\n" + "  -viewcvs/viewvc/cvsweb/chora/jcvsweb/bugzilla/mantis <url>\n"
112                         + "                     add links to installation at <url>\n" + "\n" + "Full options list: http://statcvs.sf.net/manual");
113         System.exit(1);
114     }
115 
116     private static void printVersionAndExit() {
117         System.out.println("Version " + Messages.getString("PROJECT_VERSION"));
118         System.exit(1);
119     }
120 
121     private static void printOutOfMemMessageAndExit() {
122         System.err.println("OutOfMemoryError.");
123         System.err.println("Try running java with the -mx option (e.g. -mx128m for 128Mb).");
124         System.exit(1);
125     }
126 
127     private static void printLogErrorMessageAndExit(final String message) {
128         System.err.println("Logfile parsing failed.");
129         System.err.println(message);
130         System.exit(1);
131     }
132 
133     private static void printIoErrorMessageAndExit(final String message) {
134         System.err.println(message);
135         System.exit(1);
136     }
137 
138     /**
139      * Generates HTML report. {@link net.sf.statcvs.output.ConfigurationOptions}
140      * must be initialized before calling this method.
141      * @throws LogSyntaxException if the logfile contains unexpected syntax
142      * @throws IOException if some file can't be read or written
143      * @throws ConfigurationException if a required ConfigurationOption was not set
144      */
145     public static void generateDefaultHTMLSuite() throws LogSyntaxException, IOException, ConfigurationException {
146 
147         if (ConfigurationOptions.getLogFileName() == null) {
148             throw new ConfigurationException("Missing logfile name");
149         }
150         if (ConfigurationOptions.getCheckedOutDirectory() == null) {
151             throw new ConfigurationException("Missing checked out directory");
152         }
153 
154         final long memoryUsedOnStart = Runtime.getRuntime().totalMemory();
155         final long startTime = System.currentTimeMillis();
156 
157         initLogManager(ConfigurationOptions.getLoggingProperties());
158 
159         logger.info("Parsing CVS log '" + ConfigurationOptions.getLogFileName() + "'");
160 
161         final Reader logReader = new FileReader(ConfigurationOptions.getLogFileName());
162         final RepositoryFileManager repFileMan = new RepositoryFileManager(ConfigurationOptions.getCheckedOutDirectory());
163         final Builder builder = new Builder(repFileMan, ConfigurationOptions.getIncludePattern(), ConfigurationOptions.getExcludePattern(),
164                 ConfigurationOptions.getSymbolicNamesPattern());
165         new CvsLogfileParser(logReader, builder).parse();
166         if (ConfigurationOptions.getProjectName() == null) {
167             ConfigurationOptions.setProjectName(builder.getProjectName());
168         }
169         if (ConfigurationOptions.getWebRepository() != null) {
170             ConfigurationOptions.getWebRepository().setAtticFileNames(builder.getAtticFileNames());
171         }
172 
173         logger.info("Generating report for " + ConfigurationOptions.getProjectName() + " into " + ConfigurationOptions.getOutputDir());
174         logger.info("Using " + ConfigurationOptions.getCssHandler());
175         final Repository content = builder.createCvsContent();
176         if (content.isEmpty()) {
177             if (builder.allRejectedByExcludePattern()) {
178                 logger.warning("Exclude pattern '" + ConfigurationOptions.getExcludePattern() + "' removed all files from repository");
179             } else if (builder.allRejectedByIncludePattern()) {
180                 logger.warning("Include pattern '" + ConfigurationOptions.getIncludePattern() + "' rejected all files from repository");
181             } else {
182                 logger.warning("Empty repository");
183             }
184         }
185         if (builder.isLocalFilesNotFound()) {
186             logger.warning("The log references many files that do not exist in the local copy.");
187             logger.warning("Reports will be inaccurate or broken.");
188             logger.warning("Log not generated in '" + ConfigurationOptions.getCheckedOutDirectory() + "'?");
189         } else if (!builder.hasLocalCVSMetadata()) {
190             logger.warning("No CVS metadata found in working copy. Reports may be inaccurate.");
191         } else if (builder.isLogAndLocalFilesOutOfSync()) {
192             logger.warning("Log and working copy are out of sync. Reports will be inaccurate.");
193         }
194 
195         // make JFreeChart work on systems without GUI
196         System.setProperty("java.awt.headless", "true");
197 
198         final ReportConfig config = new ReportConfig(content, ConfigurationOptions.getProjectName(), ConfigurationOptions.getOutputDir(), ConfigurationOptions
199                 .getMarkupSyntax(), ConfigurationOptions.getCssHandler());
200         config.setWebRepository(ConfigurationOptions.getWebRepository());
201         config.setWebBugtracker(ConfigurationOptions.getWebBugtracker());
202         config.setNonDeveloperLogins(ConfigurationOptions.getNonDeveloperLogins());
203 
204         final List extraReports = new ArrayList();
205         //        extraReports.add(new RepoMapPageMaker(config).toFile());
206         //        extraReports.add(new ChurnPageMaker(config).toFile());
207 
208         if ("xml".equalsIgnoreCase(ConfigurationOptions.getOutputFormat())) {
209             new ReportSuiteMaker(config, ConfigurationOptions.getNotes(), extraReports).toXml();
210         } else {
211             new ReportSuiteMaker(config, ConfigurationOptions.getNotes(), extraReports).toFile().write();
212         }
213 
214         final long endTime = System.currentTimeMillis();
215         final long memoryUsedOnEnd = Runtime.getRuntime().totalMemory();
216 
217         logger.info("runtime: " + (((double) endTime - startTime) / 1000) + " seconds");
218         logger.info("memory usage: " + (((double) memoryUsedOnEnd - memoryUsedOnStart) / 1024) + " kb");
219     }
220 }