1 37 package net.sourceforge.cruisecontrol.util; 38 39 import java.io.BufferedInputStream ; 40 import java.io.ByteArrayInputStream ; 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 import java.io.InputStream ; 44 import java.io.IOException ; 45 import java.util.Vector ; 46 import java.util.StringTokenizer ; 47 48 import org.apache.log4j.Logger; 49 import org.apache.commons.net.ftp.FTPClient; 50 import org.apache.commons.net.ftp.FTPReply; 51 import org.apache.commons.net.ftp.FTP; 52 import net.sourceforge.cruisecontrol.CruiseControlException; 53 54 60 public abstract class AbstractFTPClass { 61 62 private static final Logger LOG = Logger.getLogger(AbstractFTPClass.class); 63 64 private String targetHost; 65 private int targetPort = 21; 66 private String targetUser = "anonymous"; 67 private String targetPasswd = "eat@joes.com"; 68 private String targetDir = "."; 69 private String targetSeparator = "/"; 70 71 private boolean passive = false; 72 73 74 public void setTargetUser(String targetUser) { 75 this.targetUser = targetUser; 76 } 77 78 public void setTargetHost(String targetHost) { 79 this.targetHost = targetHost; 80 } 81 82 public void setTargetPort(int targetPort) { 83 this.targetPort = targetPort; 84 } 85 86 public void setTargetPasswd(String targetPasswd) { 87 this.targetPasswd = targetPasswd; 88 } 89 90 public void setTargetDir(String targetDir) { 91 this.targetDir = targetDir; 92 } 93 94 public void setTargetSeparator(String targetSeparator) { 95 this.targetSeparator = targetSeparator; 96 } 97 98 public void setPassive(boolean p) { 99 this.passive = p; 100 } 101 102 103 109 public void validate() throws CruiseControlException { 110 if (targetHost == null) { 111 throw new CruiseControlException("'targethost' not specified in configuration file"); 112 } 113 if (targetDir == null) { 114 targetDir = "."; 115 } 116 } 117 118 119 protected FTPClient openFTP() throws CruiseControlException { 120 LOG.info("Opening FTP connection to " + targetHost); 121 122 FTPClient ftp = new FTPClient(); 123 124 try { 125 ftp.connect(targetHost, targetPort); 126 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { 127 throw new CruiseControlException("FTP connection failed: " 128 + ftp.getReplyString()); 129 } 130 131 LOG.info("logging in to FTP server"); 132 if (!ftp.login(targetUser, targetPasswd)) { 133 throw new CruiseControlException("Could not login to FTP server"); 134 } 135 LOG.info("login succeeded"); 136 137 if (passive) { 138 setPassive(ftp); 139 } 140 } catch (IOException ioe) { 141 LOG.error(ioe); 142 throw new CruiseControlException(ioe.getMessage()); 143 } 144 return ftp; 145 } 146 147 148 protected void closeFTP(FTPClient ftp) throws CruiseControlException { 149 if (ftp != null && ftp.isConnected()) { 150 try { 151 LOG.info("disconnecting"); 152 ftp.logout(); 153 ftp.disconnect(); 154 } catch (IOException ex) { 155 } 157 } 158 } 159 160 161 protected void setBinary(FTPClient ftp) throws CruiseControlException { 162 try { 163 ftp.setFileType(FTP.IMAGE_FILE_TYPE); 164 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { 165 throw new CruiseControlException( 166 "could not set transfer type: " 167 + ftp.getReplyString()); 168 } 169 } catch (IOException ex) { 170 LOG.error(ex); 171 throw new CruiseControlException(ex.getMessage()); 172 } 173 } 174 175 176 private void setPassive(FTPClient ftp) throws CruiseControlException { 177 LOG.info("entering passive mode"); 178 ftp.enterLocalPassiveMode(); 179 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { 180 throw new CruiseControlException("could not enter into passive " 181 + "mode: " + ftp.getReplyString()); 182 } 183 } 184 185 186 protected void makeDir(FTPClient ftp, String dir, boolean ignoreFailures) 187 throws CruiseControlException { 188 dir = targetDir + targetSeparator + dir; 189 try { 190 if (!ftp.makeDirectory(dir)) { 191 int rc = ftp.getReplyCode(); 195 if (!(ignoreFailures 196 && (rc == 550 || rc == 553 || rc == 521))) { 197 throw new CruiseControlException( 198 "could not create directory: " 199 + ftp.getReplyString()); 200 } 201 LOG.info("directory already exists"); 202 } else { 203 LOG.info("directory created OK"); 204 } 205 } catch (IOException ex) { 206 LOG.error(ex); 207 throw new CruiseControlException(ex.getMessage()); 208 } 209 } 210 211 212 215 protected void sendFile(FTPClient ftp, File infile, String outfilename) 216 throws CruiseControlException { 217 InputStream instream = null; 218 try { 219 LOG.info("transferring " + infile.getAbsolutePath()); 220 221 instream = new BufferedInputStream (new FileInputStream (infile)); 222 sendStream(ftp, instream, outfilename); 223 } catch (IOException ioe) { 224 throw new CruiseControlException(ioe.getMessage()); 225 } finally { 226 if (instream != null) { 227 try { 228 instream.close(); 229 } catch (IOException ex) { 230 } 232 } 233 } 234 } 235 236 237 240 protected void sendStream(FTPClient ftp, InputStream instream, 241 String outfilename) throws CruiseControlException { 242 LOG.info("transferring to file " + outfilename); 243 outfilename = targetDir + targetSeparator + resolveFile(outfilename); 244 245 try { 246 ftp.storeFile(outfilename, instream); 247 boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode()); 248 249 if (!success) { 250 throw new CruiseControlException("could not put file: " 251 + ftp.getReplyString()); 252 } 253 } catch (IOException ex) { 254 LOG.error(ex); 255 throw new CruiseControlException(ex.getMessage()); 256 } 257 } 258 259 260 protected String resolveFile(String file) { 261 return file.replace(File.separatorChar, targetSeparator.charAt(0)); 262 } 263 264 265 protected void makeDirsForFile(FTPClient ftp, String filename, 266 Vector knownPaths) throws CruiseControlException { 267 String fname = resolveFile(filename); 268 LOG.info("making dirs for file " + fname); 269 int pos = fname.lastIndexOf(targetSeparator); 270 if (pos > 0) { 271 makeDirs(ftp, fname.substring(0, pos), knownPaths); 272 } 273 } 274 275 276 281 protected void makeDirs(FTPClient ftp, String pathname, 282 Vector knownPaths) throws CruiseControlException { 283 if (knownPaths == null) { 284 knownPaths = new Vector (); 285 } 286 287 StringTokenizer st = new StringTokenizer (targetDir + targetSeparator 288 + resolveFile(pathname), targetSeparator, false); 289 290 try { 291 String cwd = ftp.printWorkingDirectory(); 292 LOG.info("makeDirs: current dir = " + cwd); 293 String fullPath = targetDir; 294 while (st.hasMoreTokens()) { 295 String dir = st.nextToken(); 296 if (dir == null || dir.length() <= 0) { 297 continue; 298 } 299 fullPath += targetSeparator + dir; 300 LOG.info("makeDirs: dir = " + dir + ", fullPath = " 301 + fullPath); 302 308 if (!ftp.changeWorkingDirectory(dir)) { 309 LOG.info("makeDirs: could not CD into " + dir); 310 if (!ftp.makeDirectory(dir)) { 311 throw new CruiseControlException( 312 "could not create directory [" + dir + ", full=" 313 + fullPath + "]: " 314 + ftp.getReplyString()); 315 } 316 LOG.info("makeDirs: created dir " + dir); 317 if (!ftp.changeWorkingDirectory(dir)) { 318 throw new CruiseControlException( 319 "could not change to directory: " 320 + ftp.getReplyString()); 321 } 322 LOG.info("makeDirs: CDed into " + dir); 323 } 324 knownPaths.addElement(fullPath); 325 } 326 ftp.changeWorkingDirectory(cwd); 327 } catch (IOException ex) { 328 LOG.error(ex); 329 throw new CruiseControlException(ex.getMessage()); 330 } 331 } 332 333 339 protected void sendFileToFTPPath(String text, String path) throws CruiseControlException { 340 ByteArrayInputStream bais = new ByteArrayInputStream ( 341 text.getBytes()); 342 343 FTPClient ftp = openFTP(); 344 345 347 try { 348 makeDirsForFile(ftp, path, null); 349 sendStream(ftp, bais, path); 350 } finally { 351 closeFTP(ftp); 352 } 353 } 354 } 355 | Popular Tags |