All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.modules.ftpclient.DeleteOrNothingStrategy Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package org.mule.modules.ftpclient;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.function.Consumer;

import org.mule.api.MuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Strategy for deleting processed files (or to do nothing).
 */
public class DeleteOrNothingStrategy implements CompletionStrategy {
    private static Logger logger = LoggerFactory.getLogger(DeleteOrNothingStrategy.class);

    private boolean deleteAfterGet;

    public DeleteOrNothingStrategy(boolean deleteAfterGet) {
        this.deleteAfterGet = deleteAfterGet;
    }

    @Override
    public Consumer createCompletionHandler(@SuppressWarnings("unused") MuleMessage message,
            String filename, String translatedName) {
        Consumer onClose;

        if (deleteAfterGet) {
            onClose = new FileDeleter(filename, translatedName);
        } else {
            onClose = new FileDeleter();
        }

        return onClose;
    }

    private static class FileDeleter implements Consumer {
        private Collection fileSet;

        public FileDeleter(String... files) {
            fileSet = new HashSet<>();
            for (String file : files) {
                fileSet.add(file);
            }
        }

        @Override
        public void accept(ClientWrapper wrapper) {
            String directory = wrapper.getCurrentDirectory();
            for (String file : fileSet) {
                try {
                    wrapper.delete(directory, file);
                } catch (IOException e) {
                    logger.warn("Could not delete file " + directory + "/" + file, e);
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy