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.util.StreamPumper; 42 import org.apache.log4j.Logger; 43 44 import java.io.BufferedReader ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.InputStreamReader ; 48 import java.text.ParseException ; 49 import java.text.SimpleDateFormat ; 50 import java.util.ArrayList ; 51 import java.util.Collections ; 52 import java.util.Date ; 53 import java.util.Hashtable ; 54 import java.util.List ; 55 import java.util.Map ; 56 57 62 public class SSCM implements net.sourceforge.cruisecontrol.SourceControl { 63 64 private static final Logger LOG = Logger.getLogger(SSCM.class); 65 private final SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 66 67 private SSCMCLIStringParam strparamBranch = new SSCMCLIStringParam("branch", "-b", false); 68 private SSCMCLIStringParam strparamRepository = new SSCMCLIStringParam("repository", "-p", false); 69 private SSCMCLIStringParam strparamFile = new SSCMCLIStringParam("file", "", false); 70 private SSCMCLIStringParam strparamServerConnect = new SSCMCLIStringParam("serverconnect", "-z", false); 71 private SSCMCLIStringParam strparamServerLogin = new SSCMCLIStringParam("serverlogin", "-y", false); 72 private SSCMCLIBoolParam fparamSearchRegExp = new SSCMCLIBoolParam("searchregexp", "-x", false); 73 private SSCMCLIBoolParam fparamRecursive = new SSCMCLIBoolParam("recursive", "-r", false); 74 75 private Hashtable hashProperties = new Hashtable (); 76 private String strProperty = null; 77 78 public void validate() throws CruiseControlException { } 79 80 public void setBranch(String str) { 81 strparamBranch.setData(str); 82 } 83 84 public void setRepository(String str) { 85 strparamRepository.setData(str); 86 } 87 88 public void setFile(String str) { 89 strparamFile.setData(str); 90 } 91 92 public void setServerConnect(String str) { 93 strparamServerConnect.setData(str); 94 } 95 96 public void setServerLogin(String str) { 97 strparamServerLogin.setData(str); 98 } 99 100 public void setSearchRegExp(String str) { 101 if (str.equals("1")) { 102 fparamSearchRegExp.setData(null); 103 } 104 } 105 106 public void setRecursive(String str) { 107 if (str.equals("1")) { 108 fparamRecursive.setData(null); 109 } 110 } 111 112 public List getModifications(Date lastBuild, Date now) { 113 java.util.List paramList = new java.util.ArrayList (); 114 if (!strparamFile.isSet()) { 115 strparamFile.setData("/"); 116 } 117 paramList.add(strparamFile); 118 paramList.add(strparamBranch); 119 paramList.add(strparamRepository); 120 paramList.add(fparamRecursive); 121 paramList.add(fparamSearchRegExp); 122 paramList.add(strparamServerLogin); 123 paramList.add(strparamServerConnect); 124 125 List listMods = executeCLICommand(paramList, buildDateTimeRangeCLIParam(lastBuild, now)); 126 127 if (listMods == null) { 128 listMods = Collections.EMPTY_LIST; 129 } 130 if (listMods.size() > 0 && strProperty != null) { 131 hashProperties.put(strProperty, "true"); 132 } 133 134 return listMods; 135 } 136 137 public Map getProperties() { 138 return (hashProperties); 139 } 140 141 public void setProperty(String property) { 142 strProperty = property; 143 } 144 145 protected List executeCLICommand(java.util.List paramList, String strDTRangeParam) { 146 List listMods = null; 147 StringBuffer strbufferCmdLine = new StringBuffer ("sscm cc "); 148 149 boolean fAllRequirementsMet = true; 151 for (int i = 0; i < paramList.size() && fAllRequirementsMet; ++i) { 152 SSCMCLIParam param = (SSCMCLIParam) paramList.get(i); 153 if (param != null) { 154 if (param.checkRequired()) { 155 String str = param.getFormatted(); 156 if (str != null) { 157 strbufferCmdLine.append(str); 158 strbufferCmdLine.append(' '); 159 } 160 } else { 161 fAllRequirementsMet = false; 162 LOG.error("Required parameter '" + param.getParamName() + "' is missing!"); 163 } 164 } 165 } 166 167 if (fAllRequirementsMet) { 168 strbufferCmdLine.append(' '); 169 strbufferCmdLine.append(strDTRangeParam); 170 strbufferCmdLine.append(' '); 171 172 LOG.debug("\n" + strbufferCmdLine + "\n"); 173 174 try { 175 Process process = Runtime.getRuntime().exec(strbufferCmdLine.toString()); 176 new Thread (new StreamPumper(process.getErrorStream())).start(); 177 178 InputStream input = process.getInputStream(); 179 listMods = parseCLIOutput(input); 180 181 process.waitFor(); 182 183 process.getInputStream().close(); 184 process.getOutputStream().close(); 185 process.getErrorStream().close(); 186 } catch (IOException e) { 187 LOG.error("Problem trying to execute command line process", e); 188 } catch (InterruptedException e) { 189 LOG.error("Problem trying to execute command line process", e); 190 } 191 } 192 193 return listMods; 194 } 195 196 protected List parseCLIOutput(InputStream input) throws IOException { 197 List listMods = new ArrayList (); 198 BufferedReader reader = new BufferedReader (new InputStreamReader (input)); 199 200 String line = reader.readLine(); 201 202 if (!"total-0".equals(line)) { 204 while ((line = reader.readLine()) != null) { 205 Modification mod = parseOutputLine(line); 206 if (mod != null) { 207 listMods.add(mod); 208 } 209 } 210 } 211 212 return listMods; 213 } 214 215 protected Modification parseOutputLine(String str) { 216 LOG.debug("Output-" + str + "-\n"); 217 218 if (str == null || str.length() == 0) { 219 return null; 220 } 221 Modification mod = new Modification("sscm"); 222 Modification.ModifiedFile modfile = mod.createModifiedFile(null, null); 223 224 boolean fValid = false; 225 String strToken = "><"; 226 int iLeft = 1; 227 228 int iRight = str.indexOf(strToken, iLeft); 230 if (iRight > iLeft) { 231 modfile.folderName = str.substring(iLeft, iRight); 232 iLeft = iRight + strToken.length(); 233 234 iRight = str.indexOf(strToken, iLeft); 236 if (iRight > iLeft) { 237 modfile.fileName = str.substring(iLeft, iRight); 238 iLeft = iRight + strToken.length(); 239 240 iRight = str.indexOf(strToken, iLeft); 242 if (iRight > iLeft) { 243 mod.revision = str.substring(iLeft, iRight); 244 iLeft = iRight + strToken.length(); 245 246 iRight = str.indexOf(strToken, iLeft); 248 if (iRight > iLeft) { 249 modfile.action = str.substring(iLeft, iRight); 250 iLeft = iRight + strToken.length(); 251 252 iRight = str.indexOf(strToken, iLeft); 254 if (iRight > iLeft) { 255 mod.modifiedTime = buildDateTimeFromCLIOutput(str.substring(iLeft, iRight)); 256 iLeft = iRight + strToken.length(); 257 258 iRight = str.indexOf(strToken, iLeft); 260 if (iRight >= iLeft) { 261 mod.comment = str.substring(iLeft, iRight); 262 iLeft = iRight + strToken.length(); 263 264 iRight = str.indexOf(strToken, iLeft); 266 if (iRight > iLeft) { 267 mod.userName = str.substring(iLeft, iRight); 268 iLeft = iRight + strToken.length(); 269 270 iRight = str.indexOf(">", iLeft); 272 if (iRight > iLeft) { 273 mod.emailAddress = str.substring(iLeft, iRight); 274 fValid = true; 275 276 if (strProperty != null) { 277 hashProperties.put(strProperty, "true"); 278 } 279 } 280 } 281 } 282 } 283 } 284 } 285 } 286 } 287 288 if (!fValid) { 289 mod = null; 290 LOG.debug("Invalid output; skipping this entry"); 291 } 292 return (mod); 293 } 294 295 protected String buildDateTimeRangeCLIParam(Date lastBuild, Date now) { 296 String strLast = formatter.format(lastBuild); 297 String strNow = formatter.format(now); 298 return "-d" + strLast + ":" + strNow; 299 } 300 301 protected Date buildDateTimeFromCLIOutput(String str) { 302 Date dt; 303 try { 304 dt = formatter.parse(str); 305 } catch (ParseException e) { 306 dt = null; 307 LOG.error("Unable to parse DateTime from Surround", e); 308 } 309 return dt; 310 } 311 312 public abstract static class SSCMCLIParam { 313 public SSCMCLIParam(String strParamNameIN, String strParamIN, boolean fIsRequiredIN) { 314 strParamName = strParamNameIN; 315 strParam = strParamIN; 316 fIsRequired = fIsRequiredIN; 317 fIsSet = false; 318 } 319 320 public String getParamName() { 321 return (strParamName); 322 } 323 324 public String getParam() { 325 return (strParam); 326 } 327 328 public void setRequired(boolean f) { 329 fIsRequired = f; 330 } 331 332 public boolean isRequired() { 333 return fIsRequired; 334 } 335 336 public boolean isSet() { 337 return fIsSet; 338 } 339 340 public boolean checkRequired() { 341 return !(isRequired() && !isSet()); 342 } 343 344 public abstract String getFormatted(); 345 346 public abstract void setData(Object obj); 347 348 protected void setSet(boolean f) { 349 fIsSet = f; 350 } 351 352 private String strParamName; 353 private String strParam; 354 private boolean fIsRequired; 355 private boolean fIsSet; 356 } 357 358 public static class SSCMCLIBoolParam extends SSCMCLIParam { 359 public SSCMCLIBoolParam(String strParamNameIN, String strParamIN, boolean fIsRequiredIN) { 360 super(strParamNameIN, strParamIN, fIsRequiredIN); 361 } 362 363 public void setData(Object obj) { 364 fData = true; 365 setSet(true); 366 } 367 368 public String getFormatted() { 369 String str = null; 370 if (isSet() && fData) { 371 str = getParam(); 372 } 373 return str; 374 } 375 376 private boolean fData; 377 } 378 379 public static class SSCMCLIStringParam extends SSCMCLIParam { 380 public SSCMCLIStringParam(String strParamNameIN, String strParamIN, boolean fIsRequiredIN) { 381 super(strParamNameIN, strParamIN, fIsRequiredIN); 382 } 383 384 public void setData(Object obj) { 385 strData = (String ) obj; 386 setSet(true); 387 } 388 389 public String getFormatted() { 390 String str = null; 391 if (isSet()) { 392 str = getParam() + strData; 393 } 394 return str; 395 } 396 397 private String strData; 398 } 399 400 } 401 402 | Popular Tags |