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: Mput.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.FileInputStream;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.commons.net.ftp.FTPClient;
035:
036: import com.bostechcorp.cbesb.runtime.file.FileOperator;
037: import com.bostechcorp.cbesb.runtime.file.FilePatternMatcher;
038:
039: /**
040: * Transfers the specified file(s) to the FTP server based on regular expression file
041: * pattern.
042: *
043: * @author j.zhang
044: * @version 1.0.0
045: */
046: public class Mput extends AbstractCommandExpression {
047:
048: protected final transient Log logger = LogFactory
049: .getLog(getClass());
050:
051: /**
052: * The regular expression file pattern specified the files in local file system.
053: */
054: public static final String MPUT = "mput";
055:
056: /**
057: * The Map of parameters.
058: */
059: private Map paramMap = new HashMap<String, String>();
060:
061: @Override
062: /* (non-Javadoc)
063: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
064: */
065: public void interpret(Map<String, String> paramMap) {
066: this .paramMap = paramMap;
067: }
068:
069: @Override
070: /* (non-Javadoc)
071: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#execute(org.apache.commons.net.ftp.FTPClient)
072: */
073: public Object execute(FtpExecContext context) {
074: FTPClient ftp = context.getFtpclient();
075: logger.debug("mput:");
076: String mput = (String) paramMap.get(MPUT);
077: String path = "";
078: int slash = mput.indexOf("/");
079: try {
080: if (slash != -1) {
081: path = mput.substring(0, slash - 1);
082: }
083:
084: File folder = new File(path);
085: if (!folder.isAbsolute()) {
086: folder = new File(context.getLocalWorkDir(), path);
087: }
088: File[] files = folder.listFiles();
089:
090: if (files != null) {
091: String filePattern;
092: if (slash != -1) {
093: filePattern = mput.substring(slash + 1);
094: } else {
095: filePattern = mput;
096: }
097:
098: for (int i = 0; i < files.length; i++) {
099: if (FilePatternMatcher.matchPattern(files[i]
100: .getName(), filePattern, "Glob")) {
101: logger.debug(" Sending: "
102: + files[i].getName());
103: FileInputStream fis = new FileInputStream(
104: files[i]);
105: boolean result = ftp.storeFile(files[i]
106: .getName(), fis);
107: fis.close();
108: if (!result) {
109: throw new Exception("Unable to put file: '"
110: + files[i].getName());
111: }
112: }
113: }
114: }
115: } catch (Exception e) {
116: // e.printStackTrace();
117: return e;
118: }
119: return true;
120: }
121: }
|