1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.util.Properties ; 40 41 import org.apache.log4j.Logger; 42 import org.apache.oro.text.regex.MalformedPatternException; 43 import org.apache.oro.text.regex.MatchResult; 44 import org.apache.oro.text.regex.Pattern; 45 import org.apache.oro.text.regex.PatternCompiler; 46 import org.apache.oro.text.regex.PatternMatcher; 47 import org.apache.oro.text.regex.Perl5Compiler; 48 import org.apache.oro.text.regex.Perl5Matcher; 49 import org.jdom.Element; 50 51 import net.sourceforge.cruisecontrol.CruiseControlException; 52 import net.sourceforge.cruisecontrol.sourcecontrols.CMSynergy; 53 import net.sourceforge.cruisecontrol.util.ManagedCommandline; 54 import net.sourceforge.cruisecontrol.util.ValidationHelper; 55 56 63 public class CMSynergyBaselinePublisher extends CMSynergyPublisher { 64 65 68 public static final String CCM_BASELINE_PURPOSE = "Integration Testing"; 69 70 private static final Logger LOG = Logger.getLogger(CMSynergyBaselinePublisher.class); 71 private static final Pattern LOG_PROPERTY_PATTERN; 72 private String purpose = CCM_BASELINE_PURPOSE; 73 private String name; 74 private String description; 75 76 static { 77 PatternCompiler compiler = new Perl5Compiler(); 79 try { 80 LOG_PROPERTY_PATTERN = compiler.compile("(.*)\\@\\{([^@{}]+)\\}(.*)"); 82 } catch (MalformedPatternException e) { 83 LOG.fatal("Error compiling pattern for property matching", e); 85 throw new IllegalStateException (); 86 } 87 88 } 89 90 95 public void setPurpose(String purpose) { 96 this.purpose = purpose; 97 } 98 99 111 public void setBaselineName(String name) { 112 this.name = name; 113 } 114 115 120 public void setDescription(String description) { 121 this.description = description; 122 } 123 124 127 public void publish(Element log) throws CruiseControlException { 128 129 if (!shouldPublish(log)) { 131 return; 132 } 133 134 Properties logProperties = getBuildProperties(log); 136 137 String baselineName = null; 139 if (name != null) { 140 baselineName = parsePropertiesInString(name, logProperties); 141 } 142 143 ManagedCommandline cmd = CMSynergy.createCcmCommand( 145 getCcmExe(), getSessionName(), getSessionFile()); 146 147 cmd.createArgument().setValue("baseline"); 148 cmd.createArgument().setValue("-create"); 149 if (baselineName != null) { 150 cmd.createArgument().setValue(baselineName); 151 } 152 if (description != null) { 153 cmd.createArgument().setValue("-description"); 154 cmd.createArgument().setValue(description); 155 } 156 cmd.createArgument().setValue("-release"); 157 cmd.createArgument().setValue(getProjectRelease()); 158 cmd.createArgument().setValue("-purpose"); 159 cmd.createArgument().setValue(purpose); 160 cmd.createArgument().setValue("-project"); 161 cmd.createArgument().setValue(getProject()); 162 cmd.createArgument().setValue("-subprojects"); 163 164 try { 166 cmd.execute(); 167 cmd.assertExitCode(0); 168 } catch (Exception e) { 169 StringBuffer error = new StringBuffer ( 170 "Failed to create intermediate baseline for project \""); 171 error.append(getProject()); 172 error.append("\"."); 173 throw new CruiseControlException(error.toString(), e); 174 } 175 176 StringBuffer message = new StringBuffer ("Created baseline"); 178 if (baselineName != null) { 179 message.append(" ").append(baselineName); 180 } 181 message.append("."); 182 LOG.info(message.toString()); 183 } 184 185 188 public void validate() throws CruiseControlException { 189 ValidationHelper.assertIsSet(getProject(), "project", this.getClass()); 190 } 191 192 197 private String getProjectRelease() throws CruiseControlException { 198 String release; 199 200 ManagedCommandline cmd = CMSynergy.createCcmCommand( 202 getCcmExe(), getSessionName(), getSessionFile()); 203 cmd.createArgument().setValue("attribute"); 204 cmd.createArgument().setValue("-show"); 205 cmd.createArgument().setValue("release"); 206 cmd.createArgument().setValue("-project"); 207 cmd.createArgument().setValue(getProject()); 208 209 try { 210 cmd.execute(); 211 cmd.assertExitCode(0); 212 release = cmd.getStdoutAsString().trim(); 213 } catch (Exception e) { 214 throw new CruiseControlException( 215 "Could not determine the release value of project \"" 216 + getProject() + "\".", e); 217 } 218 219 return release; 220 } 221 222 234 private String parsePropertiesInString(String string, Properties buildProperties) { 235 236 PatternMatcher matcher = new Perl5Matcher(); 237 238 while (matcher.contains(string, LOG_PROPERTY_PATTERN)) { 240 MatchResult result = matcher.getMatch(); 241 String pre = result.group(1); 242 String propertyName = result.group(2); 243 String post = result.group(3); 244 String value = buildProperties.getProperty(propertyName); 245 if (value == null) { 246 LOG.warn("Could not resolve property \"" + propertyName 247 + "\"."); 248 value = "_"; 249 } 250 string = pre + value + post; 251 } 252 253 return string; 254 } 255 } 256 | Popular Tags |