001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: Get.java 11093 2007-12-21 18:22:22Z mpreston $
024: */
025: package com.bostechcorp.cbesb.runtime.ftp.interpreter;
026:
027: import java.io.File;
028: import java.io.FileNotFoundException;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.commons.net.ftp.FTPClient;
037: import org.apache.commons.net.ftp.FTPFile;
038:
039: /**
040: * Retrieves the specified file from the FTP server.
041: *
042: * @author j.zhang
043: * @version 1.0.0
044: */
045: public class Get extends AbstractCommandExpression {
046:
047: protected final transient Log logger = LogFactory
048: .getLog(getClass());
049:
050: /**
051: * The file name stored into the local file system; if omitted, the specified file
052: * name will be will be used to store file in the default local directory.
053: */
054: public static final String LOCAL_NAME = "localName";
055:
056: /**
057: * The path to the file in the FTP server.
058: */
059: public static final String GET = "get";
060:
061: /**
062: * The attribute indicate the file should be deleted or not after get it from the FTP server.
063: */
064: public static final String DELETE_AFTER_TRANSFER = "deleteAfterTransfer";
065:
066: /**
067: * The Map of parameters.
068: */
069: // private Map paramMap = new HashMap<String, String>();
070: private Map paramMap;
071:
072: @Override
073: /* (non-Javadoc)
074: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
075: */
076: public void interpret(Map<String, String> paramMap) {
077: this .paramMap = paramMap;
078: }
079:
080: @Override
081: /* (non-Javadoc)
082: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#execute(org.apache.commons.net.ftp.FTPClient)
083: */
084: public Object execute(FtpExecContext context) {
085: logger.debug("get:");
086: FTPClient ftp = context.getFtpclient();
087:
088: String localName = (String) paramMap.get(LOCAL_NAME);
089: String get = (String) paramMap.get(GET);
090: String deleteAfterTransfer = (String) paramMap
091: .get(DELETE_AFTER_TRANSFER);
092: boolean deleteFlag = false;
093: if (deleteAfterTransfer != null
094: && !"".equals(deleteAfterTransfer)) {
095: deleteFlag = Boolean.parseBoolean(deleteAfterTransfer
096: .toLowerCase());
097: }
098: File remoteFile = new File(get);
099: File localFile;
100: if (localName == null || "".equals(localName)) {
101: localFile = new File(context.getLocalWorkDir(), remoteFile
102: .getName());
103: } else {
104: localFile = new File(context.getLocalWorkDir(), localName);
105: }
106: logger.debug(" Remote File: " + remoteFile.getName());
107: logger.debug(" Local File: " + localFile.getAbsolutePath());
108: try {
109: String ftpPath = ftp.printWorkingDirectory();
110: if (get.contains("/")) {
111: int separator = get.lastIndexOf("/");
112: if (separator != -1 && separator != 0) {
113: ftpPath = ftpPath + get.substring(0, separator);
114: }
115: } else if (get.contains("\\")) {
116: int separator = get.lastIndexOf("\\");
117: if (separator != -1 && separator != 0) {
118: ftpPath = ftpPath + get.substring(0, separator);
119: }
120: }
121: FTPFile[] ftpFiles = ftp.listFiles(ftpPath);
122:
123: for (int i = 0; i < ftpFiles.length; i++) {
124: if (ftpFiles[i].isDirectory()) {
125: continue;
126: }
127: if (ftpFiles[i].getName().equals(remoteFile.getName())) {
128: long fileSize = ftpFiles[i].getSize();
129: boolean twoPassFlag = true;
130: while (twoPassFlag) {
131: Thread.sleep(2000);
132: long tempFileSize = ftpFiles[i].getSize();
133: if (fileSize == tempFileSize) {
134: twoPassFlag = false;
135: }
136: }
137: }
138: }
139:
140: FileOutputStream stream = new FileOutputStream(localFile
141: .getAbsolutePath());
142: ftp.retrieveFile(remoteFile.getPath(), stream);
143: stream.close();
144: if (deleteFlag) {
145: if (!ftp.deleteFile(remoteFile.getPath())) {
146: throw new Exception("Cannot delete remote file: "
147: + remoteFile.getPath());
148: }
149: }
150: } catch (FileNotFoundException fnfe) {
151: // fnfe.printStackTrace();
152: return fnfe;
153: } catch (IOException ioe) {
154: // ioe.printStackTrace();
155: return ioe;
156: } catch (Exception e) {
157: return e;
158: }
159: return true;
160: }
161: }
|