1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import net.sourceforge.cruisecontrol.Modification; 41 import net.sourceforge.cruisecontrol.SourceControl; 42 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommand; 43 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommandline; 44 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser; 45 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.DateTimespec; 46 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.Runner; 47 import org.apache.log4j.Logger; 48 49 import java.io.BufferedReader ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.InputStreamReader ; 53 import java.util.ArrayList ; 54 import java.util.Date ; 55 import java.util.Hashtable ; 56 import java.util.List ; 57 import java.util.Map ; 58 import java.util.StringTokenizer ; 59 60 67 public class Accurev implements SourceControl, AccurevInputParser { 68 private static final Logger LOG = Logger.getLogger(Accurev.class); 69 private String stream; 70 private boolean verbose; 71 private Hashtable properties = new Hashtable (); 72 private String propertyOnDelete; 73 private ArrayList modifications; 74 private Runner runner; 75 76 81 public void setStream(String stream) { 82 this.stream = stream; 83 } 84 85 90 public void setVerbose(boolean verbose) { 91 this.verbose = verbose; 92 } 93 94 100 public void setProperty(String propertyName) { 101 if (propertyName != null) { 102 properties.put(propertyName, "true"); 103 } 104 } 105 106 111 public void setPropertyOnDelete(String propertyName) { 112 propertyOnDelete = propertyName; 113 addPropertyOnDelete(); 114 } 115 116 public Map getProperties() { 117 return properties; 118 } 119 120 public void validate() throws CruiseControlException { 121 if (stream == null) { 122 throw new CruiseControlException("'stream' is a required attribute for Accurev"); 123 } 124 } 125 126 133 public List getModifications(Date lastBuild, Date now) { 134 LOG.info("Accurev: getting modifications for " + stream); 135 AccurevCommandline hist = AccurevCommand.HIST.create(); 136 if (runner != null) { 137 hist.setRunner(runner); 138 } 139 hist.setVerbose(verbose); 140 hist.setInputParser(this); 141 hist.setStream(stream); 142 hist.setTransactionRange(new DateTimespec(lastBuild), new DateTimespec(now)); 143 hist.run(); 144 return modifications; 145 } 146 147 161 public boolean parseStream(InputStream input) throws IOException , CruiseControlException { 162 modifications = new ArrayList (); 163 Modification modification = null; 164 BufferedReader reader = new BufferedReader (new InputStreamReader (input)); 165 while (true) { 166 String line = reader.readLine(); 167 if (line == null) { 168 break; 169 } 170 LOG.debug(line); 171 if (line.startsWith("transaction")) { 172 modification = new Modification(); 174 String [] parts = getParts(line); 175 modification.comment = ""; 176 modification.revision = parts[0].substring(parts[0].indexOf(' ') + 1); 177 modification.type = parts[1].trim(); 178 modification.modifiedTime = DateTimespec.parse(parts[2].trim()); 179 modification.userName = parts[3].substring(6).trim(); 180 modifications.add(modification); 181 } else if (line.startsWith(" #")) { 182 if (modification != null) { 184 modification.comment += line.substring(3) + "\n"; 185 } else { 186 LOG.warn("Comment outside modification - skipping"); 187 } 188 } else if (line.startsWith(" \\.\\") || line.startsWith(" /./")) { 190 final char separator = line.charAt(2); 192 final int lastSlash = line.lastIndexOf(separator); 193 int lastSpace = line.lastIndexOf(' '); 194 lastSpace = line.lastIndexOf(' ', lastSpace - 1); 195 if (lastSpace > lastSlash) { 196 String fileName = line.substring(lastSlash + 1, lastSpace); 197 String folderName = ((lastSlash > 5) ? line.substring(5, lastSlash) : line.substring(5)).replace( 198 separator, '/'); 199 Modification.ModifiedFile modfile = modification.createModifiedFile(fileName, folderName); 200 modfile.action = "change"; 201 } 202 } 203 } 204 return true; 205 } 206 207 private String [] getParts(String line) { 208 List partsList = new ArrayList (); 209 StringTokenizer tokenizer = new StringTokenizer (line, ";"); 210 while (tokenizer.hasMoreTokens()) { 211 partsList.add(tokenizer.nextToken()); 212 } 213 String [] parts = new String [partsList.size()]; 214 partsList.toArray(parts); 215 return parts; 216 } 217 218 private void addPropertyOnDelete() { 219 if (propertyOnDelete != null) { 220 properties.put(propertyOnDelete, "true"); 221 LOG.debug("setting property " + propertyOnDelete + " to be true"); 222 } 223 } 224 225 public void setRunner(Runner runner) { 226 this.runner = runner; 227 } 228 } | Popular Tags |