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: RenameLocal.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: /**
036: * Rename the specified local file.
037: *
038: * @author j.zhang
039: * @version 1.0.0
040: */
041: public class RenameLocal extends AbstractCommandExpression {
042:
043: protected final transient Log logger = LogFactory
044: .getLog(getClass());
045:
046: /**
047: * The name to the local file to be renamed.
048: */
049: public static final String RENAME_LOCAL = "renameLocal";
050:
051: /**
052: * The new name of the local file.
053: */
054: public static final String TO_NAME = "toName";
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("renameLocal:");
076: String renameLocal = (String) paramMap.get(RENAME_LOCAL);
077: String toName = (String) paramMap.get(TO_NAME);
078: logger.debug(" Original Name: " + renameLocal);
079: logger.debug(" New Name: " + toName);
080: File renameLocalFile = new File(renameLocal);
081: if (!renameLocalFile.isAbsolute()) {
082: renameLocalFile = new File(context.getLocalWorkDir(),
083: renameLocal);
084: }
085: try {
086: if (!renameLocalFile.getAbsoluteFile().exists()) {
087: throw new Exception("Local file '"
088: + renameLocalFile.getAbsolutePath()
089: + "' does not exist.");
090: }
091: if (toName == null || toName.equals("")) {
092: throw new Exception("No 'toName' value was specified.");
093: }
094: if (!renameLocalFile.getAbsoluteFile().renameTo(
095: new File(context.getLocalWorkDir(), toName)
096: .getAbsoluteFile())) {
097: throw new Exception("Unable to rename local file '"
098: + renameLocal + "' to '" + toName + "'");
099: }
100: } catch (Exception e) {
101: // e.printStackTrace();
102: return e;
103: }
104: return true;
105: }
106: }
|