1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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.api.endpoint.ImmutableEndpoint; |
12 | |
|
13 | |
import java.io.BufferedInputStream; |
14 | |
import java.io.IOException; |
15 | |
import java.io.InputStream; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
public class SftpInputStream extends BufferedInputStream implements ErrorOccurredDecorator |
22 | |
{ |
23 | 0 | private final Log logger = LogFactory.getLog(getClass()); |
24 | |
|
25 | |
private SftpClient client; |
26 | 0 | private boolean autoDelete = true; |
27 | |
private String fileName; |
28 | |
|
29 | |
public String getFileName() |
30 | |
{ |
31 | 0 | return fileName; |
32 | |
} |
33 | |
|
34 | |
public void setFileName(String fileName) |
35 | |
{ |
36 | 0 | this.fileName = fileName; |
37 | 0 | } |
38 | |
|
39 | 0 | private boolean errorOccured = false; |
40 | |
private ImmutableEndpoint endpoint; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private static final int LOG_BYTE_INTERVAL = 10000000; |
46 | 0 | private long bytesRead = 0; |
47 | 0 | private long nextLevelToLogBytesRead = LOG_BYTE_INTERVAL; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public SftpInputStream(SftpClient client, |
62 | |
InputStream is, |
63 | |
String fileName, |
64 | |
boolean autoDelete, |
65 | |
ImmutableEndpoint endpoint) throws Exception |
66 | |
{ |
67 | 0 | super(is); |
68 | |
|
69 | 0 | this.client = client; |
70 | 0 | this.fileName = fileName; |
71 | 0 | this.autoDelete = autoDelete; |
72 | 0 | this.endpoint = endpoint; |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
public synchronized int read() throws IOException |
77 | |
{ |
78 | 0 | logReadBytes(1); |
79 | 0 | return super.read(); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public synchronized int read(byte[] b, int off, int len) throws IOException |
84 | |
{ |
85 | 0 | logReadBytes(len); |
86 | 0 | return super.read(b, off, len); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public int read(byte[] b) throws IOException |
91 | |
{ |
92 | 0 | logReadBytes(b.length); |
93 | 0 | return super.read(b); |
94 | |
} |
95 | |
|
96 | |
public void close() throws IOException |
97 | |
{ |
98 | 0 | if (logger.isDebugEnabled()) |
99 | |
{ |
100 | 0 | logger.debug("Closing the stream for the file " + fileName); |
101 | |
} |
102 | |
try |
103 | |
{ |
104 | 0 | super.close(); |
105 | |
|
106 | 0 | if (autoDelete && !errorOccured) |
107 | |
{ |
108 | 0 | client.deleteFile(fileName); |
109 | |
} |
110 | |
} |
111 | 0 | catch (IOException e) |
112 | |
{ |
113 | 0 | logger.error("Error occured while closing file " + fileName, e); |
114 | 0 | throw e; |
115 | |
} |
116 | |
finally |
117 | |
{ |
118 | |
|
119 | |
|
120 | 0 | try |
121 | |
{ |
122 | 0 | ((SftpConnector) endpoint.getConnector()).releaseClient(endpoint, client); |
123 | |
} |
124 | 0 | catch (Exception e) |
125 | |
{ |
126 | 0 | logger.error(e.getMessage(), e); |
127 | 0 | } |
128 | 0 | } |
129 | 0 | } |
130 | |
|
131 | |
public void setErrorOccurred() |
132 | |
{ |
133 | 0 | if (logger.isDebugEnabled()) logger.debug("setErrorOccurred() called"); |
134 | 0 | this.errorOccured = true; |
135 | 0 | } |
136 | |
|
137 | |
@Override |
138 | |
public String toString() |
139 | |
{ |
140 | 0 | return "SftpInputStream{" + "fileName='" + fileName + '\'' + " from endpoint=" |
141 | |
+ endpoint.getEndpointURI() + '}'; |
142 | |
} |
143 | |
|
144 | |
private void logReadBytes(int newBytesRead) |
145 | |
{ |
146 | 0 | if (!logger.isDebugEnabled()) return; |
147 | |
|
148 | 0 | this.bytesRead += newBytesRead; |
149 | 0 | if (this.bytesRead >= nextLevelToLogBytesRead) |
150 | |
{ |
151 | 0 | logger.debug("Read " + this.bytesRead + " bytes and couting..."); |
152 | 0 | nextLevelToLogBytesRead += LOG_BYTE_INTERVAL; |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | |
} |