01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU General Public License as published by
09: * the Free Software Foundation; either version 2 of the License, or
10: * (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
20: * USA
21: *
22: *
23: * $Id: DeleteLocalFile.java 11093 2007-12-21 18:22:22Z mpreston $
24: */
25: package com.bostechcorp.cbesb.runtime.ftp.interpreter;
26:
27: import java.io.File;
28: import java.util.HashMap;
29: import java.util.Map;
30:
31: import org.apache.commons.logging.Log;
32: import org.apache.commons.logging.LogFactory;
33: import org.apache.commons.net.ftp.FTPClient;
34:
35: /**
36: * Deletes the specified file from the local file system.
37: *
38: * @author j.zhang
39: * @version 1.0.0
40: */
41: public class DeleteLocalFile extends AbstractCommandExpression {
42:
43: protected final transient Log logger = LogFactory
44: .getLog(getClass());
45:
46: /**
47: * The path to the file to be deleted.
48: */
49: public static final String DELETE_LOCAL_FILE = "deleteLocalFile";
50:
51: /**
52: * The Map of parameters.
53: */
54: private Map paramMap = new HashMap<String, String>();
55:
56: @Override
57: /* (non-Javadoc)
58: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
59: */
60: public void interpret(Map<String, String> paramMap) {
61: this .paramMap = paramMap;
62: }
63:
64: @Override
65: /* (non-Javadoc)
66: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#execute(org.apache.commons.net.ftp.FTPClient)
67: */
68: public Object execute(FtpExecContext context) {
69: FTPClient ftp = context.getFtpclient();
70: logger.debug("deleteLocalFile:");
71: String deleteLocalFile = (String) paramMap
72: .get(DELETE_LOCAL_FILE);
73: File deleteFile = new File(deleteLocalFile);
74: if (!deleteFile.isAbsolute()) {
75: deleteFile = new File(context.getLocalWorkDir(),
76: deleteLocalFile);
77: }
78: logger.debug(" File: " + deleteFile.getAbsolutePath());
79: try {
80: if (deleteFile.getAbsoluteFile().exists()) {
81: boolean deleteFlag = deleteFile.getAbsoluteFile()
82: .delete();
83: if (!deleteFlag) {
84: throw new Exception("Local file '"
85: + deleteFile.getCanonicalPath()
86: + "' could not be deleted.");
87: }
88: }
89: } catch (Exception e) {
90: // e.printStackTrace();
91: return e;
92: }
93: return true;
94: }
95: }
|