Package com.sos.VirtualFileSystem.FTP

Source Code of com.sos.VirtualFileSystem.FTP.SOSVfsFtpFile

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.VirtualFileSystem.FTP;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.VirtualFileSystem.Interfaces.ISOSVfsFileTransfer;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFile;
import com.sos.VirtualFileSystem.common.SOSVfsCommonFile;
import com.sos.i18n.annotation.I18NResourceBundle;

/**
* @author KB
*
*/
@I18NResourceBundle(baseName = "SOSVirtualFileSystem", defaultLocale = "en")
public class SOSVfsFtpFile extends SOSVfsCommonFile {
  private final String    conClassName  = "SOSVfsFtpFile";
  private Logger        logger      = Logger.getLogger(SOSVfsFtpFile.class);
  // private SOSVfsFtp objVfsHandler = null;
//  private ISOSVfsFileTransfer  objVfsHandler  = null;
//  private InputStream      objInputStream  = null;
//  private OutputStream    objOutputStream  = null;
  private String        strFileName    = EMPTY_STRING;

  public SOSVfsFtpFile(final String pstrFileName) {
    final String conMethodName = conClassName + "::SOSVfsFtpFile";
    String strF = pstrFileName;
    if (objVFSHandler != null) {
      String strCurrDir = objVFSHandler.DoPWD();
      logger.debug(String.format("%1$s: currDir = %2$s", conMethodName, strCurrDir));
      if (strF.startsWith("./") == true) {
        strF = strF.replace("./", strCurrDir + "/");
      }
    }
    this.strFileName = strF;
  }

  /**
   * \brief FileExists
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public boolean FileExists() {
    final String conMethodName = conClassName + "::FileExists";
    boolean flgResult = false;
    logger.debug(String.format("%1$s: SourceFileName = %2$s", conMethodName, strFileName));
    // TODO hier wird im aktuellen Verzeichnis gesucht. geht schief, wenn die datei im Subfolder ist
    // TODO Der Dateiname darf hier nur aus dem Namen der Datei bestehen. Ist die Datei in einem Subfolder, dann mu� der Subfolder
    // ebenfalls Namensbestandteil sein.
    // TODO im Moment kommt der Dateiname mal mit und mal ohne Pfadname hier an.
    // TODO Methoden bauen: GibDateiNameOhnePFad und GibDateiNameMitPfad
    if (1 == 1) {
      File fleF = new File(AdjustRelativePathName(strFileName));
      String strP = fleF.getParent();
      if (strP == null) {
        strP = ".";
      }
      strP = ".";
      String strN = fleF.getName();
      if (objVFSHandler.getFileSize(strFileName) >= 0) {
        flgResult = true;
      }
      /**
       * inperformant. the approach with size is much more better and faster.
       */
//      Vector<String> vecTargetFileNamesList = objVFSHandler.nList(strP);
//      flgResult = vecTargetFileNamesList.contains(strFileName);
//      if (flgResult == false) {
//        flgResult = vecTargetFileNamesList.contains(strN);       
//      }
    }
    else {
      Vector<String> vecTargetFileNamesList = objVFSHandler.nList(".");
      String strCurrDir = objVFSHandler.DoPWD();
      logger.debug(String.format("%1$s: currDir = %2$s", conMethodName, strCurrDir));
      String strT = strFileName;
      if (strT.startsWith(strCurrDir) == false) {
        strT = strCurrDir + "/" + strFileName;
      }
      flgResult = vecTargetFileNamesList.contains(strT);
      if (flgResult == false) { // Evtl. Windows?
        flgResult = vecTargetFileNamesList.contains(strCurrDir + "\\" + strFileName);
      }
    }
    logger.debug(String.format("%1$s: flgResult = %2$s for File %3$s", conMethodName, flgResult, strFileName));
    return flgResult;
  }

  /**
   * \brief delete
   *
   * \details
   *
   * \return
   *
   */
  @Override
  public boolean delete() {
    final String conMethodName = conClassName + "::delete";
    try {
      objVFSHandler.delete(strFileName);
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return true;
  }

  /**
   * \brief getFileAppendStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public OutputStream getFileAppendStream() {
    final String conMethodName = conClassName + "::getFileAppendStream";
    OutputStream objO = null;
    try {
      strFileName = AdjustRelativePathName(strFileName);
      objO = objVFSHandler.getAppendFileStream(strFileName);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return objO;
  }

  /**
   * \brief getFileInputStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public InputStream getFileInputStream() {
    final String conMethodName = conClassName + "::getFileInputStream";
    try {
      if (objInputStream == null) {
        strFileName = AdjustRelativePathName(strFileName);
        objInputStream = objVFSHandler.getInputStream(strFileName);
        if (objInputStream == null) {
          objVFSHandler.openInputFile(strFileName);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return objInputStream;
  }

  /**
   * \brief getFileOutputStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public OutputStream getFileOutputStream() {
    final String conMethodName = conClassName + "::getFileOutputStream";
    try {
      if (objOutputStream == null) {
        strFileName = AdjustRelativePathName(strFileName);
        objOutputStream = objVFSHandler.getOutputStream(strFileName);
        if (objOutputStream == null) {
          objVFSHandler.openOutputFile(strFileName);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return objOutputStream;
  }

  private String AdjustRelativePathName (final String pstrPathName) {
    String strT = pstrPathName;
   
    if (pstrPathName.startsWith("./") || pstrPathName.startsWith(".\\")) {
      String strPath = objVFSHandler.DoPWD() + "/";
      strT = new File(pstrPathName).getName();
      strT = strPath + strT;
      logger.debug(String.format("Path adjusted from '%1$s' to '%2$s'", pstrPathName, strT));
    }
   
    return strT;
  }
 
  /**
   * \brief getFilePermissions
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public Integer getFilePermissions() throws Exception {
    // TODO Auto-generated method stub
    return 0;
  }

  /**
   * \brief getFileSize
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public long getFileSize() {
    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::getFileSize";
    long lngFileSize = -1;
    try {
      lngFileSize = objVFSHandler.getFileSize(strFileName);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException("getFileSize failed", e);
    }
    return lngFileSize;
  }

  /**
   * \brief getName
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getName() {
    return strFileName;
  }

  /**
   * \brief getParent
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getParentVfs() {
    return null;
  }

  /**
   * \brief getParentFile
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public ISOSVirtualFile getParentVfsFile() {
    // TODO Auto-generated method stub
    return null;
  }

  /**
   * \brief isDirectory
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public boolean isDirectory() {
    boolean flgResult = objVFSHandler.isDirectory(strFileName);
    return flgResult;
  }

  /**
   * \brief isEmptyFile
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public boolean isEmptyFile() {
    boolean flgResult = (this.getFileSize() <= 0);
    return flgResult;
  }

  /**
   * \brief notExists
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public boolean notExists() {
    final String conMethodName = conClassName + "::notExists";
    boolean flgResult = false;
    try {
      flgResult = (this.FileExists() == false);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return flgResult;
  }

  /**
   * \brief putFile
   *
   * \details
   *
   * \return
   *
   * @param fleFile
   * @throws Exception
   */
  @Override
  public void putFile(File fleFile) {
    notImplemented();
  }

  /**
   * \brief putFile
   *
   * \details
   *
   * \return
   *
   * @param strFileName
   * @throws Exception
   */
  @Override
  public void putFile(@SuppressWarnings("hiding") String strFileName) {
    notImplemented();
  }

  /**
   * \brief rename
   *
   * \details
   *
   * \return
   *
   * @param pstrNewFileName
   */
  @Override
  public void rename(String pstrNewFileName) {
    objVFSHandler.rename(strFileName, pstrNewFileName);
  }

  /**
   * \brief setFilePermissions
   *
   * \details
   *
   * \return
   *
   * @param pintNewPermission
   * @throws Exception
   */
  @Override
  public void setFilePermissions(Integer pintNewPermission) {
    notImplemented();
  }

  /**
   * \brief setHandler
   *
   * \details
   *
   * \return
   *
   * @param pobjVFSHandler
   */
  @Override
  public void setHandler(ISOSVfsFileTransfer pobjVFSHandler) {
//    this.objVFSHandler = (SOSVfsFtp) pobjVFSHandler;
    this.objVFSHandler =  pobjVFSHandler;
  }

  @Override
  public String getModificationTime() {
    final String conMethodName = conClassName + "::getModificationTime";
    String strT = "";
    try {
      strT = objVFSHandler.getModificationTime(strFileName);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return strT;
  }

  @Override
  public String MakeZIPFile(String pstrZipFileNameExtension) {
    logger.info(String.format("MakeZIPFile not possible and not implemented."));
    return this.strFileName;
  }

  @Override
  public void close() {
    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::close";
    if (objOutputStream != null) {
      this.closeOutput();
    }
    else {
      if (objInputStream != null) {
        this.closeInput();
      }
    }
  }

  @Override
  public void closeInput() {
    final String conMethodName = conClassName + "::closeInput";
    try {
      if (this.getFileInputStream() != null) {
        this.getFileInputStream().close();
      }
      try {
        objVFSHandler.CompletePendingCommand();
      }
      catch (Exception e) {
        // e.printStackTrace();
      }
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    finally {
      objInputStream = null;
    }
  }

  @Override
  public void closeOutput() {
    final String conMethodName = conClassName + "::closeOutput";
    try {
      OutputStream objO = this.getFileOutputStream();
      if (objO != null) {
        objO.flush();
        objO.close();
        objVFSHandler.CompletePendingCommand();
        if (objVFSHandler.isNegativeCommandCompletion()) {
          throw new JobSchedulerException("..error occurred during transfer on the data-target for file [" + strFileName + "]: "
              + objVFSHandler.getReplyString());
        }
      }
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    finally {
      objOutputStream = null;
    }
  }

  @Override
  public void flush() {
    final String conMethodName = conClassName + "::flush";
    try {
      this.getFileOutputStream().flush();
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
  }

  @Override
  public int read(byte[] bteBuffer) {
    final String conMethodName = conClassName + "::read";
    int lngBytesRed = 0;
    try {
      InputStream objI = this.getFileInputStream();
      if (objI != null) {
        lngBytesRed = objI.read(bteBuffer);
      }
      else {
        lngBytesRed = objVFSHandler.read(bteBuffer);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return lngBytesRed;
  }

  @Override
  public int read(byte[] bteBuffer, int intOffset, int intLength) {
    final String conMethodName = conClassName + "::read";
    int lngBytesRed = 0;
    try {
      InputStream objI = this.getFileInputStream();
      if (objI != null) {
        lngBytesRed = objI.read(bteBuffer, intOffset, intLength);
      }
      else {
        lngBytesRed = objVFSHandler.read(bteBuffer, intOffset, intLength);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
    return lngBytesRed;
  }

  @Override
  public void write(byte[] bteBuffer, int intOffset, int intLength) {
    final String conMethodName = conClassName + "::write";
    try {
      if (this.getFileOutputStream() == null) {
        throw new JobSchedulerException(String.format("%1$s failed, can not get OutputStream for file %2$s", conMethodName, strFileName));
      }
      this.getFileOutputStream().write(bteBuffer, intOffset, intLength);
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed for file %2$s", conMethodName, strFileName), e);
    }
  }

  @Override
  public void write(byte[] bteBuffer) {
    final String conMethodName = conClassName + "::write";
    try {
      this.getFileOutputStream().write(bteBuffer);
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), e);
    }
  }

  @Override
  public void putFile(ISOSVirtualFile pobjVirtualFile) throws Exception {
    // TODO Auto-generated method stub
    notImplemented();
  }

}
TOP

Related Classes of com.sos.VirtualFileSystem.FTP.SOSVfsFtpFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.