001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling;
018
019import java.io.File;
020import java.util.Objects;
021
022import org.apache.logging.log4j.core.appender.rolling.action.Action;
023import org.apache.logging.log4j.core.appender.rolling.action.CommonsCompressAction;
024import org.apache.logging.log4j.core.appender.rolling.action.GzCompressAction;
025import org.apache.logging.log4j.core.appender.rolling.action.ZipCompressAction;
026
027/**
028 *  Enumerates over supported file extensions for compression.
029 */
030public enum FileExtension {
031    ZIP(".zip") {
032        @Override
033        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
034                                    final int compressionLevel) {
035            return new ZipCompressAction(source(renameTo), target(compressedName), deleteSource, compressionLevel);
036        }
037    },
038    GZ(".gz") {
039        @Override
040        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
041                                    final int compressionLevel) {
042            return new GzCompressAction(source(renameTo), target(compressedName), deleteSource, compressionLevel);
043        }
044    },
045    BZIP2(".bz2") {
046        @Override
047        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
048                                    final int compressionLevel) {
049            // One of "gz", "bzip2", "xz", "pack200", or "deflate".
050            return new CommonsCompressAction("bzip2", source(renameTo), target(compressedName), deleteSource);
051        }
052    },
053    DEFLATE(".deflate") {
054        @Override
055        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
056                                    final int compressionLevel) {
057            // One of "gz", "bzip2", "xz", "pack200", or "deflate".
058            return new CommonsCompressAction("deflate", source(renameTo), target(compressedName), deleteSource);
059        }
060    },
061    PACK200(".pack200") {
062        @Override
063        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
064                                    final int compressionLevel) {
065            // One of "gz", "bzip2", "xz", "pack200", or "deflate".
066            return new CommonsCompressAction("pack200", source(renameTo), target(compressedName), deleteSource);
067        }
068    },
069    XZ(".xz") {
070        @Override
071        Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
072                                    final int compressionLevel) {
073            // One of "gz", "bzip2", "xz", "pack200", or "deflate".
074            return new CommonsCompressAction("xz", source(renameTo), target(compressedName), deleteSource);
075        }
076    };
077
078    public static FileExtension lookup(final String fileExtension) {
079        for (final FileExtension ext : values()) {
080            if (ext.isExtensionFor(fileExtension)) {
081                return ext;
082            }
083        }
084        return null;
085    }
086
087    public static FileExtension lookupForFile(final String fileName) {
088        for (final FileExtension ext : values()) {
089            if (fileName.endsWith(ext.extension)) {
090                return ext;
091            }
092        }
093        return null;
094    }
095
096    private final String extension;
097
098    private FileExtension(final String extension) {
099        Objects.requireNonNull(extension, "extension");
100        this.extension = extension;
101    }
102
103    abstract Action createCompressAction(String renameTo, String compressedName, boolean deleteSource,
104                                         int compressionLevel);
105
106    String getExtension() {
107        return extension;
108    }
109
110    boolean isExtensionFor(final String s) {
111        return s.endsWith(this.extension);
112    }
113
114    int length() {
115        return extension.length();
116    }
117
118    File source(final String fileName) {
119        return new File(fileName);
120    }
121
122    File target(final String fileName) {
123        return new File(fileName);
124    }
125}