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