View Javadoc

1   /*
2    * $Id: SftpFileArchiveInputStream.java 21125 2011-01-26 21:21:10Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.sftp;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.mule.util.FileUtils;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  
22  /**
23   * Ensures that the file is moved to the archiveFile folder after a successful
24   * consumption of the file
25   * 
26   * @author Magnus Larsson
27   */
28  public class SftpFileArchiveInputStream extends FileInputStream implements ErrorOccurredDecorator
29  {
30      /**
31       * logger used by this class
32       */
33      private static final Log logger = LogFactory.getLog(SftpFileArchiveInputStream.class);
34  
35      private File file;
36      private File archiveFile;
37      private boolean errorOccured = false;
38  
39      // Log every 10 000 000 bytes read at debug-level
40      // Good if really large files are transferred and you tend to get nervous by not
41      // seeing any progress in the logfile...
42      private static final int LOG_BYTE_INTERVAL = 10000000;
43      private long bytesRead = 0;
44      private long nextLevelToLogBytesRead = LOG_BYTE_INTERVAL;
45  
46      public SftpFileArchiveInputStream(File file) throws FileNotFoundException
47      {
48          super(file);
49  
50          this.file = file;
51          this.archiveFile = null;
52      }
53  
54      public SftpFileArchiveInputStream(File file, File archiveFile) throws FileNotFoundException
55      {
56          super(file);
57  
58          this.file = file;
59          this.archiveFile = archiveFile;
60      }
61  
62      @Override
63      public int read() throws IOException
64      {
65          logReadBytes(1);
66          return super.read();
67      }
68  
69      @Override
70      public int read(byte[] b, int off, int len) throws IOException
71      {
72          logReadBytes(len);
73          return super.read(b, off, len);
74      }
75  
76      @Override
77      public int read(byte[] b) throws IOException
78      {
79          logReadBytes(b.length);
80          return super.read(b);
81      }
82  
83      public void close() throws IOException
84      {
85          if (logger.isDebugEnabled())
86          {
87              logger.debug("Closing the stream for the file " + file);
88          }
89          super.close();
90  
91          if (!errorOccured && archiveFile != null)
92          {
93              if (logger.isInfoEnabled())
94              {
95                  logger.info("Move archiveTmpSendingFile (" + file + ") to archiveFolder (" + archiveFile
96                              + ")...");
97              }
98              FileUtils.moveFile(file, archiveFile);
99          }
100     }
101 
102     public void setErrorOccurred()
103     {
104         if (logger.isDebugEnabled()) logger.debug("setErrorOccurred() called");
105         this.errorOccured = true;
106     }
107 
108     private void logReadBytes(int newBytesRead)
109     {
110         if (!logger.isDebugEnabled()) return;
111 
112         this.bytesRead += newBytesRead;
113         if (this.bytesRead >= nextLevelToLogBytesRead)
114         {
115             logger.debug("Read " + this.bytesRead + " bytes and couting...");
116             nextLevelToLogBytesRead += LOG_BYTE_INTERVAL;
117         }
118     }
119 
120 }