1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.BufferedReader ; 40 import java.io.File ; 41 import java.io.InputStream ; 42 import java.io.InputStreamReader ; 43 import java.io.OutputStream ; 44 import java.io.PrintWriter ; 45 import java.util.ArrayList ; 46 import java.util.Date ; 47 import java.util.Hashtable ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 import net.sourceforge.cruisecontrol.CruiseControlException; 52 import net.sourceforge.cruisecontrol.Modification; 53 import net.sourceforge.cruisecontrol.SourceControl; 54 import net.sourceforge.cruisecontrol.util.ValidationHelper; 55 import net.sourceforge.cruisecontrol.util.StreamPumper; 56 import net.sourceforge.cruisecontrol.util.Commandline; 57 58 import org.apache.log4j.Logger; 59 60 74 public class MKS implements SourceControl { 75 private static final Logger LOG = Logger.getLogger(MKS.class); 76 77 private Hashtable properties = new Hashtable (); 78 79 private String property; 80 81 private String project; 82 83 private File localWorkingDir; 84 85 90 private boolean doNothing; 91 92 101 private List listOfModifications = new ArrayList (); 102 103 public void setProject(String project) { 104 this.project = project; 105 } 106 107 114 public void setLocalWorkingDir(String local) { 115 localWorkingDir = new File (local); 116 } 117 118 public void setProperty(String property) { 119 this.property = property; 120 } 121 122 public Map getProperties() { 123 return properties; 124 } 125 126 public void setDoNothing(String doNothing) { 127 this.doNothing = new Boolean (doNothing).booleanValue(); 128 } 129 130 public void validate() throws CruiseControlException { 131 ValidationHelper.assertIsSet(localWorkingDir, "localWorkingDir", this.getClass()); 132 ValidationHelper.assertIsSet(project, "project", this.getClass()); 133 } 134 135 146 public List getModifications(Date lastBuild, Date now) { 147 148 int numberOfFilesForDot = 0; 149 boolean printCR = false; 150 151 if (doNothing) { 152 if (property != null) { 153 properties.put(property, "true"); 154 } 155 return listOfModifications; 156 } 157 String cmd; 158 159 String projectFilePath = localWorkingDir.getAbsolutePath() + File.separator + project; 160 if (!new File (projectFilePath).exists()) { 161 throw new RuntimeException ("project file not found at " + projectFilePath); 162 } 163 cmd = new String ("si resync -f -R -S " + projectFilePath); 164 165 172 173 try { 174 LOG.debug(cmd); 175 Process proc = Runtime.getRuntime() 176 .exec(cmd, null, localWorkingDir); 177 logStream(proc.getInputStream(), System.out); 178 InputStream in = proc.getErrorStream(); 179 BufferedReader reader = new BufferedReader ( 180 new InputStreamReader (in)); 181 String line = reader.readLine(); 182 183 while (line != null) { 184 int idxCheckedOutRevision = line 185 .indexOf(": checked out revision"); 186 187 if (idxCheckedOutRevision == -1) { 188 numberOfFilesForDot++; 189 if (numberOfFilesForDot == 20) { 190 System.out.print("."); numberOfFilesForDot = 0; 192 printCR = true; 193 } 194 line = reader.readLine(); 195 continue; 196 } 197 if (printCR) { 198 System.out.println(""); printCR = false; 200 } 201 LOG.info(line); 202 203 int idxSeparator = line.lastIndexOf(File.separator); 204 String folderName = line.substring(0, idxSeparator); 205 String fileName = line.substring(idxSeparator + 1, 206 idxCheckedOutRevision); 207 Modification modification = new Modification(); 208 Modification.ModifiedFile modFile = modification 209 .createModifiedFile(fileName, folderName); 210 modification.modifiedTime = new Date (new File (folderName, 211 fileName).lastModified()); 212 modFile.revision = line.substring(idxCheckedOutRevision + 23); 213 modification.revision = modFile.revision; 214 setUserNameAndComment(modification, folderName, fileName); 215 216 listOfModifications.add(modification); 217 218 line = reader.readLine(); 219 220 if (property != null) { 221 properties.put(property, "true"); 222 } 223 } 224 proc.waitFor(); 225 proc.getInputStream().close(); 226 proc.getOutputStream().close(); 227 proc.getErrorStream().close(); 228 229 } catch (Exception ex) { 230 LOG.warn(ex.getMessage(), ex); 231 } 232 System.out.println(); LOG.info("resync finished"); 234 235 return listOfModifications; 236 } 237 238 246 private void setUserNameAndComment(Modification modification, 247 String folderName, String fileName) { 248 249 try { 250 Commandline commandLine = new Commandline(); 251 commandLine.setExecutable("si"); 252 253 if (localWorkingDir != null) { 254 commandLine.setWorkingDirectory(localWorkingDir.getAbsolutePath()); 255 } 256 257 commandLine.createArgument().setValue("rlog"); 258 commandLine.createArgument().setValue("--format={author};{description}"); 259 commandLine.createArgument().setValue("--noHeaderFormat"); 260 commandLine.createArgument().setValue("--noTrailerFormat"); 261 commandLine.createArgument().setValue("-r"); 262 commandLine.createArgument().setValue(modification.revision); 263 commandLine.createArgument().setValue(folderName + File.separator + fileName); 264 265 LOG.debug(commandLine.toString()); 266 267 Process proc = commandLine.execute(); 268 269 logStream(proc.getErrorStream(), System.err); 270 InputStream in = proc.getInputStream(); 271 BufferedReader reader = new BufferedReader ( 272 new InputStreamReader (in)); 273 String line = reader.readLine(); 274 LOG.debug(line); 275 276 int idx = line.indexOf(";"); 277 while (idx == -1) { 278 line = reader.readLine(); LOG.debug(line); 280 idx = line.indexOf(";"); 281 } 282 283 modification.userName = line.substring(0, idx); 284 modification.comment = line.substring(idx + 1); 285 286 proc.waitFor(); 287 proc.getInputStream().close(); 288 proc.getOutputStream().close(); 289 proc.getErrorStream().close(); 290 } catch (Exception e) { 291 LOG.warn(e.getMessage(), e); 292 modification.userName = ""; 293 modification.comment = ""; 294 } 295 } 296 297 private static void logStream(InputStream inStream, OutputStream outStream) { 298 StreamPumper errorPumper = new StreamPumper(inStream, new PrintWriter ( 299 outStream, true)); 300 new Thread (errorPumper).start(); 301 } 302 } 303 | Popular Tags |