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: 2009/03/09 21:45:42 $
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.64 2009/03/09 21:45:42 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                         + "  -charset <charset> specify the charset to use for html/xdoc\n" + "  -verbose           print extra progress information\n"
112                         + "  -viewcvs/viewvc/cvsweb/chora/jcvsweb/bugzilla/mantis <url>\n" + "                     add links to installation at <url>\n" + "\n"
113                         + "Full options list: http://statcvs.sf.net/manual");
114         System.exit(1);
115     }
116 
117     private static void printVersionAndExit() {
118         System.out.println("Version " + Messages.getString("PROJECT_VERSION"));
119         System.exit(1);
120     }
121 
122     private static void printOutOfMemMessageAndExit() {
123         System.err.println("OutOfMemoryError.");
124         System.err.println("Try running java with the -mx option (e.g. -mx128m for 128Mb).");
125         System.exit(1);
126     }
127 
128     private static void printLogErrorMessageAndExit(final String message) {
129         System.err.println("Logfile parsing failed.");
130         System.err.println(message);
131         System.exit(1);
132     }
133 
134     private static void printIoErrorMessageAndExit(final String message) {
135         System.err.println(message);
136         System.exit(1);
137     }
138 
139     /**
140      * Generates HTML report. {@link net.sf.statcvs.output.ConfigurationOptions}
141      * must be initialized before calling this method.
142      * @throws LogSyntaxException if the logfile contains unexpected syntax
143      * @throws IOException if some file can't be read or written
144      * @throws ConfigurationException if a required ConfigurationOption was not set
145      */
146     public static void generateDefaultHTMLSuite() throws LogSyntaxException, IOException, ConfigurationException {
147 
148         if (ConfigurationOptions.getLogFileName() == null) {
149             throw new ConfigurationException("Missing logfile name");
150         }
151         if (ConfigurationOptions.getCheckedOutDirectory() == null) {
152             throw new ConfigurationException("Missing checked out directory");
153         }
154 
155         final long memoryUsedOnStart = Runtime.getRuntime().totalMemory();
156         final long startTime = System.currentTimeMillis();
157 
158         initLogManager(ConfigurationOptions.getLoggingProperties());
159 
160         logger.info("Parsing CVS log '" + ConfigurationOptions.getLogFileName() + "'");
161 
162         final Reader logReader = new FileReader(ConfigurationOptions.getLogFileName());
163         final RepositoryFileManager repFileMan = new RepositoryFileManager(ConfigurationOptions.getCheckedOutDirectory());
164         final Builder builder = new Builder(repFileMan, ConfigurationOptions.getIncludePattern(), ConfigurationOptions.getExcludePattern(),
165                 ConfigurationOptions.getSymbolicNamesPattern());
166         new CvsLogfileParser(logReader, builder).parse();
167         if (ConfigurationOptions.getProjectName() == null) {
168             ConfigurationOptions.setProjectName(builder.getProjectName());
169         }
170         if (ConfigurationOptions.getWebRepository() != null) {
171             ConfigurationOptions.getWebRepository().setAtticFileNames(builder.getAtticFileNames());
172         }
173 
174         logger.info("Generating report for " + ConfigurationOptions.getProjectName() + " into " + ConfigurationOptions.getOutputDir());
175         logger.info("Using " + ConfigurationOptions.getCssHandler());
176         final Repository content = builder.createCvsContent();
177         if (content.isEmpty()) {
178             if (builder.allRejectedByExcludePattern()) {
179                 logger.warning("Exclude pattern '" + ConfigurationOptions.getExcludePattern() + "' removed all files from repository");
180             } else if (builder.allRejectedByIncludePattern()) {
181                 logger.warning("Include pattern '" + ConfigurationOptions.getIncludePattern() + "' rejected all files from repository");
182             } else {
183                 logger.warning("Empty repository");
184             }
185         }
186         if (builder.isLocalFilesNotFound()) {
187             logger.warning("The log references many files that do not exist in the local copy.");
188             logger.warning("Reports will be inaccurate or broken.");
189             logger.warning("Log not generated in '" + ConfigurationOptions.getCheckedOutDirectory() + "'?");
190         } else if (!builder.hasLocalCVSMetadata()) {
191             logger.warning("No CVS metadata found in working copy. Reports may be inaccurate.");
192         } else if (builder.isLogAndLocalFilesOutOfSync()) {
193             logger.warning("Log and working copy are out of sync. Reports will be inaccurate.");
194         }
195 
196         // make JFreeChart work on systems without GUI
197         System.setProperty("java.awt.headless", "true");
198 
199         final ReportConfig config = new ReportConfig(content, ConfigurationOptions.getProjectName(), ConfigurationOptions.getOutputDir(), ConfigurationOptions
200                 .getMarkupSyntax(), ConfigurationOptions.getCssHandler(), ConfigurationOptions.getCharSet());
201         config.setWebRepository(ConfigurationOptions.getWebRepository());
202         config.setWebBugtracker(ConfigurationOptions.getWebBugtracker());
203         config.setNonDeveloperLogins(ConfigurationOptions.getNonDeveloperLogins());
204 
205         final List extraReports = new ArrayList();
206         //        extraReports.add(new RepoMapPageMaker(config).toFile());
207         //        extraReports.add(new ChurnPageMaker(config).toFile());
208 
209         if ("xml".equalsIgnoreCase(ConfigurationOptions.getOutputFormat())) {
210             new ReportSuiteMaker(config, ConfigurationOptions.getNotes(), extraReports).toXml();
211         } else {
212             new ReportSuiteMaker(config, ConfigurationOptions.getNotes(), extraReports).toFile().write();
213         }
214 
215         final long endTime = System.currentTimeMillis();
216         final long memoryUsedOnEnd = Runtime.getRuntime().totalMemory();
217 
218         logger.info("runtime: " + (((double) endTime - startTime) / 1000) + " seconds");
219         logger.info("memory usage: " + (((double) memoryUsedOnEnd - memoryUsedOnStart) / 1024) + " kb");
220     }
221 }