1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import net.sourceforge.cruisecontrol.SourceControl; 40 import net.sourceforge.cruisecontrol.CruiseControlException; 41 import net.sourceforge.cruisecontrol.Modification; 42 import net.sourceforge.cruisecontrol.util.ValidationHelper; 43 import net.sourceforge.cruisecontrol.util.ManagedCommandline; 44 45 import org.apache.log4j.Logger; 46 47 import java.util.Hashtable ; 48 import java.util.Date ; 49 import java.util.List ; 50 import java.util.ArrayList ; 51 import java.util.Map ; 52 import java.util.StringTokenizer ; 53 import java.util.Iterator ; 54 import java.io.IOException ; 55 56 66 public class AlienBrain extends AlienBrainCore implements SourceControl { 67 68 private static final Logger LOG = Logger.getLogger(AlienBrain.class); 69 74 private static final long FILETIME_EPOCH_DIFF = 11644473600000L; 75 76 private static final long HUNDRED_NANO_PER_MILLI_RATIO = 10000L; 77 private static final String AB_NO_MODIFICATIONS = "No files or folders found!"; 78 private static final String AB_MODIFICATION_SUMMARY_PREFIX = "Total of "; 79 80 private Hashtable properties = new Hashtable (); 81 82 86 public Map getProperties() { 87 return properties; 88 } 89 90 public void validate() throws CruiseControlException { 91 ValidationHelper.assertIsSet(getPath(), "path", this.getClass()); 92 } 93 94 102 public List getModifications(Date lastBuild, Date now) { 103 List mods = new ArrayList (); 104 try { 105 validate(); 106 mods = getModificationsFromAlienBrain(lastBuild, now); 107 } catch (Exception e) { 108 LOG.error("Log command failed to execute succesfully", e); 109 } 110 111 return mods; 112 } 113 114 120 public static long dateToFiletime(Date date) { 121 long milliSecsSinceUnixEpoch = date.getTime(); 122 long milliSecsSinceFiletimeEpoch = milliSecsSinceUnixEpoch + FILETIME_EPOCH_DIFF; 123 return milliSecsSinceFiletimeEpoch * HUNDRED_NANO_PER_MILLI_RATIO; 124 } 125 126 132 public static Date filetimeToDate(long filetime) { 133 long milliSecsSinceFiletimeEpoch = filetime / HUNDRED_NANO_PER_MILLI_RATIO; 134 long milliSecsSinceUnixEpoch = milliSecsSinceFiletimeEpoch - FILETIME_EPOCH_DIFF; 135 return new Date (milliSecsSinceUnixEpoch); 136 } 137 138 145 protected ManagedCommandline buildGetModificationsCommand(Date lastBuild, Date now) { 146 ManagedCommandline cmdLine = buildCommonCommand(); 147 cmdLine.createArgument().setValue("find"); 148 cmdLine.createArgument().setValue(getPath()); 149 cmdLine.createArgument().setValue("-regex"); 150 cmdLine.createArgument().setValue("SCIT > " + dateToFiletime(lastBuild)); 151 cmdLine.createArgument().setValue("-format"); 152 cmdLine.createArgument().setValue("#SCIT#|#DbPath#|#Changed By#|#CheckInComment#"); 153 154 return cmdLine; 155 } 156 157 163 protected List getModificationsFromAlienBrain(Date lastBuild, Date now) 164 throws IOException , CruiseControlException { 165 166 if (getBranch() != null) { 167 setActiveBranch(getBranch()); 168 } 169 170 ManagedCommandline cmdLine = buildGetModificationsCommand(lastBuild, now); 171 LOG.debug("Executing: " + cmdLine.toString()); 172 cmdLine.execute(); 173 174 return parseModifications(cmdLine.getStdoutAsList()); 175 } 176 177 181 protected List parseModifications(List modifications) throws IOException { 182 List mods = new ArrayList (); 183 184 for (Iterator it = modifications.iterator(); it.hasNext(); ) { 185 String line = (String ) it.next(); 186 line = line.trim(); 187 if (line.equals(AB_NO_SESSION)) { 188 LOG.error(AB_NO_SESSION); 189 continue; 190 } else if (line.equals(AB_NO_MODIFICATIONS)) { 191 continue; 192 } else if (line.startsWith(AB_MODIFICATION_SUMMARY_PREFIX)) { 193 continue; 194 } else if (line.startsWith("|")) { 195 line = "0" + line; 198 } 199 200 Modification m = parseModificationDescription(line); 201 mods.add(m); 202 } 203 return mods; 204 } 205 206 210 protected static Modification parseModificationDescription(String description) { 211 Modification m = new Modification("AlienBrain"); 212 213 StringTokenizer st = new StringTokenizer (description, "|"); 214 215 m.modifiedTime = AlienBrain.filetimeToDate(Long.parseLong(st.nextToken())); 216 m.createModifiedFile(st.nextToken(), null); 217 m.userName = st.nextToken(); 218 while (st.hasMoreTokens()) { 219 m.comment += st.nextToken(); 220 } 221 222 return m; 223 } 224 } 225 | Popular Tags |