1 37 package net.sourceforge.cruisecontrol.bootstrappers; 38 39 import net.sourceforge.cruisecontrol.Bootstrapper; 40 import net.sourceforge.cruisecontrol.CruiseControlException; 41 import net.sourceforge.cruisecontrol.util.Commandline; 42 import net.sourceforge.cruisecontrol.util.StreamPumper; 43 import net.sourceforge.cruisecontrol.util.ValidationHelper; 44 45 import org.apache.log4j.Logger; 46 47 import java.io.File ; 48 import java.io.IOException ; 49 import java.io.PrintWriter ; 50 51 58 public class SVNBootstrapper implements Bootstrapper { 59 private static final Logger LOG = Logger.getLogger(SVNBootstrapper.class); 60 61 62 private String fileName; 63 private String localWorkingCopy; 64 private String userName; 65 private String password; 66 67 70 public void setFile(String fileName) { 71 this.fileName = fileName; 72 } 73 74 81 public void setLocalWorkingCopy(String localWorkingCopy) { 82 this.localWorkingCopy = localWorkingCopy; 83 } 84 85 88 public void setUsername(String userName) { 89 this.userName = userName; 90 } 91 92 95 public void setPassword(String password) { 96 this.password = password; 97 } 98 99 107 public void validate() throws CruiseControlException { 108 ValidationHelper.assertTrue(fileName != null || localWorkingCopy != null, 109 "At least 'filename' or 'localWorkingCopy' is a " 110 + "required attribute on the Subversion bootstrapper task"); 111 112 if (localWorkingCopy != null) { 113 File workingDir = new File (localWorkingCopy); 114 ValidationHelper.assertTrue(workingDir.exists() && workingDir.isDirectory(), 115 "'localWorkingCopy' must be an existing " + "directory."); 116 } 117 } 118 119 122 public void bootstrap() { 123 try { 124 Commandline commandLine = buildUpdateCommand(); 125 execUpdateCommand(commandLine); 126 } catch (Exception e) { 127 LOG.error("Error executing svn update command", e); 128 } 129 } 130 131 138 Commandline buildUpdateCommand() throws CruiseControlException { 139 Commandline command = new Commandline(); 140 command.setExecutable("svn"); 141 142 if (localWorkingCopy != null) { 143 command.setWorkingDirectory(localWorkingCopy); 144 } 145 146 command.createArgument().setValue("update"); 147 command.createArgument().setValue("--non-interactive"); 148 if (userName != null) { 149 command.createArgument().setValue("--username"); 150 command.createArgument().setValue(userName); 151 } 152 if (password != null) { 153 command.createArgument().setValue("--password"); 154 command.createArgument().setValue(password); 155 } 156 if (fileName != null) { 157 command.createArgument().setValue(fileName); 158 } 159 160 LOG.debug("SVNBootstrapper: Executing command = " + command); 161 162 return command; 163 } 164 165 private void execUpdateCommand(Commandline command) 166 throws IOException , InterruptedException { 167 168 Process p = command.execute(); 169 170 logErrorStream(p); 171 logOutStream(p); 172 173 p.waitFor(); 174 p.getInputStream().close(); 175 p.getOutputStream().close(); 176 p.getErrorStream().close(); 177 } 178 179 private void logErrorStream(Process p) { 180 StreamPumper errorPumper = 181 new StreamPumper(p.getErrorStream(), new PrintWriter (System.err, true)); 182 new Thread (errorPumper).start(); 183 } 184 185 private void logOutStream(Process p) { 186 StreamPumper outPumper = 187 new StreamPumper(p.getInputStream(), new PrintWriter (System.out, true)); 188 new Thread (outPumper).start(); 189 } 190 } 191 | Popular Tags |