龙将斩千 v5.5.5.39856版本的 MD5 值为:5c05b1d6f8d872ca3fe0343d6ff07caf

以下内容为反编译后的 FixedWindowRollingPolicy.java 源代码,内容仅作参考


package ch.qos.logback.core.rolling;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.rolling.helper.CompressionMode;
import ch.qos.logback.core.rolling.helper.Compressor;
import ch.qos.logback.core.rolling.helper.FileFilterUtil;
import ch.qos.logback.core.rolling.helper.FileNamePattern;
import ch.qos.logback.core.rolling.helper.RenameUtil;
import java.io.File;
import java.util.Date;

public class FixedWindowRollingPolicy extends RollingPolicyBase {
    static final String FNP_NOT_SET = "The \"FileNamePattern\" property must be set before using FixedWindowRollingPolicy. ";
    private static int MAX_WINDOW_SIZE = 20;
    static final String PRUDENT_MODE_UNSUPPORTED = "See also http://logback.qos.ch/codes.html#tbr_fnp_prudent_unsupported";
    static final String SEE_PARENT_FN_NOT_SET = "Please refer to http://logback.qos.ch/codes.html#fwrp_parentFileName_not_set";
    public static final String ZIP_ENTRY_DATE_PATTERN = "yyyy-MM-dd_HHmm";
    Compressor compressor;
    RenameUtil util = new RenameUtil();
    int minIndex = 1;
    int maxIndex = 7;

    private String transformFileNamePatternFromInt2Date(String str) {
        return FileFilterUtil.afterLastSlash(FileFilterUtil.slashify(str)).replace("%i", "%d{yyyy-MM-dd_HHmm}");
    }

    @Override
    public String getActiveFileName() {
        return getParentsRawFileProperty();
    }

    public int getMaxIndex() {
        return this.maxIndex;
    }

    protected int getMaxWindowSize() {
        return MAX_WINDOW_SIZE;
    }

    public int getMinIndex() {
        return this.minIndex;
    }

    @Override
    public void rollover() throws RolloverFailure {
        Compressor compressor;
        String activeFileName;
        String convertInt;
        String str;
        if (this.maxIndex >= 0) {
            File file = new File(this.fileNamePattern.convertInt(this.maxIndex));
            if (file.exists()) {
                file.delete();
            }
            int i = this.maxIndex;
            while (true) {
                i--;
                if (i < this.minIndex) {
                    break;
                }
                String convertInt2 = this.fileNamePattern.convertInt(i);
                if (new File(convertInt2).exists()) {
                    this.util.rename(convertInt2, this.fileNamePattern.convertInt(i + 1));
                } else {
                    addInfo("Skipping roll-over for inexistent file " + convertInt2);
                }
            }
            switch (this.compressionMode) {
                case NONE:
                    this.util.rename(getActiveFileName(), this.fileNamePattern.convertInt(this.minIndex));
                    return;
                case GZ:
                    compressor = this.compressor;
                    activeFileName = getActiveFileName();
                    convertInt = this.fileNamePattern.convertInt(this.minIndex);
                    str = null;
                    break;
                case ZIP:
                    compressor = this.compressor;
                    activeFileName = getActiveFileName();
                    convertInt = this.fileNamePattern.convertInt(this.minIndex);
                    str = this.zipEntryFileNamePattern.convert(new Date());
                    break;
                default:
                    return;
            }
            compressor.compress(activeFileName, convertInt, str);
        }
    }

    public void setMaxIndex(int i) {
        this.maxIndex = i;
    }

    public void setMinIndex(int i) {
        this.minIndex = i;
    }

    @Override
    public void start() {
        this.util.setContext(this.context);
        if (this.fileNamePatternStr == null) {
            addError(FNP_NOT_SET);
            addError(CoreConstants.SEE_FNP_NOT_SET);
            throw new IllegalStateException("The \"FileNamePattern\" property must be set before using FixedWindowRollingPolicy. See also http://logback.qos.ch/codes.html#tbr_fnp_not_set");
        }
        this.fileNamePattern = new FileNamePattern(this.fileNamePatternStr, this.context);
        determineCompressionMode();
        if (isParentPrudent()) {
            addError("Prudent mode is not supported with FixedWindowRollingPolicy.");
            addError(PRUDENT_MODE_UNSUPPORTED);
            throw new IllegalStateException("Prudent mode is not supported.");
        }
        if (getParentsRawFileProperty() == null) {
            addError("The File name property must be set before using this rolling policy.");
            addError(SEE_PARENT_FN_NOT_SET);
            throw new IllegalStateException("The \"File\" option must be set.");
        }
        if (this.maxIndex < this.minIndex) {
            addWarn("MaxIndex (" + this.maxIndex + ") cannot be smaller than MinIndex (" + this.minIndex + ").");
            addWarn("Setting maxIndex to equal minIndex.");
            this.maxIndex = this.minIndex;
        }
        int maxWindowSize = getMaxWindowSize();
        if (this.maxIndex - this.minIndex > maxWindowSize) {
            addWarn("Large window sizes are not allowed.");
            this.maxIndex = this.minIndex + maxWindowSize;
            addWarn("MaxIndex reduced to " + this.maxIndex);
        }
        if (this.fileNamePattern.getIntegerTokenConverter() == null) {
            throw new IllegalStateException("FileNamePattern [" + this.fileNamePattern.getPattern() + "] does not contain a valid IntegerToken");
        }
        if (this.compressionMode == CompressionMode.ZIP) {
            this.zipEntryFileNamePattern = new FileNamePattern(transformFileNamePatternFromInt2Date(this.fileNamePatternStr), this.context);
        }
        this.compressor = new Compressor(this.compressionMode);
        this.compressor.setContext(this.context);
        super.start();
    }
}