CleanupTask.java 4.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.pdfg.common.AESProperties
 *  com.adobe.pdfg.exception.ConfigException
 *  com.adobe.pdfg.service.api.PDFGConfigService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.pdfg.impl;

import com.adobe.pdfg.common.AESProperties;
import com.adobe.pdfg.exception.ConfigException;
import com.adobe.pdfg.service.api.PDFGConfigService;
import java.io.File;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CleanupTask
implements Runnable {
    private static volatile boolean IS_TASK_ACTIVE = false;
    private List<File> listOfFolders = null;
    private long jobExpiration = 1000 * (long)AESProperties.getJobExpirationSeconds();
    private static Logger logger = LoggerFactory.getLogger((String)"PDFG Cleanup Daemon");
    private static final long MILLISECONDS_PER_SECOND = 1000;
    private final PDFGConfigService configService;

    public CleanupTask(PDFGConfigService configService) {
        if (this.jobExpiration == 0) {
            this.jobExpiration = 86400000;
        }
        this.configService = configService;
        logger.debug("Cleanup expiration time is set to " + this.jobExpiration + " ms.");
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void run() {
        logger.debug("Woken up at " + new Date());
        if (!IS_TASK_ACTIVE) {
            try {
                IS_TASK_ACTIVE = true;
                this.cleanUp();
                Object var2_1 = null;
                IS_TASK_ACTIVE = false;
            }
            catch (Throwable var1_3) {
                Object var2_2 = null;
                IS_TASK_ACTIVE = false;
                throw var1_3;
            }
        } else {
            logger.debug("Previous cleanup is still in progress.");
        }
    }

    private void cleanUp() {
        logger.info("Performing cleanup of old PDFG files");
        this.populateListOfFolders();
        long currentTime = System.currentTimeMillis();
        for (File folder : this.listOfFolders) {
            this.cleanUpFolder(folder, currentTime, 0);
        }
    }

    private boolean cleanUpFolder(File file, long currentTime, int level) {
        logger.debug("Cleaning up folder: " + file.getAbsolutePath());
        boolean bCleanedAll = true;
        File[] files = file.listFiles();
        if (files != null && files.length != 0) {
            for (int count = 0; count < files.length; ++count) {
                File currentFile = files[count];
                if (currentFile.isFile()) {
                    long timeModified = currentFile.lastModified();
                    if (currentTime - timeModified > this.jobExpiration) {
                        boolean bCleanedCurrent = currentFile.delete();
                        if (bCleanedCurrent) {
                            logger.debug("Successful file deletion: " + currentFile.getAbsolutePath());
                        } else {
                            logger.debug("File deletion failure: " + currentFile.getAbsolutePath());
                        }
                        bCleanedAll = bCleanedAll && bCleanedCurrent;
                        continue;
                    }
                    logger.debug("File too young to be cleaned up: " + currentFile.getAbsolutePath());
                    bCleanedAll = false;
                    continue;
                }
                boolean bCleanedSubFolder = this.cleanUpFolder(currentFile, currentTime, level + 1);
                if (bCleanedSubFolder) {
                    boolean deleteThisFolder = false;
                    if (level == 0) {
                        deleteThisFolder = true;
                    } else {
                        long timeModified = currentFile.lastModified();
                        boolean bl = deleteThisFolder = currentTime - timeModified > this.jobExpiration;
                    }
                    if (!deleteThisFolder) continue;
                    boolean bCleanedCurrent = currentFile.delete();
                    if (bCleanedCurrent) {
                        logger.debug("Successful folder deletion: " + currentFile.getAbsolutePath());
                    } else {
                        logger.debug("Folder deletion failure: " + currentFile.getAbsolutePath());
                    }
                    bCleanedAll = bCleanedAll && bCleanedCurrent;
                    continue;
                }
                bCleanedAll = false;
            }
        }
        return bCleanedAll;
    }

    void populateListOfFolders() {
        if (this.listOfFolders == null) {
            try {
                this.listOfFolders = this.configService.getPDFGTempFolders();
            }
            catch (ConfigException e) {
                throw new RuntimeException((Throwable)e);
            }
        }
    }
}