View Javadoc

1   package net.sf.statsvn.util;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.InputStreamReader;
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import net.sf.statcvs.util.LookaheadReader;
10  import net.sf.statsvn.output.SvnConfigurationOptions;
11  
12  /**
13   * Utility class that verifies if the correct version of subversion is used.
14   * 
15   * @author Jean-Philippe Daigle <jpdaigle@softwareengineering.ca>
16   * 
17   * @version $Id: SvnStartupUtils.java 394 2009-08-10 20:08:46Z jkealey $
18   */
19  public class SvnStartupUtils implements ISvnVersionProcessor {
20  	private static final String SVN_VERSION_COMMAND = "svn --version";
21  
22  	public static final String SVN_MINIMUM_VERSION = "1.3.0";
23  
24  	public static final String SVN_MINIMUM_VERSION_DIFF_PER_REV = "1.4.0";
25  
26  	private static final String SVN_VERSION_LINE_PATTERN = ".* [0-9]+\\.[0-9]+\\.[0-9]+.*";
27  
28  	private static final String SVN_VERSION_PATTERN = "[0-9]+\\.[0-9]+\\.[0-9]+";
29  
30  
31      protected ISvnProcessor processor;
32  
33      /**
34       * Invokes various calls needed during StatSVN's startup, including the svn version command line.   
35       */
36      public SvnStartupUtils(ISvnProcessor processor) {
37          this.processor = processor;
38      }
39  
40      protected ISvnProcessor getProcessor() {
41          return processor;
42      }
43  
44  	/* (non-Javadoc)
45       * @see net.sf.statsvn.util.IVersionProcessor#checkSvnVersionSufficient()
46       */
47  	public synchronized String checkSvnVersionSufficient() throws SvnVersionMismatchException {
48  		ProcessUtils pUtils = null;
49  		try {
50  
51  			pUtils = ProcessUtils.call(SVN_VERSION_COMMAND);
52  			final InputStream istream = pUtils.getInputStream();
53  			final LookaheadReader reader = new LookaheadReader(new InputStreamReader(istream));
54  
55  			while (reader.hasNextLine()) {
56  				final String line = reader.nextLine();
57  				if (line.matches(SVN_VERSION_LINE_PATTERN)) {
58  					// We have our version line
59  					final Pattern pRegex = Pattern.compile(SVN_VERSION_PATTERN);
60  					final Matcher m = pRegex.matcher(line);
61  					if (m.find()) {
62  						final String versionString = line.substring(m.start(), m.end());
63  
64  						// we perform a simple string comparison against the version numbers
65  						if (versionString.compareTo(SVN_MINIMUM_VERSION) >= 0) {
66  							return versionString; // success
67  						} else {
68  							throw new SvnVersionMismatchException(versionString, SVN_MINIMUM_VERSION);
69  						}
70  					}
71  				}
72  			}
73  
74  			if (pUtils.hasErrorOccured()) {
75  				throw new IOException(pUtils.getErrorMessage());
76  			}
77  		} catch (final IOException e) {
78  			SvnConfigurationOptions.getTaskLogger().info(e.getMessage());
79  		} catch (final RuntimeException e) {
80  			SvnConfigurationOptions.getTaskLogger().info(e.getMessage());
81  		} finally {
82  			if (pUtils != null) {
83  				try {
84  					pUtils.close();
85  				} catch (final IOException e) {
86  					SvnConfigurationOptions.getTaskLogger().info(e.getMessage());
87  				}
88  			}
89  		}
90  
91  		throw new SvnVersionMismatchException();
92  	}
93  
94  	/* (non-Javadoc)
95       * @see net.sf.statsvn.util.IVersionProcessor#checkDiffPerRevPossible(java.lang.String)
96       */
97  	public synchronized boolean checkDiffPerRevPossible(final String version) {
98  		// we perform a simple string comparison against the version numbers
99  		return version.compareTo(SVN_MINIMUM_VERSION_DIFF_PER_REV) >= 0;
100 	}
101 }