001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling;
015
016import ch.qos.logback.core.FileAppender;
017import ch.qos.logback.core.rolling.helper.CompressionMode;
018import ch.qos.logback.core.rolling.helper.FileNamePattern;
019import ch.qos.logback.core.spi.ContextAwareBase;
020
021/**
022 * Implements methods common to most, it not all, rolling policies. Currently
023 * such methods are limited to a compression mode getter/setter.
024 * 
025 * @author Ceki Gülcü
026 */
027public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
028    protected CompressionMode compressionMode = CompressionMode.NONE;
029
030    FileNamePattern fileNamePattern;
031    // fileNamePatternStr is always slashified, see setter
032    protected String fileNamePatternStr;
033
034    private FileAppender<?> parent;
035
036    // use to name files within zip file, i.e. the zipEntry
037    FileNamePattern zipEntryFileNamePattern;
038    private boolean started;
039
040    /**
041     * Given the FileNamePattern string, this method determines the compression
042     * mode depending on last letters of the fileNamePatternStr. Patterns ending
043     * with .gz imply GZIP compression, endings with '.zip' imply ZIP compression.
044     * Otherwise and by default, there is no compression.
045     * 
046     */
047    protected void determineCompressionMode() {
048        if (fileNamePatternStr.endsWith(".gz")) {
049            addInfo("Will use gz compression");
050            compressionMode = CompressionMode.GZ;
051        } else if (fileNamePatternStr.endsWith(".zip")) {
052            addInfo("Will use zip compression");
053            compressionMode = CompressionMode.ZIP;
054        } else {
055            addInfo("No compression will be used");
056            compressionMode = CompressionMode.NONE;
057        }
058    }
059
060    public void setFileNamePattern(String fnp) {
061        fileNamePatternStr = fnp;
062    }
063
064    public String getFileNamePattern() {
065        return fileNamePatternStr;
066    }
067
068    public CompressionMode getCompressionMode() {
069        return compressionMode;
070    }
071
072    public boolean isStarted() {
073        return started;
074    }
075
076    public void start() {
077        started = true;
078    }
079
080    public void stop() {
081        started = false;
082    }
083
084    public void setParent(FileAppender<?> appender) {
085        this.parent = appender;
086    }
087
088    public boolean isParentPrudent() {
089        return parent.isPrudent();
090    }
091
092    public String getParentsRawFileProperty() {
093        return parent.rawFileProperty();
094    }
095}