cancel
Showing results for 
Search instead for 
Did you mean: 

How to delete file after import

wwatroba
Champ in-the-making
Champ in-the-making
Hello,
I'm trying to import files, using following code:

         RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>() {
            public Void execute() throws Throwable {
               
               ImporterService importerService = serviceRegistry.getImporterService();
               
               ACPImportPackageHandler handler = new ACPImportPackageHandler(new File("path/file.acp"),
                     ACPImportPackageHandler.DEFAULT_ENCODING);
               importerService.importView(handler, location, null, null);
               
               return null;
            }
         };
         
         retryingTransactionHelper.doInTransaction(callback, false);‍‍‍‍‍‍‍‍‍‍‍‍‍‍
but after it, I can't delete file (path/file.acp). What I'm doing wrong? It looks like there is a problem with closing stream.
1 REPLY 1

pjanuario
Champ in-the-making
Champ in-the-making
Hi have faced the same problem and fixed it with an workaround.
I presume that the problem is because:
- ACPImportPackageHandler.zipFile
- ImpoterService .Reader: retrieved by ACPImportPackageHandler
- ImpoterService .InputStream: retrieved by ACPImportPackageHandler
This resources aren't closed by ImpoterService or ACPImportPackageHandler.

The workaround that i used was extend the ACPImportPackageHandler and use it in ImporterService:

public class CustomACPImportPackageHandler extends ACPImportPackageHandler {
    private Reader readerDataStream = null;
    private InputStream importStream = null;

    public CustomACPImportPackageHandler(File zipFile, String dataFileEncoding) {
        super(zipFile, dataFileEncoding);
    }

    @Override
    public void endImport() {
        super.endImport();
        this.close();
    }

    @Override
    public Reader getDataStream() {
        this.readerDataStream = super.getDataStream();
        return this.readerDataStream;
    }

    @Override
    public InputStream importStream(String content) {
        this.importStream = super.importStream(content);
        return this.importStream;
    }

    public void close() {
        try {
            this.zipFile.close();
            this.readerDataStream.close();
            this.importStream.close();
        } catch (IOException e) {
            throw new RuntimeException("Unable to close the zip file", e);
        }
    }
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍