Package com.sos.VirtualFileSystem.local

Source Code of com.sos.VirtualFileSystem.local.SOSVfsLocalFile

/********************************************************* 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.local;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.zip.GZIPOutputStream;

import org.apache.log4j.Logger;

import com.sos.JSHelper.Basics.JSToolBox;
import com.sos.JSHelper.DataElements.JSDataElementDateTime;
import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.JSHelper.io.Files.JSFile;
import com.sos.VirtualFileSystem.FTP.SOSVfsFtpFile;
import com.sos.VirtualFileSystem.Interfaces.ISOSVfsFileTransfer;
import com.sos.VirtualFileSystem.Interfaces.ISOSVirtualFile;
import com.sos.i18n.annotation.I18NResourceBundle;

/**
* \class SOSVfsLocalFile
*
* \brief SOSVfsLocalFile -
*
* \details
*
* \section SOSVfsLocalFile.java_intro_sec Introduction
*
* \section SOSVfsLocalFile.java_samples Some Samples
*
* \code
*   .... code goes here ...
* \endcode
*
* <p style="text-align:center">
* <br />---------------------------------------------------------------------------
* <br /> APL/Software GmbH - Berlin
* <br />##### generated by ClaviusXPress (http://www.sos-berlin.com) #########
* <br />---------------------------------------------------------------------------
* </p>
* \author KB
* @version $Id: SOSVfsLocalFile.java 14789 2011-07-08 15:51:52Z sos $14.09.2010
* \see reference
*
* Created on 14.09.2010 17:36:43
*/

/**
* @author KB
*
*/
@I18NResourceBundle(baseName = "SOSVirtualFileSystem", defaultLocale = "en")
public class SOSVfsLocalFile extends JSFile implements ISOSVirtualFile {
  private Logger        logger        = Logger.getLogger(SOSVfsLocalFile.class);
  private ISOSVfsFileTransfer  objVFSHandler    = null;
  private InputStream      objInputStream    = null;
  private OutputStream    objOutputStream    = null;

  /**
   *
   */
  private static final long  serialVersionUID  = 7478704922673917684L;
  private final String    conClassName    = "SOSVfsLocalFile";

  public SOSVfsLocalFile(String pstrFileName) {
    super(pstrFileName);
  }

  /**
   * \brief FileExists
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public boolean FileExists() throws Exception {
    boolean flgResult = super.exists();
    return flgResult;
  }

  /**
   * \brief delete
   *
   * \details
   *
   * \return
   *
   */
  @Override
  public boolean delete() {
    super.delete();
    logger.debug(String.format("File %1$s deleted", strFileName));
    return true;
  }

  /**
   * \brief deleteFile
   *
   * \details
   *
   * \return
   *
   * @throws Exception
   */
  @Override
  public void deleteFile() {
    super.delete();

  }

  /**
   * \brief getFile
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public ISOSVirtualFile getFile() throws Exception {
    return this;
  }

  /**
   * \brief getFileAppendStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public OutputStream getFileAppendStream() {
    final String conMethodName = conClassName + "::getFileAppendStream";

    OutputStream objO = null;
    try {
      objO = new FileOutputStream(new File(strFileName), true);
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
      String strT = String.format("%1$s failed", conMethodName);
      logger.error(strT, e);
      throw new JobSchedulerException(strT, e);
    }
    return objO;
  }

  /**
   * \brief getFileInputStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public InputStream getFileInputStream() {
    final String conMethodName = conClassName + "::getFileInputStream";

    InputStream objO = null;
    try {
      if (objInputStream == null) {
        objInputStream = new FileInputStream(new File(strFileName));
      }
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
      String strT = String.format("%1$s failed", conMethodName);
      logger.error(strT, e);
      throw new JobSchedulerException(strT, e);
    }
    return objInputStream;
  }

  /**
   * \brief getFileOutputStream
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public OutputStream getFileOutputStream() {
    final String conMethodName = conClassName + "::getFileOutputStream";

    // OutputStream objO = null;
    try {
      if (objOutputStream == null) {
        objOutputStream = new FileOutputStream(new File(strFileName), false);
      }
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
      String strT = String.format("%1$s failed", conMethodName);
      logger.error(strT, e);
      throw new JobSchedulerException(strT, e);
    }
    return objOutputStream;
  }

  /**
   * \brief getFilePermissions
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public Integer getFilePermissions() throws Exception {

    return 0;
  }

  /**
   * \brief getFileSize
   *
   * \details
   *
   * \return
   *
   * @return
   * @throws Exception
   */
  @Override
  public long getFileSize() {
    return super.length();
  }

  /**
   * \brief getHandler
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public ISOSVfsFileTransfer getHandler() {
    return this.objVFSHandler;
  }

  /**
   * \brief getModificationTime
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getModificationTime() {
    Date dteModificationTime = new Date(super.lastModified());
    String strMod = new JSDataElementDateTime(dteModificationTime).FormattedValue();
    return strMod;
  }

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

    return super.getAbsolutePath();
  }

  /**
   * \brief getParent
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getParentVfs() {
    String strT = super.getParent();
    return strT;
  }

  /**
   * \brief getParentFile
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public ISOSVirtualFile getParentVfsFile() {
    File strP = super.getParentFile();
    ISOSVirtualFile objF = new SOSVfsFtpFile(strP.getAbsolutePath());
    objF.setHandler(getHandler());
    return objF;
  }

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

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

  /**
   * \brief notExists
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public boolean notExists() {
    boolean flgResult = super.exists() == false;
    return flgResult;
  }

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

  /**
   * \brief putFile
   *
   * \details
   *
   * \return
   *
   * @param strFileName
   * @throws Exception
   */
  @Override
  public void putFile(String pstrFileName) throws Exception {
    JSToolBox.notImplemented();
  }

  /**
   * \brief rename
   *
   * \details
   *
   * \return
   *
   * @param pstrNewFileName
   */
  @Override
  public void rename(String pstrNewFileName) {
    super.renameTo(new File(pstrNewFileName));
    logger.info(String.format("File %1$s renamed to %2$s ", strFileName, pstrNewFileName));
  }

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

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

  public String MakeZIPFile(final String pstrZipFileNameExtension) {
    // File fleTargetFile = new File(this.fleFile.getAbsolutePath() + pstrZipFileNameExtension);
    // String strTargetFileName = fleTargetFile.getName();
    String strSourceTransferName = "";
    File fleSourceTransferFile = null;
    try {
      fleSourceTransferFile = File.createTempFile("sos", pstrZipFileNameExtension);
      // fleSourceTransferFile.deleteOnExit();
      strSourceTransferName = fleSourceTransferFile.getAbsolutePath();
    }
    catch (Exception e) {
      throw new JobSchedulerException("createTempFile returns with an exception", e);
    }
    try {
      logger.info("start compress file");
      this.compressFile(fleSourceTransferFile);
    }
    catch (Exception e) {
      e.printStackTrace(System.err);
      logger.error(e.getLocalizedMessage(), new JobSchedulerException("SOSGZip returns an exception", e));
      // throw new JobSchedulerException("SOSGZip returns an exception", e);
    }
    return strSourceTransferName;
  }

  public void compressFile(File outputFile) throws Exception {

    BufferedInputStream in = null;
    GZIPOutputStream out = null;

    try {
      in = new BufferedInputStream(new FileInputStream(this.fleFile));
      out = new GZIPOutputStream(new FileOutputStream(outputFile));
      // TODO �ber Option steuern
      byte buffer[] = new byte[60000];
      int bytesRead;
      while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
      }
    }
    catch (Exception e) {
      throw new JobSchedulerException("GZip failed", e);
    }
    finally {
      try {
        if (in != null)
          in.close();
        if (out != null)
          out.close();
      }
      catch (Exception e) {
      }
    }
  }

  @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 (objInputStream != null) {
        objInputStream.close();
      }
    }
    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 {
      this.getFileOutputStream().flush();
      this.getFileOutputStream().close();
    }
    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 {
      lngBytesRed = this.getFileInputStream().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 {
      lngBytesRed = this.getFileInputStream().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 {
      this.getFileOutputStream().write(bteBuffer, intOffset, intLength);
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new JobSchedulerException(String.format("%1$s failed", conMethodName), 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 String2File(String pstrContent) {

    try {
      OutputStream objOS = this.getFileOutputStream();
      objOS.write(pstrContent.getBytes());
      objOS.close();
    }
    catch (IOException e) {
      throw new JobSchedulerException("String2File end with exception", e);
    }

  }

  @Override
  public String File2String() {
    InputStream objFI = this.getFileInputStream();
    if (objFI == null) {
      throw new JobSchedulerException("can't get InputStream");
    }
    StringBuffer strB = new StringBuffer((int) this.getFileSize());
    int lngBufferSize = 1024;
    byte[] buffer = new byte[lngBufferSize];
    int intBytesTransferred;
    try {
      while ((intBytesTransferred = objFI.read(buffer)) != -1) {
        strB.append(new String(buffer).substring(0, intBytesTransferred));
      }
      objFI.close();
    }
    catch (Exception e) {
      throw new JobSchedulerException("File2String failed", e);
    }
    return strB.toString();
  }

  @Override
  public void putFile(ISOSVirtualFile pobjVirtualFile) throws Exception {
    boolean flgClosingDone = false;
    long lngTotalBytesTransferred = 0;
    try {
      int lngBufferSize = 4096; // objOptions.BufferSize.value();
      byte[] buffer = new byte[lngBufferSize];
      int intBytesTransferred;
      synchronized (this) {
        while ((intBytesTransferred = pobjVirtualFile.read(buffer)) != -1) {
          try {
            this.write(buffer, 0, intBytesTransferred);
          }
          catch (JobSchedulerException e) {
            break;
          }
          // TODO in case of wrong outputbuffer tha handling of the error must be improved
          lngTotalBytesTransferred += intBytesTransferred;
        }
      }
      pobjVirtualFile.closeInput();
      this.closeOutput();
      flgClosingDone = true;
    }
    catch (Exception e) {
      e.printStackTrace(System.err);
      throw new JobSchedulerException("transfer aborted with an exception", e);
    }
    finally {
      if (flgClosingDone == false) {
        pobjVirtualFile.closeInput();
        this.closeOutput();
        flgClosingDone = true;
      }
    }
  }

}
TOP

Related Classes of com.sos.VirtualFileSystem.local.SOSVfsLocalFile

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.