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: MDeleteLocalFiles.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.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:
035: import com.bostechcorp.cbesb.runtime.file.FileOperator;
036: import com.bostechcorp.cbesb.runtime.file.FilePatternMatcher;
037:
038: /**
039: * Deletes the specified file(s) from the local file system based on the regular
040: * expression file pattern.
041: *
042: * @author j.zhang
043: * @version 1.0.0
044: */
045: public class MDeleteLocalFiles extends AbstractCommandExpression {
046:
047: protected final transient Log logger = LogFactory
048: .getLog(getClass());
049:
050: /**
051: * The regular expression file pattern specified the file(s) in the local file system
052: * to be deleted.
053: */
054: public static final String M_DELETE_LOCAL_FILES = "mDeleteLocalFiles";
055:
056: /**
057: * The Map of parameters.
058: */
059: private Map paramMap = new HashMap<String, String>();
060:
061: /* (non-Javadoc)
062: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
063: */
064: @Override
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("mDeleteLocalFiles:");
076: String mDeleteLocalFiles = (String) paramMap
077: .get(M_DELETE_LOCAL_FILES);
078: String path = "";
079: int slash = mDeleteLocalFiles.lastIndexOf("/");
080: if (slash == -1) {
081: slash = mDeleteLocalFiles.lastIndexOf("\\");
082: }
083: try {
084: if (slash != -1) {
085: path = mDeleteLocalFiles.substring(0, slash);
086: }
087: File dir = new File(path);
088: if (!dir.isAbsolute()) {
089: dir = new File(context.getLocalWorkDir(), path);
090: }
091: File[] files = FileOperator.scanDir(dir.getAbsolutePath());
092: if (files != null) {
093: String filePattern;
094: if (slash != -1) {
095: filePattern = mDeleteLocalFiles
096: .substring(slash + 1);
097: } else {
098: filePattern = mDeleteLocalFiles;
099: }
100: for (int i = 0; i < files.length; i++) {
101: if (FilePatternMatcher.matchPattern(files[i]
102: .getName(), filePattern, "Glob")) {
103: logger.debug(" Deleting local File: "
104: + files[i].getAbsolutePath());
105: boolean deleteFlag = files[i].getAbsoluteFile()
106: .delete();
107: if (!deleteFlag) {
108: throw new Exception(
109: "Cannot delete local file: '"
110: + files[i]
111: .getAbsolutePath()
112: + "'");
113: }
114: }
115: }
116: }
117: } catch (Exception e) {
118: // e.printStackTrace();
119: return e;
120: }
121: return true;
122: }
123: }
|