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: MDeleteFiles.java 11093 2007-12-21 18:22:22Z mpreston $
024: */
025: package com.bostechcorp.cbesb.runtime.ftp.interpreter;
026:
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.commons.net.ftp.FTPClient;
034: import org.apache.commons.net.ftp.FTPFile;
035:
036: import com.bostechcorp.cbesb.runtime.file.FilePatternMatcher;
037:
038: /**
039: * Deletes the specified file(s) from the FTP server based on the glob expression file
040: * pattern.
041: *
042: * @author j.zhang
043: * @version 1.0.0
044: */
045: public class MDeleteFiles extends AbstractCommandExpression {
046:
047: protected final transient Log logger = LogFactory
048: .getLog(getClass());
049:
050: /**
051: * The glob expression file pattern specified the file(s) in the FTP server to be
052: * deleted.
053: */
054: public static final String M_DELETE_FILES = "mDeleteFiles";
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("mDeleteFiles:");
076: String mDeleteFiles = (String) paramMap.get(M_DELETE_FILES);
077: logger.debug(" pattern: " + mDeleteFiles);
078: int slash = mDeleteFiles.lastIndexOf("/");
079: String path = "";
080: try {
081: if (slash != -1) {
082: path = mDeleteFiles.substring(0, slash);
083: }
084: FTPFile[] files = null;
085: if (!"".equals(path)) {
086: files = ftp.listFiles(path);
087: } else {
088: files = ftp.listFiles();
089: }
090: if (files != null) {
091: String filePattern;
092: if (slash != -1) {
093: filePattern = mDeleteFiles.substring(slash + 1);
094: } else {
095: filePattern = mDeleteFiles;
096: }
097: for (int i = 0; i < files.length; i++) {
098: if (files[i].isDirectory()) {
099: continue;
100: }
101: if (FilePatternMatcher.matchPattern(files[i]
102: .getName(), filePattern, "Glob")) {
103: logger.debug(" Deleting File: " + path
104: + files[i].getName());
105: boolean deleteFlag = ftp.deleteFile(path
106: + files[i].getName());
107: if (!deleteFlag) {
108: throw new Exception("File: '" + path
109: + files[i].getName()
110: + "' could not be deleted.");
111: }
112: }
113: }
114: }
115: } catch (IOException ioe) {
116: // ioe.printStackTrace();
117: return ioe;
118: } catch (Exception e) {
119: // e.printStackTrace();
120: return e;
121: }
122: return true;
123: }
124: }
|