@@ -35,21 +35,21 @@ public class SftpFileOutput
35
35
private final String userInfo ;
36
36
private final String host ;
37
37
private final int port ;
38
- private final String workingFileScheme ;
39
38
private final String pathPrefix ;
40
39
private final String sequenceFormat ;
41
40
private final String fileNameExtension ;
42
41
43
42
private final int taskIndex ;
44
43
private int fileIndex = 0 ;
45
- private FileObject currentWorkingFile ;
46
- private OutputStream currentWorkingFileOutputStream ;
44
+ private FileObject currentFile ;
45
+ private OutputStream currentFileOutputStream ;
47
46
48
47
private StandardFileSystemManager initializeStandardFileSystemManager ()
49
48
{
50
- // TODO: change logging format: org.apache.commons.logging.Log
51
- // System.setProperty("org.apache.commons.logging.Log",
52
- // "org.apache.commons.logging.impl.NoOpLog");
49
+ if (logger .isDebugEnabled ()) {
50
+ // TODO: change logging format: org.apache.commons.logging.Log
51
+ System .setProperty ("org.apache.commons.logging.Log" , "org.apache.commons.logging.impl.NoOpLog" );
52
+ }
53
53
StandardFileSystemManager manager = new StandardFileSystemManager ();
54
54
try {
55
55
manager .init ();
@@ -100,7 +100,6 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
100
100
this .host = task .getHost ();
101
101
this .port = task .getPort ();
102
102
this .pathPrefix = task .getPathPrefix ();
103
- this .workingFileScheme = task .getWorkingFileScheme ();
104
103
this .sequenceFormat = task .getSequenceFormat ();
105
104
this .fileNameExtension = task .getFileNameExtension ();
106
105
this .taskIndex = taskIndex ;
@@ -109,32 +108,28 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
109
108
@ Override
110
109
public void nextFile ()
111
110
{
112
- closeCurrentWithUpload ();
111
+ closeCurrentFile ();
113
112
114
113
try {
115
- currentWorkingFile = newWorkingFile ( getWorkingFileUri (getOutputFilePath ()));
116
- currentWorkingFileOutputStream = currentWorkingFile .getContent ().getOutputStream ();
117
- logger .info ("new working file: {}" , currentWorkingFile .getPublicURIString ());
114
+ currentFile = newSftpFile ( getSftpFileUri (getOutputFilePath ()));
115
+ currentFileOutputStream = currentFile .getContent ().getOutputStream ();
116
+ logger .info ("new sftp file: {}" , currentFile .getPublicURIString ());
118
117
}
119
118
catch (FileSystemException e ) {
120
119
logger .error (e .getMessage ());
121
120
Throwables .propagate (e );
122
121
}
123
- catch (URISyntaxException e ) {
124
- logger .error (e .getMessage ());
125
- Throwables .propagate (e );
126
- }
127
122
}
128
123
129
124
@ Override
130
125
public void add (Buffer buffer )
131
126
{
132
- if (currentWorkingFile == null ) {
127
+ if (currentFile == null ) {
133
128
throw new IllegalStateException ("nextFile() must be called before poll()" );
134
129
}
135
130
136
131
try {
137
- currentWorkingFileOutputStream .write (buffer .array (), buffer .offset (), buffer .limit ());
132
+ currentFileOutputStream .write (buffer .array (), buffer .offset (), buffer .limit ());
138
133
}
139
134
catch (IOException e ) {
140
135
logger .error (e .getMessage ());
@@ -146,13 +141,13 @@ public void add(Buffer buffer)
146
141
@ Override
147
142
public void finish ()
148
143
{
149
- closeCurrentWithUpload ();
144
+ closeCurrentFile ();
150
145
}
151
146
152
147
@ Override
153
148
public void close ()
154
149
{
155
- closeCurrentWithUpload ();
150
+ closeCurrentFile ();
156
151
manager .close ();
157
152
}
158
153
@@ -167,69 +162,38 @@ public TaskReport commit()
167
162
return null ;
168
163
}
169
164
170
- private void closeCurrentWithUpload ()
165
+
166
+ private void closeCurrentFile ()
171
167
{
172
- try {
173
- closeCurrentWorkingFileContent ();
174
- uploadCurrentWorkingFileToSftp ();
175
- closeCurrentWorkingFile ();
168
+ if (currentFile == null ) {
169
+ return ;
176
170
}
177
- catch (URISyntaxException e ) {
178
- logger .error (e .getMessage ());
179
- Throwables .propagate (e );
171
+
172
+ try {
173
+ currentFileOutputStream .close ();
174
+ currentFile .getContent ().close ();
175
+ currentFile .close ();
180
176
}
181
177
catch (IOException e ) {
182
178
logger .error (e .getMessage ());
183
179
Throwables .propagate (e );
184
180
}
185
- fileIndex ++;
186
- currentWorkingFile = null ;
187
- }
188
-
189
- private void closeCurrentWorkingFileContent ()
190
- throws IOException
191
- {
192
- if (currentWorkingFile == null ) {
193
- return ;
181
+ finally {
182
+ fileIndex ++;
183
+ currentFile = null ;
184
+ currentFileOutputStream = null ;
194
185
}
195
- currentWorkingFileOutputStream .close ();
196
- currentWorkingFile .getContent ().close ();
197
186
}
198
187
199
- private void uploadCurrentWorkingFileToSftp ()
200
- throws FileSystemException , URISyntaxException
188
+ private URI getSftpFileUri (String remoteFilePath )
201
189
{
202
- if (currentWorkingFile == null ) {
203
- return ;
204
- }
205
-
206
- try (FileObject remoteSftpFile = newSftpFile (getSftpFileUri (getOutputFilePath ()))) {
207
- remoteSftpFile .copyFrom (currentWorkingFile , Selectors .SELECT_SELF );
208
- logger .info ("Upload: {}" , remoteSftpFile .getPublicURIString ());
190
+ try {
191
+ return new URI ("sftp" , userInfo , host , port , remoteFilePath , null , null );
209
192
}
210
- }
211
-
212
- private void closeCurrentWorkingFile ()
213
- throws FileSystemException
214
- {
215
- if (currentWorkingFile == null ) {
216
- return ;
193
+ catch (URISyntaxException e ) {
194
+ logger .error (e .getMessage ());
195
+ throw new RuntimeException (e );
217
196
}
218
-
219
- currentWorkingFile .close ();
220
- currentWorkingFile .delete ();
221
- }
222
-
223
- private URI getSftpFileUri (String remoteFilePath )
224
- throws URISyntaxException
225
- {
226
- return new URI ("sftp" , userInfo , host , port , remoteFilePath , null , null );
227
- }
228
-
229
- private URI getWorkingFileUri (String remoteFilePath )
230
- throws URISyntaxException
231
- {
232
- return new URI (workingFileScheme , null , remoteFilePath , null );
233
197
}
234
198
235
199
private String getOutputFilePath ()
@@ -242,12 +206,4 @@ private FileObject newSftpFile(URI sftpUri)
242
206
{
243
207
return manager .resolveFile (sftpUri .toString (), fsOptions );
244
208
}
245
-
246
- private FileObject newWorkingFile (URI workingFileUri )
247
- throws FileSystemException
248
- {
249
- FileObject workingFile = manager .resolveFile (workingFileUri );
250
- workingFile .createFile ();
251
- return workingFile ;
252
- }
253
209
}
0 commit comments