KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > CVSGrab


1 /*
2  * CVSGrab
3  * Author: Ludovic Claude (ludovicc@users.sourceforge.net)
4  * Distributable under BSD license.
5  */

6 package net.sourceforge.cvsgrab;
7
8 import java.io.BufferedWriter JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileReader JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.LineNumberReader JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import net.sourceforge.cvsgrab.util.CVSGrabLog;
23 import net.sourceforge.cvsgrab.util.ThreadPool;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.CommandLineParser;
27 import org.apache.commons.cli.GnuParser;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.OptionBuilder;
31 import org.apache.commons.cli.OptionGroup;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34 import org.apache.commons.httpclient.methods.GetMethod;
35 import org.apache.commons.lang.SystemUtils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.commons.logging.impl.LogFactoryImpl;
39
40 /**
41  * Application class for CVSGrab. <br>
42  * CVSGrab loads and updates the files from the ViewCVS web interface of a CVS
43  * repository
44  *
45  * @author Ludovic Claude
46  * @created April 19, 2002
47  * @version 1.0
48  */

49 public class CVSGrab {
50
51     /**
52      * The dummy root to use if the cvsRoot option is not set
53      */

54     public static final String JavaDoc DUMMY_ROOT = ":pserver:anonymous@dummyhost:/dummyroot";
55
56     public static final String JavaDoc CONNECTIONS_OPTION = "connections";
57     public static final String JavaDoc WEB_PASSWORD_OPTION = "webPassword";
58     public static final String JavaDoc WEB_USER_OPTION = "webUser";
59     public static final String JavaDoc PROXY_PASSWORD_OPTION = "proxyPassword";
60     public static final String JavaDoc PROXY_USER_OPTION = "proxyUser";
61     public static final String JavaDoc PROXY_NTDOMAIN_OPTION = "proxyNTDomain";
62     public static final String JavaDoc PROXY_PORT_OPTION = "proxyPort";
63     public static final String JavaDoc PROXY_HOST_OPTION = "proxyHost";
64     public static final String JavaDoc PRUNE_OPTION = "prune";
65     public static final String JavaDoc QUIET_OPTION = "quiet";
66     public static final String JavaDoc VERBOSE_OPTION = "verbose";
67     public static final String JavaDoc DEBUG_WIRE_OPTION = "debugWire";
68     public static final String JavaDoc DEBUG_OPTION = "debug";
69     public static final String JavaDoc CVS_ROOT_OPTION = "cvsRoot";
70     public static final String JavaDoc WEB_INTERFACE_OPTION = "webInterface";
71     public static final String JavaDoc PROJECT_ROOT_OPTION = "projectRoot";
72     public static final String JavaDoc QUERY_PARAMS_OPTION = "queryParams";
73     public static final String JavaDoc TAG_OPTION = "tag";
74     public static final String JavaDoc PACKAGE_DIR_OPTION = "packageDir";
75     public static final String JavaDoc DEST_DIR_OPTION = "destDir";
76     public static final String JavaDoc PACKAGE_PATH_OPTION = "packagePath";
77     public static final String JavaDoc ROOT_URL_OPTION = "rootUrl";
78     public static final String JavaDoc URL_OPTION = "url";
79     public static final String JavaDoc CLEAN_UPDATE_OPTION = "clean";
80     public static final String JavaDoc LIST_WEB_INTERFACES_CMD = "listWebInterfaces";
81     public static final String JavaDoc DIFF_CMD = "diff";
82     public static final String JavaDoc HELP_CMD = "help";
83
84     private static final String JavaDoc[] WEB_OPTIONS = {ROOT_URL_OPTION, PACKAGE_PATH_OPTION,
85             PROJECT_ROOT_OPTION, TAG_OPTION, QUERY_PARAMS_OPTION, WEB_INTERFACE_OPTION,
86             PROXY_HOST_OPTION, PROXY_PORT_OPTION, PROXY_NTDOMAIN_OPTION, PROXY_USER_OPTION,
87             PROXY_PASSWORD_OPTION, WEB_USER_OPTION, WEB_PASSWORD_OPTION};
88     private static final String JavaDoc FORUM_URL = "http://sourceforge.net/forum/forum.php?forum_id=174128";
89     private static final String JavaDoc VERSION = "2.2.2";
90     private static final String JavaDoc DEFAULT_DEST_DIR = ".";
91     private static Log LOG;
92
93     private boolean _pruneEmptyDirs = false;
94     private boolean _cleanUpdate;
95     private boolean _error = false;
96     private String JavaDoc _packageDir;
97     private String JavaDoc _destDir = DEFAULT_DEST_DIR;
98     private String JavaDoc _cvsRoot = DUMMY_ROOT;
99     private Options _options;
100     private WebOptions _webOptions = new WebOptions();
101     private CvsWebInterface _webInterface;
102
103     public static Log getLog() {
104         if (LOG == null) {
105             LOG = LogFactory.getLog(CVSGrab.class);
106         }
107         return LOG;
108     }
109
110     public static void setLog(Log log) {
111         LOG = log;
112     }
113
114     /**
115      * Constructor for the CVSGrab object
116      */

117     public CVSGrab() {
118         _options = new Options();
119         _options.addOption(HELP_CMD, false, "[Command] Prints this help message");
120         _options.addOption(LIST_WEB_INTERFACES_CMD, false, "[Command] Lists the web interfaces to the CVS repository that are"
121                 + "\t supported by this tool");
122         _options.addOption(new OptionBuilder()
123                 .withDescription("[Command] Builds the differences against the same remote version. Result is stored in the file patch.txt")
124                 .create(DIFF_CMD));
125         _options.addOption(new OptionBuilder().withArgName("url")
126                 .hasArg()
127                 .withDescription("The full url used to access the CVS repository from a web browser")
128                 .create(URL_OPTION));
129         _options.addOption(new OptionBuilder().withArgName("url")
130                 .hasArg()
131                 .withDescription("[if full url not used] The root url used to access the CVS repository from a web browser")
132                 .create(ROOT_URL_OPTION));
133         _options.addOption(new OptionBuilder().withArgName("path")
134                 .hasArg()
135                 .withDescription("[if full url not used] The path relative to rootUrl of the package or module to download")
136                 .create(PACKAGE_PATH_OPTION));
137         _options.addOption(new OptionBuilder().withArgName("root")
138                 .hasArg()
139                 .withDescription("[optional] The project root, for cvs with multiple repositories")
140                 .create(PROJECT_ROOT_OPTION));
141         _options.addOption(new OptionBuilder().withArgName("version tag")
142                 .hasArg()
143                 .withDescription("[optional] The version tag of the files to download")
144                 .create(TAG_OPTION));
145         _options.addOption(new OptionBuilder().withArgName("query params")
146                 .hasArg()
147                 .withDescription("[optional] Additional query parameters")
148                 .create(QUERY_PARAMS_OPTION));
149         _options.addOption(new OptionBuilder().withArgName("web interface id")
150                 .hasArg()
151                 .withDescription("[optional] The id for the web interface for the CVS repository to use. " +
152                         "If this option is not set, autodetect the web interface." +
153                 "Call cvsgrab -listWebInterfaces to get a list of valid values for this option.")
154                 .create(WEB_INTERFACE_OPTION));
155         _options.addOption(new OptionBuilder().withArgName("dir")
156                 .hasArg()
157                 .withDescription("[optional] The destination directory.")
158                 .create(DEST_DIR_OPTION));
159         _options.addOption(new OptionBuilder().withArgName("dir")
160                 .hasArg()
161                 .withDescription("The name of the package to use locally, relative to destDir, overrides packagePath")
162                 .create(PACKAGE_DIR_OPTION));
163         _options.addOption(new OptionBuilder().withArgName("cvs root")
164                 .hasArg()
165                 .withDescription("[optional] The original cvs root, used to maintain compatibility with a standard CVS client")
166                 .create(CVS_ROOT_OPTION));
167         _options.addOption(new OptionBuilder()
168                 .withDescription("[optional] Prune (remove) the empty directories.")
169                 .create(PRUNE_OPTION));
170         _options.addOption(new OptionBuilder()
171                 .withDescription("[optional] Clean update. Backup locally modified files and download anyway the latest version of the file.")
172                 .create(CLEAN_UPDATE_OPTION));
173         Option debugOption = new OptionBuilder()
174                 .withDescription("[optional] Turn debugging on.")
175                 .create(DEBUG_OPTION);
176         Option debugWireOption = new OptionBuilder()
177                 .withDescription("[optional] Turn debugging on, including very verbose network traffic.")
178                 .create(DEBUG_WIRE_OPTION);
179         Option verboseOption = new OptionBuilder()
180                 .withDescription("[optional] Turn verbosity on.")
181                 .create(VERBOSE_OPTION);
182         Option quietOption = new OptionBuilder()
183                 .withDescription("[optional] Be extra quiet.")
184                 .create(QUIET_OPTION);
185         _options.addOptionGroup(new OptionGroup().addOption(debugOption)
186                 .addOption(debugWireOption)
187                 .addOption(verboseOption)
188                 .addOption(quietOption));
189         _options.addOption(new OptionBuilder().withArgName("nb of connections")
190                 .hasArg()
191                 .withDescription("[optional] The number of simultaneous connections to use for downloads, default 1")
192                 .create(CONNECTIONS_OPTION));
193         _options.addOption(new OptionBuilder().withArgName("host")
194                 .hasArg()
195                 .withDescription("[optional] Proxy host")
196                 .create(PROXY_HOST_OPTION));
197         _options.addOption(new OptionBuilder().withArgName("port")
198                 .hasArg()
199                 .withDescription("[optional] Proxy port")
200                 .create(PROXY_PORT_OPTION));
201         _options.addOption(new OptionBuilder().withArgName("domain")
202                 .hasArg()
203                 .withDescription("[optional] NT Domain for the authentification on a MS proxy")
204                 .create(PROXY_NTDOMAIN_OPTION));
205         _options.addOption(new OptionBuilder().withArgName("user")
206                 .hasArg()
207                 .withDescription("[optional] Username for the proxy")
208                 .create(PROXY_USER_OPTION));
209         _options.addOption(new OptionBuilder().withArgName("password")
210                 .hasArg()
211                 .withDescription("[optional] Password for the proxy. If this option is omitted, then cvsgrab will prompt securely for the password.")
212                 .create(PROXY_PASSWORD_OPTION));
213         _options.addOption(new OptionBuilder().withArgName("user")
214                 .hasArg()
215                 .withDescription("[optional] Username for the web server")
216                 .create(WEB_USER_OPTION));
217         _options.addOption(new OptionBuilder().withArgName("password")
218                 .hasArg()
219                 .withDescription("[optional] Password for the web server. If this option is omitted, then cvsgrab will prompt securely for the password.")
220                 .create(WEB_PASSWORD_OPTION));
221     }
222
223     /**
224      * The main program for the CVSGrab class
225      *
226      * @param args The command line arguments
227      */

228     public static void main(String JavaDoc[] args) {
229         System.setProperty(LogFactoryImpl.LOG_PROPERTY, CVSGrabLog.class.getName());
230         CVSGrab grabber = new CVSGrab();
231         try {
232             grabber.run(args);
233         } catch (ParseException e) {
234             System.err.println(e.getMessage());
235             grabber.printHelp();
236         }
237     }
238
239     private void run(String JavaDoc[] args) throws ParseException {
240         CommandLineParser parser = new GnuParser();
241         CommandLine cmd = parser.parse( _options, args);
242
243         // Help and list supported web interfaces
244
if (cmd.hasOption(HELP_CMD)) {
245             printHelp();
246             return;
247         }
248         if (cmd.hasOption(LIST_WEB_INTERFACES_CMD)) {
249             printWebInterfaces();
250             return;
251         }
252
253         // Handle logging levels
254
cvsgrabLogLevel("info", "WARNING");
255         httpclientLogLevel("error", "SEVERE");
256
257         if (cmd.hasOption(DEBUG_OPTION)) {
258             cvsgrabLogLevel("trace", "FINEST");
259             httpclientLogLevel("info", "INFO");
260         }
261         if (cmd.hasOption(DEBUG_WIRE_OPTION)) {
262             httpclientLogLevel("trace", "FINEST");
263             logLevel("httpclient.wire", "trace", "FINE");
264         }
265         if (cmd.hasOption(VERBOSE_OPTION)) {
266             cvsgrabLogLevel("debug", "INFO");
267             httpclientLogLevel("error", "SEVERE");
268         }
269         if (cmd.hasOption(QUIET_OPTION)) {
270             cvsgrabLogLevel("warn", "INFO");
271             httpclientLogLevel("error", "SEVERE");
272         }
273
274         // Handle remote repository options
275
Properties JavaDoc webProperties = new Properties JavaDoc();
276         for (int i = 0; i < WEB_OPTIONS.length; i++) {
277             String JavaDoc option = WEB_OPTIONS[i];
278             if (cmd.hasOption(option)) {
279                 webProperties.put(option, cmd.getOptionValue(option));
280             }
281         }
282         _webOptions.readProperties(webProperties);
283
284         // Handle local repository options
285

286         // By default, projectRoot and cvsRoot should match (they are configured in different parameters,
287
// because projectRoot is the root seen from the web repository, and cvsRoot is the root seen from
288
// the cvs client, and some sites can have different names for them, but so far that's only speculation
289
// and no real case). cvsRoot exists also because of historical reasons, it was introduced for
290
// compatibility with cvs clients.
291
if (cmd.hasOption(PROJECT_ROOT_OPTION)) {
292             setCvsRoot(cmd.getOptionValue(PROJECT_ROOT_OPTION));
293         }
294         if (cmd.hasOption(CVS_ROOT_OPTION)) {
295             setCvsRoot(cmd.getOptionValue(CVS_ROOT_OPTION));
296         }
297         if (cmd.hasOption(DEST_DIR_OPTION)) {
298             setDestDir(cmd.getOptionValue(DEST_DIR_OPTION));
299         }
300         if (cmd.hasOption(PACKAGE_DIR_OPTION)) {
301             setPackageDir(cmd.getOptionValue(PACKAGE_DIR_OPTION));
302         }
303         if (cmd.hasOption(PRUNE_OPTION)) {
304             setPruneEmptyDirs(true);
305         }
306         if (cmd.hasOption(CLEAN_UPDATE_OPTION)) {
307             setCleanUpdate(true);
308         }
309
310         // Handle connection settings
311
_webOptions.setupConnectionSettings();
312         if (cmd.hasOption(CONNECTIONS_OPTION)) {
313             int connections = Integer.parseInt(cmd.getOptionValue(CONNECTIONS_OPTION));
314             if (connections > 1) {
315                 ThreadPool.init(connections);
316                 WebBrowser.getInstance().useMultithreading();
317             }
318         }
319
320         // Handle file types
321
String JavaDoc cvsGrabHome = System.getProperty("cvsgrab.home");
322         File JavaDoc file = new File JavaDoc(cvsGrabHome, "FileTypes.properties");
323         try {
324             Properties JavaDoc fileTypes = new Properties JavaDoc();
325             InputStream JavaDoc is = new FileInputStream JavaDoc(file);
326             fileTypes.load(is);
327             RemoteFile.setFileTypes(fileTypes);
328         } catch (IOException JavaDoc ex) {
329             getLog().error("Cannot read the file " + file.getPath());
330         }
331
332         // Handle autodetection of the full url
333
if (cmd.hasOption(URL_OPTION)) {
334             setUrl(cmd.getOptionValue(URL_OPTION));
335         }
336
337         if (cmd.hasOption(DIFF_CMD)) {
338             diffCVSRepository();
339         } else {
340             grabCVSRepository();
341         }
342     }
343
344     private static void cvsgrabLogLevel(String JavaDoc simpleLevel, String JavaDoc jdk14Level) {
345         logLevel("net.sourceforge.cvsgrab", simpleLevel, jdk14Level);
346     }
347
348     private static void httpclientLogLevel(String JavaDoc simpleLevel, String JavaDoc jdk14Level) {
349         logLevel("org.apache.commons.httpclient", simpleLevel, jdk14Level);
350     }
351
352     private static void logLevel(String JavaDoc packageName, String JavaDoc simpleLevel, String JavaDoc jdk14Level) {
353         if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
354             setJdk14LogLevel(packageName + ".level", jdk14Level);
355         }
356         System.setProperty("org.apache.commons.logging.simplelog.log." + packageName, simpleLevel);
357     }
358
359     /**
360      * @param properties
361      */

362     private static void setJdk14LogLevel(String JavaDoc className, String JavaDoc level) {
363         try {
364             Class JavaDoc loggerClass = Class.forName("java.util.logging.Logger");
365             Class JavaDoc levelClass = Class.forName("java.util.logging.Level");
366             Object JavaDoc logger = loggerClass.getMethod("getLogger", new Class JavaDoc[] {String JavaDoc.class}).invoke(null, new Object JavaDoc[] {className});
367             Object JavaDoc levelObj = levelClass.getField(level).get(null);
368             loggerClass.getMethod("setLevel", new Class JavaDoc[] {levelClass}).invoke(logger, new Object JavaDoc[] {levelObj});
369         } catch (Exception JavaDoc ex) {
370             System.err.println("Cannot change the configuration of the Java 1.4 logger");
371             ex.printStackTrace();
372         }
373     }
374
375     /**
376      * Prints help for the command line program
377      */

378     public void printHelp() {
379         // automatically generate the help statement
380
HelpFormatter formatter = new HelpFormatter();
381         formatter.printHelp(HelpFormatter.DEFAULT_WIDTH, "cvsgrab", "where options are",
382                 _options, "CVSGrab version " + VERSION +", copyright (c) 2002-2004 - Ludovic Claude.", false);
383     }
384
385     /**
386      * Prints the list of available web interfaces
387      */

388     public static void printWebInterfaces() {
389         System.out.println("CVSGrab version " + VERSION);
390         System.out.println("Currently supporting the following web interfaces:");
391         String JavaDoc[] webInterfaces = CvsWebInterface.getInterfaceIds(new CVSGrab());
392         for (int i = 0; i < webInterfaces.length; i++) {
393             System.out.println("\t" + webInterfaces[i]);
394         }
395         System.out.println("Those ids can be use with the -webInterface option to force cvsgrab to use a specific web interface.");
396     }
397
398     /**
399      * Gets the prune empty dirs
400      *
401      * @return The pruneEmptyDirs value
402      */

403     public boolean getPruneEmptyDirs() {
404         return _pruneEmptyDirs;
405     }
406
407     /**
408      * Sets the prune empty dirs
409      *
410      * @param value The new pruneEmptyDirs value
411      */

412     public void setPruneEmptyDirs(boolean value) {
413         _pruneEmptyDirs = value;
414     }
415
416     /**
417      * Gets the cleanUpdate.
418      *
419      * @return the cleanUpdate.
420      */

421     public boolean isCleanUpdate() {
422         return _cleanUpdate;
423     }
424
425     /**
426      * Sets the cleanUpdate.
427      *
428      * @param cleanUpdate The cleanUpdate to set.
429      */

430     public void setCleanUpdate(boolean cleanUpdate) {
431         _cleanUpdate = cleanUpdate;
432     }
433
434     /**
435      * @return Returns the rootUrl.
436      */

437     public String JavaDoc getRootUrl() {
438         return _webOptions.getRootUrl();
439     }
440
441     /**
442      * @return Returns the packagePath.
443      */

444     public String JavaDoc getPackagePath() {
445         return _webOptions.getPackagePath();
446     }
447
448     /**
449      * @return Returns the destDir.
450      */

451     public String JavaDoc getDestDir() {
452         return _destDir;
453     }
454
455     /**
456      * Sets the destination directory for the files to be retrieved from the repository
457      * @param destDir The destDir to set.
458      */

459     public void setDestDir(String JavaDoc destDir) {
460         _destDir = WebBrowser.forceFinalSlash(destDir);
461     }
462
463     /**
464      * @return Returns the packageDir.
465      */

466     public String JavaDoc getPackageDir() {
467         if (_packageDir == null) {
468             return getPackagePath();
469         }
470         return _packageDir;
471     }
472
473     /**
474      * @param packageDir The packageDir to set.
475      */

476     public void setPackageDir(String JavaDoc packageDir) {
477         _packageDir = WebBrowser.forceFinalSlash(packageDir);
478     }
479
480     /**
481      * @return Returns the cvsRoot.
482      */

483     public String JavaDoc getCvsRoot() {
484         return _cvsRoot;
485     }
486
487     /**
488      * Sets the cvs root. This is used by CVSGrab only to rebuild the
489      * CVS admin files that may be used later by a standard CVS client.
490      * @param cvsRoot The cvsRoot to set.
491      */

492     public void setCvsRoot(String JavaDoc cvsRoot) {
493         _cvsRoot = WebBrowser.forceFinalSlash(cvsRoot);
494     }
495
496     /**
497      * @return The project root, used by CVS with multiple repositories
498      */

499     public String JavaDoc getProjectRoot() {
500         return _webOptions.getProjectRoot();
501     }
502
503     /**
504      * @return Returns the versionTag.
505      */

506     public String JavaDoc getVersionTag() {
507         return _webOptions.getVersionTag();
508     }
509
510     /**
511      * @return Returns the queryParams.
512      */

513     public String JavaDoc getQueryParams() {
514         return _webOptions.getQueryParams();
515     }
516
517     /**
518      * Gets the webInterfaceId.
519      * @return the webInterfaceId.
520      */

521     public String JavaDoc getWebInterfaceId() {
522         return _webOptions.getWebInterfaceId();
523     }
524
525     /**
526      * @return the web options
527      */

528     public WebOptions getWebOptions() {
529         return _webOptions;
530     }
531
532     /**
533      * Analyse the root url and try to extract the package path, version tag and web options parameters from it
534      */

535     public void setUrl(String JavaDoc url) {
536         Properties JavaDoc webProperties;
537         if (getWebInterfaceId() != null) {
538             try {
539                 webProperties = getWebInterface().guessWebProperties(url);
540             } catch (Exception JavaDoc e) {
541                 getLog().error(e.getMessage());
542                 _error = true;
543                 return;
544             }
545         } else {
546             webProperties = CvsWebInterface.getWebProperties(this, url);
547             _webInterface = (CvsWebInterface) webProperties.get(CvsWebInterface.DETECTED_WEB_INTERFACE);
548         }
549         // put back the result in the WebOptions
550
_webOptions.readProperties(webProperties);
551     }
552
553     /**
554      * Main method for getting and updating files.
555      */

556     public void grabCVSRepository() {
557         printHeader();
558         loadExistingAdminFiles();
559
560         if (!checkMandatoryParameters()) return;
561
562         try {
563             checkDestDir();
564             checkWebConnection();
565
566             CvsWebInterface webInterface = getWebInterface();
567
568             LocalRepository localRepository = new LocalRepository(this);
569             RemoteRepository remoteRepository = new RemoteRepository(getRootUrl(), localRepository);
570             remoteRepository.setWebInterface(webInterface);
571
572             RemoteDirectory remoteDir = new RemoteDirectory(remoteRepository, getPackagePath(), getPackageDir());
573             remoteRepository.registerDirectoryToProcess(remoteDir);
574             while (remoteRepository.hasDirectoryToProcess()) {
575                 try {
576                     remoteDir = remoteRepository.nextDirectoryToProcess();
577                     remoteDir.loadContents();
578                     localRepository.cleanRemovedFiles(remoteDir);
579                 } catch (Exception JavaDoc ex) {
580                     ex.printStackTrace();
581                     getLog().error("Error while getting files from " + remoteDir.getUrl());
582                     _error = true;
583                 }
584             }
585             if (_pruneEmptyDirs) {
586                 localRepository.pruneEmptyDirectories();
587             }
588
589             // Print a summary
590
int newFileCount = localRepository.getNewFileCount();
591             int updatedFileCount = localRepository.getUpdatedFileCount();
592             int removedFileCount = localRepository.getRemovedFileCount();
593             int failedUpdateCount = localRepository.getFailedUpdateCount();
594             getLog().info("-----");
595             if (newFileCount > 0) {
596                 getLog().info(newFileCount + " new files");
597             }
598             if (updatedFileCount > 0) {
599                 getLog().info(updatedFileCount + " updated files");
600             }
601             if (removedFileCount > 0) {
602                 getLog().info(removedFileCount + " removed files");
603             }
604             if (failedUpdateCount > 0) {
605                 getLog().error(failedUpdateCount + " files could not be downloaded");
606             }
607         } catch (Exception JavaDoc ex) {
608             getLog().error(ex.getMessage());
609             _error = true;
610         }
611
612         if (ThreadPool.getInstance() != null) {
613             ThreadPool.getInstance().destroy();
614         }
615
616         if (_error) {
617             getLog().error("There were some errors.");
618             getLog().error("If you cannot find an obvious answer, report the problem to " + FORUM_URL);
619         }
620     }
621
622     /**
623      * Builds the differences against the remote repository
624      */

625     public void diffCVSRepository() {
626         printHeader();
627         loadExistingAdminFiles();
628
629         if (!checkMandatoryParameters()) return;
630
631         try {
632             checkDestDir();
633             checkWebConnection();
634
635             CvsWebInterface webInterface = getWebInterface();
636             File JavaDoc diffFile = new File JavaDoc("patch.txt");
637             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(diffFile)));
638
639             LocalRepository localRepository = new LocalRepository(this);
640             RemoteRepository remoteRepository = new RemoteRepository(getRootUrl(), localRepository);
641             remoteRepository.setWebInterface(webInterface);
642
643             RemoteDirectory remoteDir = new RemoteDirectory(remoteRepository, getPackagePath(), getPackageDir());
644             remoteRepository.registerDirectoryToProcess(remoteDir);
645             while (remoteRepository.hasDirectoryToProcess()) {
646                 try {
647                     remoteDir = remoteRepository.nextDirectoryToProcess();
648                     remoteDir.diffContents(writer);
649                     writer.flush();
650                 } catch (Exception JavaDoc ex) {
651                     ex.printStackTrace();
652                     getLog().error("Error while getting files from " + remoteDir.getUrl());
653                     _error = true;
654                 }
655             }
656             writer.close();
657
658             // Print a summary
659
int newFileCount = localRepository.getNewFileCount();
660             int updatedFileCount = localRepository.getUpdatedFileCount();
661             int removedFileCount = localRepository.getRemovedFileCount();
662             int failedUpdateCount = localRepository.getFailedUpdateCount();
663             getLog().info("-----");
664             if (newFileCount > 0) {
665                 getLog().info(newFileCount + " new files");
666             }
667             if (updatedFileCount > 0) {
668                 getLog().info(updatedFileCount + " updated files");
669             }
670             if (removedFileCount > 0) {
671                 getLog().info(removedFileCount + " removed files");
672             }
673             if (failedUpdateCount > 0) {
674                 getLog().error(failedUpdateCount + " files could not be downloaded");
675             }
676             getLog().info("Differences stored in " + diffFile.getAbsolutePath());
677         } catch (Exception JavaDoc ex) {
678             ex.printStackTrace();
679             getLog().error(ex.getMessage());
680             _error = true;
681         }
682
683         if (ThreadPool.getInstance() != null) {
684             ThreadPool.getInstance().destroy();
685         }
686
687         if (_error) {
688             getLog().error("There were some errors.");
689             getLog().error("If you cannot find an obvious answer, report the problem to " + FORUM_URL);
690         }
691     }
692
693     private CvsWebInterface getWebInterface() throws Exception JavaDoc {
694         if (_webInterface == null) {
695             if (getWebInterfaceId() != null) {
696                 // Forces the use of a particular web interface and version of that interface
697
_webInterface = CvsWebInterface.getInterface(this, getWebInterfaceId());
698             } else {
699                 // Auto detection of the type of the remote interface
700
_webInterface = detectWebInterface();
701             }
702         }
703         if (_webInterface == null) {
704             getLog().error("Could not detect the type of the web interface");
705             throw new RuntimeException JavaDoc("Could not detect the type of the web interface");
706         } else {
707             getLog().info("Detected cvs web interface: " + _webInterface.getType());
708         }
709
710         _webInterface.setQueryParams(getQueryParams());
711         if (getVersionTag() != null) {
712             _webInterface.setVersionTag(getVersionTag());
713         }
714         return _webInterface;
715     }
716
717     private void checkWebConnection() throws Exception JavaDoc {
718         // Tests the connection to the website
719
String JavaDoc[] urls = CvsWebInterface.getBaseUrls(this);
720         Map JavaDoc errors = new HashMap JavaDoc();
721         for (int i = 0; i < urls.length; i++) {
722             try {
723                 getLog().debug("Connecting to " + urls[i]);
724                 GetMethod connectMethod = new GetMethod(urls[i]);
725                 WebBrowser.getInstance().executeMethod(connectMethod, urls[i]);
726                 getLog().debug("Connection successful");
727                 return;
728             } catch (Exception JavaDoc ex) {
729                 errors.put(urls[i], ex.getMessage());
730             }
731         }
732         for (Iterator JavaDoc i = errors.keySet().iterator(); i.hasNext();) {
733             String JavaDoc url = (String JavaDoc) i.next();
734             System.err.println("When attempting to connect to " + url + ", got error: " + errors.get(url));
735         }
736         throw new Exception JavaDoc("Cannot connect to the website, check your proxy settings");
737     }
738
739     private void checkDestDir() {
740         File JavaDoc dd = new File JavaDoc(_destDir);
741         if (!dd.exists()) {
742             throw new RuntimeException JavaDoc("Destination directory " + _destDir + " doesn't exist");
743         }
744         if (!dd.isDirectory()) {
745             throw new RuntimeException JavaDoc("Destination " + _destDir + " is not a directory");
746         }
747         try {
748             _destDir = dd.getCanonicalPath().replace(File.separatorChar, '/');
749         } catch (IOException JavaDoc ex) {
750             throw new IllegalArgumentException JavaDoc("Could not locate the destination directory " + _destDir + ", error was " + ex.getMessage());
751         }
752     }
753
754     private boolean checkMandatoryParameters() {
755         if (getRootUrl() == null || getPackagePath() == null) {
756             if (getRootUrl() == null) {
757                 System.out.println("Error: rootUrl parameter is mandatory");
758             }
759             if (getPackagePath() == null) {
760                 System.out.println("Error: packagePath parameter is mandatory");
761             }
762             printHelp();
763             return false;
764         }
765         return true;
766     }
767
768     private void loadExistingAdminFiles() {
769         // Try to load the web options from the CVS admin files
770
File JavaDoc webRepositoryAdmin = new File JavaDoc(_destDir, "CVS/WebRepository");
771         if (webRepositoryAdmin.exists()) {
772             Properties JavaDoc webProperties = new Properties JavaDoc();
773             try {
774                 webProperties.load(new FileInputStream JavaDoc(webRepositoryAdmin));
775                 // Now merge with the current properties
776
_webOptions.writeProperties(webProperties);
777                 // And put back the result in the WebOptions
778
_webOptions.readProperties(webProperties);
779                 // Update local settings
780
if (getDestDir().equals(DEFAULT_DEST_DIR)) {
781                     File JavaDoc currentDir = new File JavaDoc(".");
782                     currentDir = currentDir.getAbsoluteFile();
783                     setPackageDir(currentDir.getName());
784                 }
785             } catch (IOException JavaDoc e) {
786                 getLog().warn("Cannot read file " + webRepositoryAdmin.getAbsolutePath(), e);
787             }
788         }
789         // Read the cvsRoot from CVS admin files
790
File JavaDoc rootAdmin = new File JavaDoc(_destDir, "CVS/Root");
791         if (rootAdmin.exists() && _cvsRoot.equals(DUMMY_ROOT)) {
792             try {
793                 FileReader JavaDoc reader = new FileReader JavaDoc(rootAdmin);
794                 LineNumberReader JavaDoc lnReader = new LineNumberReader JavaDoc(reader);
795                 _cvsRoot = lnReader.readLine();
796             } catch (IOException JavaDoc e) {
797                 getLog().warn("Cannot read file " + rootAdmin.getAbsolutePath(), e);
798             }
799         }
800     }
801
802     private void printHeader() {
803         if (getLog().isInfoEnabled()) {
804             getLog().info("CVSGrab version " + VERSION + " starting...");
805         } else {
806             System.out.println("CVSGrab version " + VERSION + " starting...");
807         }
808     }
809
810     private CvsWebInterface detectWebInterface() {
811         CvsWebInterface webInterface = null;
812         try {
813             webInterface = CvsWebInterface.findInterface(this);
814         } catch (Exception JavaDoc ex) {
815             ex.printStackTrace();
816         }
817         return webInterface;
818     }
819
820 }
821
Popular Tags