Skip to content

Commit c2d3273

Browse files
committed
Remove returning true at successful file operations
1 parent 4eaba82 commit c2d3273

File tree

8 files changed

+27
-73
lines changed

8 files changed

+27
-73
lines changed

component/src/main/java/io/siddhi/extension/execution/file/FileArchiveExtension.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
183183
File sourceFile = new File(uri);
184184
List<String> fileList = new ArrayList<>();
185185
generateFileList(uri, sourceFile, fileList, excludeSubdirectories);
186-
zip(uri, destinationDirUri, fileList);
186+
try {
187+
zip(uri, destinationDirUri, fileList);
188+
} catch (IOException e) {
189+
throw new SiddhiAppRuntimeException("IOException occurred when archiving " + uri, e);
190+
}
187191
} else {
188192
try {
189193
if (archiveType.compareToIgnoreCase(TAR_FILE_EXTENSION) == 0) {
@@ -196,9 +200,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
196200
throw new SiddhiAppRuntimeException("Exception occurred when archiving " + uri, e);
197201
}
198202
}
199-
Object[] data = {true};
200-
complexEventPopulater.populateComplexEvent(streamEvent, data);
201-
nextProcessor.process(streamEventChunk);
202203
}
203204
}
204205

@@ -299,7 +300,7 @@ public void stop() {
299300
*
300301
* @param zipFile output ZIP file location
301302
*/
302-
private void zip(String sourceFileUri, String zipFile, List<String> fileList) {
303+
private void zip(String sourceFileUri, String zipFile, List<String> fileList) throws IOException {
303304
byte[] buffer = new byte[1024];
304305
FileInputStream in = null;
305306
String filePath = null;
@@ -319,7 +320,6 @@ private void zip(String sourceFileUri, String zipFile, List<String> fileList) {
319320
}
320321
ZipEntry ze = new ZipEntry(file);
321322
zos.putNextEntry(ze);
322-
filePath = sourceFileUri + File.separator + file;
323323
in = new FileInputStream(sourceFileUri + File.separator + file);
324324
int len;
325325
while ((len = in.read(buffer)) > 0) {
@@ -328,27 +328,15 @@ private void zip(String sourceFileUri, String zipFile, List<String> fileList) {
328328
in.close();
329329
}
330330
zos.closeEntry();
331-
//remember close it
332-
zos.close();
333331
if (log.isDebugEnabled()) {
334332
log.debug("Output to Zip : " + zipFile + " is complete for folder/ file: " + sourceFileUri);
335333
}
336-
} catch (IOException e) {
337-
throw new SiddhiAppRuntimeException("IOException occurred when archiving " + sourceFileUri, e);
338-
} finally {
334+
} finally {
339335
if (in != null) {
340-
try {
341-
in.close();
342-
} catch (IOException e) {
343-
log.error("IO exception occurred when closing file input stream for file path: " + filePath);
344-
}
336+
in.close();
345337
}
346338
if (zos != null) {
347-
try {
348-
zos.close();
349-
} catch (IOException e) {
350-
log.error("IO exception occurred when closing file input stream for file path: " + filePath);
351-
}
339+
zos.close();
352340
}
353341
}
354342
}

component/src/main/java/io/siddhi/extension/execution/file/FileCopyExtension.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
175175
}
176176
}
177177
}
178-
Object[] data = {true};
179-
sendEvents(streamEvent, data, streamEventChunk);
180178
} catch (FileSystemException e) {
181179
throw new SiddhiAppRuntimeException("Exception occurred when getting the file type " +
182180
uri, e);
@@ -286,9 +284,4 @@ private void copyFileToDestination(FileObject sourceFileObject, String destinati
286284
sourceFileObject.getName().getPath(), e);
287285
}
288286
}
289-
290-
private void sendEvents(StreamEvent streamEvent, Object[] data, ComplexEventChunk<StreamEvent> streamEventChunk) {
291-
complexEventPopulater.populateComplexEvent(streamEvent, data);
292-
nextProcessor.process(streamEventChunk);
293-
}
294287
}

component/src/main/java/io/siddhi/extension/execution/file/FileCreateExtension.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
121121
try {
122122
vfsClientConnector.send(carbonMessage, vfsClientConnectorCallback, properties);
123123
vfsClientConnectorCallback.waitTillDone(WAIT_TILL_DONE, fileSourcePath);
124-
Object[] data = {true};
125-
sendEvents(streamEvent, data, streamEventChunk);
126124
} catch (ClientConnectorException e) {
127125
throw new SiddhiAppRuntimeException("Failure occurred in vfs-client while creating the file " +
128126
fileSourcePath, e);
@@ -189,9 +187,4 @@ public void start() {
189187
public void stop() {
190188

191189
}
192-
193-
private void sendEvents(StreamEvent streamEvent, Object[] data, ComplexEventChunk<StreamEvent> streamEventChunk) {
194-
complexEventPopulater.populateComplexEvent(streamEvent, data);
195-
nextProcessor.process(streamEventChunk);
196-
}
197190
}

component/src/main/java/io/siddhi/extension/execution/file/FileDeleteExtension.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
109109
try {
110110
vfsClientConnector.send(carbonMessage, vfsClientConnectorCallback, properties);
111111
vfsClientConnectorCallback.waitTillDone(WAIT_TILL_DONE, fileDeletePathUri);
112-
Object[] data = {true};
113-
sendEvents(streamEvent, data, streamEventChunk);
114112
} catch (ClientConnectorException e) {
115113
throw new SiddhiAppRuntimeException("Failure occurred in vfs-client while deleting the file " +
116114
fileDeletePathUri, e);
@@ -166,9 +164,4 @@ public void start() {
166164
public void stop() {
167165

168166
}
169-
170-
private void sendEvents(StreamEvent streamEvent, Object[] data, ComplexEventChunk<StreamEvent> streamEventChunk) {
171-
complexEventPopulater.populateComplexEvent(streamEvent, data);
172-
nextProcessor.process(streamEventChunk);
173-
}
174167
}

component/src/main/java/io/siddhi/extension/execution/file/FileMoveExtension.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
171171
}
172172
}
173173
}
174-
Object[] data = {true};
175-
sendEvents(streamEvent, data, streamEventChunk);
176174
} catch (FileSystemException e) {
177175
throw new SiddhiAppRuntimeException("Exception occurred when getting the file type " +
178176
uri, e);
@@ -279,9 +277,4 @@ private void moveFileToDestination(FileObject sourceFileObject, String destinati
279277
sourceFileObject.getName().getPath(), e);
280278
}
281279
}
282-
283-
private void sendEvents(StreamEvent streamEvent, Object[] data, ComplexEventChunk<StreamEvent> streamEventChunk) {
284-
complexEventPopulater.populateComplexEvent(streamEvent, data);
285-
nextProcessor.process(streamEventChunk);
286-
}
287280
}

component/src/main/java/io/siddhi/extension/execution/file/FileUnarchiveExtension.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@
9696
parameterNames = {"uri", "destination.dir.uri", "exclude.root.folder"}
9797
)
9898
},
99-
returnAttributes = {
100-
@ReturnAttribute(
101-
name = "isSuccess",
102-
description = "The success of the file decompress process.",
103-
type = DataType.BOOL
104-
)
105-
},
10699
examples = {
107100
@Example(
108101
syntax = "file:unarchive('/User/wso2/source/test.zip', '/User/wso2/destination')",
@@ -153,7 +146,7 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
153146
filePathUri);
154147
}
155148
}
156-
FileInputStream fis;
149+
FileInputStream fis = null;
157150
//buffer for read and write data to file
158151
byte[] buffer = new byte[BUFFER_SIZE];
159152
ZipInputStream zis = null;
@@ -184,10 +177,6 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
184177
}
185178
//close last ZipEntry
186179
zis.closeEntry();
187-
zis.close();
188-
fis.close();
189-
Object[] data = {true};
190-
sendEvents(streamEvent, data, streamEventChunk);
191180
} else if (sourceFileExtension.compareToIgnoreCase(Constant.TAR_FILE_EXTENSION) == 0) {
192181
try (TarArchiveInputStream fin = new TarArchiveInputStream(new FileInputStream(filePathUri))) {
193182
TarArchiveEntry entry;
@@ -212,12 +201,19 @@ protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processo
212201
try {
213202
zis.close();
214203
} catch (IOException e) {
215-
log.error("IO exception occurred when closing file input stream for file path: " + filePath);
204+
log.error("IO exception occurred when closing zip input stream for file path: " + filePath);
216205
}
217206
}
218207
if (fos != null) {
219208
try {
220209
fos.close();
210+
} catch (IOException e) {
211+
log.error("IO exception occurred when closing file output stream for file path: " + filePath);
212+
}
213+
}
214+
if (fis != null) {
215+
try {
216+
fis.close();
221217
} catch (IOException e) {
222218
log.error("IO exception occurred when closing file input stream for file path: " + filePath);
223219
}
@@ -290,11 +286,6 @@ public void stop() {
290286

291287
}
292288

293-
private void sendEvents(StreamEvent streamEvent, Object[] data, ComplexEventChunk<StreamEvent> streamEventChunk) {
294-
complexEventPopulater.populateComplexEvent(streamEvent, data);
295-
nextProcessor.process(streamEventChunk);
296-
}
297-
298289
private void createParentDirectory(File file, String filePathUri) {
299290
if (!file.getParentFile().exists()) {
300291
boolean suDirCreateResult = new File(file.getParent()).mkdirs();

component/src/test/java/io/siddhi/extension/io/file/FileFunctionsTestCase.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public void init() {
4949
public void doBeforeMethod() {
5050
count.set(0);
5151
try {
52-
//FileUtils.copyDirectory(sourceRoot, tempSource);
5352
FileUtils.deleteDirectory(destination);
5453
FileUtils.forceMkdir(destination);
5554
} catch (IOException e) {
@@ -372,6 +371,8 @@ public void receive(Event[] events) {
372371
Thread.sleep(100);
373372
siddhiAppRuntime.shutdown();
374373

374+
count.set(0);
375+
375376
app = "" +
376377
"@App:name('TestSiddhiApp')" +
377378
"define stream ListArchivedFileStream(sample string);\n" +
@@ -389,8 +390,9 @@ public void receive(Event[] events) {
389390
public void receive(Event[] events) {
390391
EventPrinter.print(events);
391392
int n = count.getAndIncrement();
393+
log.error(n);
392394
for (Event event : events) {
393-
if (n > 0 && n <= 3) {
395+
if (n >= 0 && n < 3) {
394396
AssertJUnit.assertTrue(fileList.contains(event.getData(0)));
395397
} else {
396398
AssertJUnit.fail("More events received than expected.");
@@ -538,6 +540,7 @@ public void receive(Event[] events) {
538540
Thread.sleep(100);
539541
siddhiAppRuntime.shutdown();
540542

543+
count.set(0);
541544
app = "" +
542545
"@App:name('TestSiddhiApp')" +
543546
"define stream ListArchivedFileStream(sample string);\n" +
@@ -556,7 +559,7 @@ public void receive(Event[] events) {
556559
EventPrinter.print(events);
557560
int n = count.getAndIncrement();
558561
for (Event event : events) {
559-
if (n > 0 && n <= 3) {
562+
if (n >= 0 && n < 3) {
560563
AssertJUnit.assertTrue(fileList.contains(event.getData(0)));
561564
} else {
562565
AssertJUnit.fail("More events received than expected.");
@@ -788,10 +791,10 @@ public void receive(Event[] events) {
788791
}
789792

790793
@Test
791-
public void fileMoveFunction() throws InterruptedException {
794+
public void fileMoveFunction() throws InterruptedException, IOException {
795+
FileUtils.copyDirectory(sourceRoot, tempSource);
792796
log.info("test Siddhi Io File Function for move()");
793797
AssertJUnit.assertFalse(isFileExist(sourceRoot + "/destination", false));
794-
count.set(0);
795798
String app = "" +
796799
"@App:name('TestSiddhiApp')" +
797800
"define stream MoveFileStream(sample string);\n" +

component/src/test/resources/testng.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
<class name="io.siddhi.extension.io.file.FileFunctionsTestCase"/>
3131
</classes>
3232
</test>
33-
</suite>
33+
</suite>

0 commit comments

Comments
 (0)