AutomaticLogFileRemoval.java 3.49 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.util.AbstractPath
 *  com.scene7.is.util.text.ParamAccess
 *  com.scene7.is.util.text.ParameterException
 *  com.scene7.is.util.text.ParsingException
 */
package com.scene7.is.ps.provider.logging;

import com.scene7.is.util.AbstractPath;
import com.scene7.is.util.text.ParamAccess;
import com.scene7.is.util.text.ParameterException;
import com.scene7.is.util.text.ParsingException;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

final class AutomaticLogFileRemoval
extends Thread {
    private static final Logger LOGGER = Logger.getLogger(AutomaticLogFileRemoval.class.getName());
    private static final int REFRESH_INTERVAL_1HOUR = 3600000;
    private File logDir;
    private MaxDaysFileFilter filter;

    public AutomaticLogFileRemoval(ParamAccess params) throws ParameterException, ParsingException {
        super("AutomaticLogFileRemoval");
        AbstractPath serverHome = new AbstractPath(params.getAsString("server.rootPath"));
        AbstractPath loggingRoot = new AbstractPath(serverHome, params.getAsString("rootPath"));
        this.logDir = new File(loggingRoot.toString());
        long maxDays = params.getAsInt("count");
        this.filter = new MaxDaysFileFilter(maxDays);
        if (maxDays > 0) {
            this.start();
        }
    }

    public void dispose() {
        LOGGER.finer("Stopping AutomaticLogFileRemoval...");
        this.interrupt();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void run() {
        try {
            LOGGER.log(Level.INFO, "AutomaticLogFileRemoval thread started");
            while (!this.isInterrupted()) {
                try {
                    this.removeOldLogFiles();
                }
                catch (Throwable e) {
                    LOGGER.log(Level.SEVERE, "Exception during automatic log file removal. Ignored", e);
                }
                Thread.sleep(3600000);
            }
        }
        catch (InterruptedException e) {
            LOGGER.log(Level.FINEST, "AutomaticLogFileRemoval thread interrupted", e);
        }
        finally {
            LOGGER.log(Level.INFO, "AutomaticLogFileRemoval thread exited");
        }
    }

    private void removeOldLogFiles() {
        File[] files = this.logDir.listFiles(this.filter);
        for (int i = 0; files != null && i < files.length; ++i) {
            if (files[i].delete()) {
                LOGGER.log(Level.INFO, "Removed old log file: " + files[i]);
                continue;
            }
            LOGGER.log(Level.WARNING, "Unable to remove old log file: " + files[i]);
        }
    }

    private final class MaxDaysFileFilter
    implements FileFilter {
        private static final long NUM_MILLISECONDS_PER_DAY = 86400000;
        private long maxDays;

        MaxDaysFileFilter(long maxDays) {
            this.maxDays = maxDays;
        }

        @Override
        public boolean accept(File candidate) {
            return candidate.isFile() && candidate.getName().endsWith(".log") && this.getAge(candidate) >= this.maxDays;
        }

        private long getAge(File file) {
            long lastModified = file.lastModified();
            if (lastModified != 0) {
                long now = new Date().getTime();
                return (now - lastModified) / 86400000;
            }
            return 0;
        }
    }

}