1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.util.Iterator ; 40 import java.util.List ; 41 42 import net.sourceforge.cruisecontrol.CruiseControlException; 43 import net.sourceforge.cruisecontrol.sourcecontrols.CMSynergy; 44 import net.sourceforge.cruisecontrol.util.ManagedCommandline; 45 import net.sourceforge.cruisecontrol.util.ValidationHelper; 46 47 import org.apache.log4j.Logger; 48 import org.jdom.Element; 49 50 56 public class CMSynergyTaskPublisher extends CMSynergyPublisher { 57 58 private static final Logger LOG = Logger.getLogger(CMSynergyTaskPublisher.class); 59 private String folderNumber; 60 private String folderName; 61 62 69 public void setFolderName(String folderName) { 70 this.folderName = folderName; 71 } 72 73 78 public void setFolderNumber(String folderNumber) { 79 this.folderNumber = folderNumber; 80 } 81 82 85 public void publish(Element log) throws CruiseControlException { 86 87 if (!shouldPublish(log)) { 89 return; 90 } 91 92 if (folderNumber == null) { 94 folderNumber = getFolderNumber(folderName, getProject()); 95 } 96 97 StringBuffer tasks = new StringBuffer (); 99 List newTasksList = getNewTasks(log); 100 Iterator newTasks = newTasksList.iterator(); 101 for (int index = 0; newTasks.hasNext(); index++) { 102 if (index > 0) { 103 tasks.append(","); 104 } 105 tasks.append(newTasks.next()); 106 } 107 108 LOG.info("Copying " + newTasksList.size() + " task(s) into folder " 109 + folderNumber + "."); 110 111 ManagedCommandline cmd = CMSynergy.createCcmCommand( 113 getCcmExe(), getSessionName(), getSessionFile()); 114 cmd.createArgument().setValue("folder"); 115 cmd.createArgument().setValue("-modify"); 116 cmd.createArgument().setValue("-add_tasks"); 117 cmd.createArgument().setValue(tasks.toString()); 118 cmd.createArgument().setValue(folderNumber); 119 120 try { 121 cmd.execute(); 122 cmd.assertExitCode(0); 123 } catch (Exception e) { 124 throw new CruiseControlException( 125 "Failed to copy new tasks to folder " + folderNumber, e); 126 } 127 } 128 129 134 public void validate() throws CruiseControlException { 135 ValidationHelper.assertFalse(folderNumber == null && folderName == null, 136 "Must specify either 'folderName' or 'folderNumber'"); 137 138 ValidationHelper.assertFalse(folderName != null && getProject() == null, 139 "'project' attribute must be set when using the 'folderName' attribute."); 140 } 141 142 157 private String getFolderNumber(String folderName, String project) throws CruiseControlException { 158 ManagedCommandline cmd = CMSynergy.createCcmCommand( 160 getCcmExe(), getSessionName(), getSessionFile()); 161 cmd.createArgument().setValue("reconfigure_properties"); 162 cmd.createArgument().setValue("-u"); 163 cmd.createArgument().setValue("-f"); 164 cmd.createArgument().setValue( 165 "%description" + CMSynergy.CCM_ATTR_DELIMITER + "%name"); 166 cmd.createArgument().setValue("-show"); 167 cmd.createArgument().setValue("folders"); 168 cmd.createArgument().setValue(project); 169 170 try { 171 cmd.execute(); 172 cmd.assertExitCode(0); 173 } catch (Exception e) { 174 throw new CruiseControlException("Could not get a list of folders in project " + project, e); 175 } 176 177 for (Iterator folders = cmd.getStdoutAsList().iterator(); folders 179 .hasNext();) { 180 String line = (String ) folders.next(); 181 if (line.indexOf(folderName) > -1) { 182 int index = line.indexOf(CMSynergy.CCM_ATTR_DELIMITER); 183 if (index == -1) { 184 LOG.warn("Bad format in result: \"" + line + "\""); 185 continue; 186 } 187 index += CMSynergy.CCM_ATTR_DELIMITER.length(); 188 return line.substring(index).trim(); 189 } 190 } 191 192 throw new CruiseControlException("Could not find a folder matching \"" 194 + folderName + "\" in project \"" + project + "\"."); 195 } 196 197 } 198 | Popular Tags |