📄 ftp.java
字号:
configurationHasBeenSet(); } } /** * Defines how many times to retry executing FTP command before giving up. * Default is 0 - try once and if failure then give up. * * @param retriesAllowed number of retries to allow. -1 means * keep trying forever. "forever" may also be specified as a * synonym for -1. */ public void setRetriesAllowed(String retriesAllowed) { if ("FOREVER".equalsIgnoreCase(retriesAllowed)) { this.retriesAllowed = Retryable.RETRY_FOREVER; } else { try { int retries = Integer.parseInt(retriesAllowed); if (retries < Retryable.RETRY_FOREVER) { throw new BuildException( "Invalid value for retriesAllowed attribute: " + retriesAllowed); } this.retriesAllowed = retries; } catch (NumberFormatException px) { throw new BuildException( "Invalid value for retriesAllowed attribute: " + retriesAllowed); } } } /** * @return Returns the systemTypeKey. */ String getSystemTypeKey() { return systemTypeKey.getValue(); } /** * @return Returns the defaultDateFormatConfig. */ String getDefaultDateFormatConfig() { return defaultDateFormatConfig; } /** * @return Returns the recentDateFormatConfig. */ String getRecentDateFormatConfig() { return recentDateFormatConfig; } /** * @return Returns the serverLanguageCodeConfig. */ String getServerLanguageCodeConfig() { return serverLanguageCodeConfig.getValue(); } /** * @return Returns the serverTimeZoneConfig. */ String getServerTimeZoneConfig() { return serverTimeZoneConfig; } /** * @return Returns the shortMonthNamesConfig. */ String getShortMonthNamesConfig() { return shortMonthNamesConfig; } /** * @return Returns the timestampGranularity. */ Granularity getTimestampGranularity() { return timestampGranularity; } /** * Sets the timestampGranularity attribute * @param timestampGranularity The timestampGranularity to set. */ public void setTimestampGranularity(Granularity timestampGranularity) { if (null == timestampGranularity || "".equals(timestampGranularity)) { return; } this.timestampGranularity = timestampGranularity; } /** * Sets the siteCommand attribute. This attribute * names the command that will be executed if the action * is "site". * @param siteCommand The siteCommand to set. */ public void setSiteCommand(String siteCommand) { this.siteCommand = siteCommand; } /** * Sets the initialSiteCommand attribute. This attribute * names a site command that will be executed immediately * after connection. * @param initialCommand The initialSiteCommand to set. */ public void setInitialSiteCommand(String initialCommand) { this.initialSiteCommand = initialCommand; } /** * Checks to see that all required parameters are set. * * @throws BuildException if the configuration is not valid. */ protected void checkAttributes() throws BuildException { if (server == null) { throw new BuildException("server attribute must be set!"); } if (userid == null) { throw new BuildException("userid attribute must be set!"); } if (password == null) { throw new BuildException("password attribute must be set!"); } if ((action == LIST_FILES) && (listing == null)) { throw new BuildException("listing attribute must be set for list " + "action!"); } if (action == MK_DIR && remotedir == null) { throw new BuildException("remotedir attribute must be set for " + "mkdir action!"); } if (action == CHMOD && chmod == null) { throw new BuildException("chmod attribute must be set for chmod " + "action!"); } if (action == SITE_CMD && siteCommand == null) { throw new BuildException("sitecommand attribute must be set for site " + "action!"); } if (this.isConfigurationSet) { try { Class.forName("org.apache.commons.net.ftp.FTPClientConfig"); } catch (ClassNotFoundException e) { throw new BuildException( "commons-net.jar >= 1.4.0 is required for at least one" + " of the attributes specified."); } } } /** * Executable a retryable object. * @param h the retry hander. * @param r the object that should be retried until it succeeds * or the number of retrys is reached. * @param descr a description of the command that is being run. * @throws IOException if there is a problem. */ protected void executeRetryable(RetryHandler h, Retryable r, String descr) throws IOException { h.execute(r, descr); } /** * For each file in the fileset, do the appropriate action: send, get, * delete, or list. * * @param ftp the FTPClient instance used to perform FTP actions * @param fs the fileset on which the actions are performed. * * @return the number of files to be transferred. * * @throws IOException if there is a problem reading a file * @throws BuildException if there is a problem in the configuration. */ protected int transferFiles(final FTPClient ftp, FileSet fs) throws IOException, BuildException { DirectoryScanner ds; if (action == SEND_FILES) { ds = fs.getDirectoryScanner(getProject()); } else { // warn that selectors are not supported if (fs.getSelectors(getProject()).length != 0) { getProject().log("selectors are not supported in remote filesets", Project.MSG_WARN); } ds = new FTPDirectoryScanner(ftp); fs.setupDirectoryScanner(ds, getProject()); ds.setFollowSymlinks(fs.isFollowSymlinks()); ds.scan(); } String[] dsfiles = null; if (action == RM_DIR) { dsfiles = ds.getIncludedDirectories(); } else { dsfiles = ds.getIncludedFiles(); } String dir = null; if ((ds.getBasedir() == null) && ((action == SEND_FILES) || (action == GET_FILES))) { throw new BuildException("the dir attribute must be set for send " + "and get actions"); } else { if ((action == SEND_FILES) || (action == GET_FILES)) { dir = ds.getBasedir().getAbsolutePath(); } } // If we are doing a listing, we need the output stream created now. BufferedWriter bw = null; try { if (action == LIST_FILES) { File pd = listing.getParentFile(); if (!pd.exists()) { pd.mkdirs(); } bw = new BufferedWriter(new FileWriter(listing)); } RetryHandler h = new RetryHandler(this.retriesAllowed, this); if (action == RM_DIR) { // to remove directories, start by the end of the list // the trunk does not let itself be removed before the leaves for (int i = dsfiles.length - 1; i >= 0; i--) { final String dsfile = dsfiles[i]; executeRetryable(h, new Retryable() { public void execute() throws IOException { rmDir(ftp, dsfile); } }, dsfile); } } else { final BufferedWriter fbw = bw; final String fdir = dir; if (this.newerOnly) { this.granularityMillis = this.timestampGranularity.getMilliseconds(action); } for (int i = 0; i < dsfiles.length; i++) { final String dsfile = dsfiles[i]; executeRetryable(h, new Retryable() { public void execute() throws IOException { switch (action) { case SEND_FILES: sendFile(ftp, fdir, dsfile); break; case GET_FILES: getFile(ftp, fdir, dsfile); break; case DEL_FILES: delFile(ftp, dsfile); break; case LIST_FILES: listFile(ftp, fbw, dsfile); break; case CHMOD: doSiteCommand(ftp, "chmod " + chmod + " " + resolveFile(dsfile)); transferred++; break; default: throw new BuildException("unknown ftp action " + action); } } }, dsfile); } } } finally { if (bw != null) { bw.close(); } } return dsfiles.length; } /** * Sends all files specified by the configured filesets to the remote * server. * * @param ftp the FTPClient instance used to perform FTP actions * * @throws IOException if there is a problem reading a file * @throws BuildException if there is a problem in the configuration. */ protected void transferFiles(FTPClient ftp) throws IOException, BuildException { transferred = 0; skipped = 0; if (filesets.size() == 0) { throw new BuildException("at least one fileset must be specified."); } else { // get files from filesets for (int i = 0; i < filesets.size(); i++) { FileSet fs = (FileSet) filesets.elementAt(i); if (fs != null) { transferFiles(ftp, fs); } } } log(transferred + " " + ACTION_TARGET_STRS[action] + " " + COMPLETED_ACTION_STRS[action]); if (skipped != 0) { log(skipped + " " + ACTION_TARGET_STRS[action] + " were not successfully " + COMPLETED_ACTION_STRS[action]); } } /** * Correct a file path to correspond to the remote host requirements. This * implementation currently assumes that the remote end can handle * Unix-style paths with forward-slash separators. This can be overridden * with the <code>separator</code> task parameter. No attempt is made to * determine what syntax is appropriate for the remote host. * * @param file the remote file name to be resolved * * @return the filename as it will appear on the server. */ protected String resolveFile(String file) { return file.replace(System.getProperty("file.separator").charAt(0), remoteFileSep.charAt(0)); } /** * Creates all parent directories specified in a complete relative * pathname. Attempts to create existing directories will not caus
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -