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

io.sealights.agents.plugin.CleanupManager Maven / Gradle / Ivy

package io.sealights.agents.plugin;

import org.apache.maven.plugin.logging.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shahar on 7/24/2016.
 */
public class CleanupManager {

    private final Log log;
    private List files = new ArrayList();

    public static CleanupManager getInstance(Log log) {
        return SingletonHolder.getInstance(log);
    }

    private CleanupManager(Log log){
        this.log = log;
    }

    public void clean(){
        for (String fileName : files){
            if (fileName == null)
                continue;
            String errMessage = "Failed to delete '" + fileName + "'.";
            try{
                File f = new File(fileName);
                if (!f.exists()){
                    log.warn(errMessage + " File not exist.");
                }
                else if (!f.delete()){
                    log.warn(errMessage);
                }else{
                    log.debug("File '" + fileName + "' deleted." );
                }
            }catch (Exception e){
                log.error(errMessage + ". Error:", e);
            }
        }
    }

    public void addFile(String fileName){
        if (fileName !=null)
            files.add(fileName);
    }

    /**
     * Lazy initialization of CleanupManager
     */
    private static class SingletonHolder {
        private static CleanupManager INSTANCE = null;

        static CleanupManager getInstance(Log log) {
            if (INSTANCE == null)
                INSTANCE = new CleanupManager(log);

            return INSTANCE;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy