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: Mget.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.FileOutputStream;
029: import java.io.IOException;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.commons.net.ftp.FTPClient;
036: import org.apache.commons.net.ftp.FTPFile;
037:
038: import com.bostechcorp.cbesb.runtime.file.FilePatternMatcher;
039:
040: /**
041: * Retrieves the specified file(s) from the FTP server based on regular expression file
042: * pattern.
043: *
044: * @author j.zhang
045: * @version 1.0.0
046: */
047: public class Mget extends AbstractCommandExpression {
048:
049: protected final transient Log logger = LogFactory
050: .getLog(getClass());
051:
052: /**
053: * The regular expression file pattern specified the files in the FTP server.
054: */
055: public static final String MGET = "mget";
056:
057: /**
058: * The attribute indicate the file should be deleted or not after get it from the FTP server.
059: */
060: public static final String DELETE_AFTER_TRANSFER = "deleteAfterTransfer";
061:
062: /**
063: * The Map of parameters.
064: */
065: private Map paramMap = new HashMap<String, String>();
066:
067: @Override
068: /* (non-Javadoc)
069: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
070: */
071: public void interpret(Map<String, String> paramMap) {
072: this .paramMap = paramMap;
073: }
074:
075: @Override
076: /* (non-Javadoc)
077: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#execute(org.apache.commons.net.ftp.FTPClient)
078: */
079: public Object execute(FtpExecContext context) {
080: logger.debug("mget:");
081: FTPClient ftp = context.getFtpclient();
082: String mget = (String) paramMap.get(MGET);
083: String deleteAfterTransfer = (String) paramMap
084: .get(DELETE_AFTER_TRANSFER);
085: boolean deleteFlag = false;
086: if (deleteAfterTransfer != null
087: && !"".equals(deleteAfterTransfer)) {
088: deleteFlag = Boolean.parseBoolean(deleteAfterTransfer
089: .toLowerCase());
090: }
091: String filePattern = "*";
092: try {
093: String currentWorkingDir = ftp.printWorkingDirectory();
094: int slash = mget.lastIndexOf("/");
095: if (slash != -1 && slash != 0) {
096: slash = mget.lastIndexOf("\\");
097: }
098: if (slash != -1 && slash != 0) {
099: String changeDir = mget.substring(0, slash);
100: boolean changeWorkingDirFlag = ftp
101: .changeWorkingDirectory(changeDir);
102: if (!changeWorkingDirFlag) {
103: throw new Exception(
104: "Cannot find directory on ftp server: "
105: + changeDir);
106: }
107: }
108: FTPFile[] files = ftp.listFiles();
109: for (int i = 0; i < files.length; i++) {
110: if (files[i].isDirectory()) {
111: continue;
112: }
113: if (slash != -1) {
114: filePattern = mget.substring(slash + 1);
115: } else {
116: filePattern = mget;
117: }
118: if (FilePatternMatcher.matchPattern(files[i].getName(),
119: filePattern, "Glob")) {
120: long fileSize = files[i].getSize();
121: boolean twoPassFlag = true;
122: while (twoPassFlag) {
123: Thread.sleep(2000);
124: long tempFileSize = files[i].getSize();
125: if (fileSize == tempFileSize) {
126: twoPassFlag = false;
127: }
128: }
129: File newFile = new File(context.getLocalWorkDir(),
130: files[i].getName());
131: FileOutputStream stream = new FileOutputStream(
132: newFile.getAbsolutePath());
133: boolean retrieveFlag = ftp.retrieveFile(files[i]
134: .getName(), stream);
135: stream.close();
136: if (!retrieveFlag) {
137: throw new Exception("Failed to retrieve file '"
138: + files[i].getName());
139: }
140: if (deleteFlag) {
141: if (!ftp.deleteFile(files[i].getName())) {
142: throw new Exception("Cannot delete file: "
143: + files[i].getName());
144: }
145: }
146: }
147: }
148: if (slash != -1 && slash != 0) {
149: if (!ftp.changeWorkingDirectory(currentWorkingDir)) {
150: throw new Exception(
151: "Cannot change back to original directory '"
152: + currentWorkingDir + "'");
153: }
154: }
155: } catch (IOException ioe) {
156: // ioe.printStackTrace();
157: return ioe;
158: } catch (Exception e) {
159: // e.printStackTrace();
160: return e;
161: }
162: return true;
163: }
164: }
|