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: Put.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.io.IOException;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.commons.net.ftp.FTPClient;
036:
037: /**
038: * Transfers the specified file to the FTP server.
039: *
040: * @author j.zhang
041: * @version 1.0.0
042: */
043: public class Put extends AbstractCommandExpression {
044:
045: protected final transient Log logger = LogFactory
046: .getLog(getClass());
047:
048: /**
049: * The file name stored into the FTP server; if omitted, the specified file name will
050: * be will be used to store file in the default remote FTP directory.
051: */
052: public static final String REMOTE_NAME = "remoteName";
053:
054: /**
055: * The path to the file in local file system.
056: */
057: public static final String PUT = "put";
058:
059: /**
060: * The Map of parameters.
061: */
062: private Map paramMap = new HashMap<String, String>();
063:
064: @Override
065: /* (non-Javadoc)
066: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#interpret(java.util.Map)
067: */
068: public void interpret(Map<String, String> paramMap) {
069: this .paramMap = paramMap;
070: }
071:
072: @Override
073: /* (non-Javadoc)
074: * @see com.bostechcorp.cbesb.runtime.ftp.AbstractCommandExpression#execute(org.apache.commons.net.ftp.FTPClient)
075: */
076: public Object execute(FtpExecContext context) {
077: FTPClient ftp = context.getFtpclient();
078: logger.debug("put:");
079: String remoteName = (String) paramMap.get(REMOTE_NAME);
080: String put = (String) paramMap.get(PUT);
081: try {
082: File localFile = new File(put);
083: if (!localFile.isAbsolute()) {
084: localFile = new File(context.getLocalWorkDir(), put);
085: }
086: if (remoteName == null || "".equals(remoteName)) {
087: remoteName = localFile.getName();
088: }
089: logger.debug(" local file: "
090: + localFile.getCanonicalPath());
091: logger.debug(" remote file: " + remoteName);
092: if (!localFile.exists()) {
093: throw new Exception("Local file "
094: + localFile.getCanonicalPath()
095: + " does not exist.");
096: }
097: FileInputStream lfis = new FileInputStream(localFile);
098: boolean result = ftp.storeFile(remoteName, lfis);
099: lfis.close();
100: if (!result) {
101: throw new Exception("Unable to put file: '"
102: + localFile.getCanonicalPath() + "'");
103: }
104: } catch (IOException ioe) {
105: // ioe.printStackTrace();
106: return ioe;
107: } catch (Exception e) {
108: // e.printStackTrace();
109: return e;
110: }
111:
112: return true;
113: }
114: }
|