View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling;
18  
19  import java.io.File;
20  import java.util.Objects;
21  
22  import org.apache.logging.log4j.core.appender.rolling.action.Action;
23  import org.apache.logging.log4j.core.appender.rolling.action.CommonsCompressAction;
24  import org.apache.logging.log4j.core.appender.rolling.action.GzCompressAction;
25  import org.apache.logging.log4j.core.appender.rolling.action.ZipCompressAction;
26  
27  /**
28   *  Enumerates over supported file extensions for compression.
29   */
30  public enum FileExtension {
31      ZIP(".zip") {
32          @Override
33          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
34                                      final int compressionLevel) {
35              return new ZipCompressAction(source(renameTo), target(compressedName), deleteSource, compressionLevel);
36          }
37      },
38      GZ(".gz") {
39          @Override
40          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
41                                      final int compressionLevel) {
42              return new GzCompressAction(source(renameTo), target(compressedName), deleteSource, compressionLevel);
43          }
44      },
45      BZIP2(".bz2") {
46          @Override
47          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
48                                      final int compressionLevel) {
49              // One of "gz", "bzip2", "xz", "pack200", or "deflate".
50              return new CommonsCompressAction("bzip2", source(renameTo), target(compressedName), deleteSource);
51          }
52      },
53      DEFLATE(".deflate") {
54          @Override
55          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
56                                      final int compressionLevel) {
57              // One of "gz", "bzip2", "xz", "pack200", or "deflate".
58              return new CommonsCompressAction("deflate", source(renameTo), target(compressedName), deleteSource);
59          }
60      },
61      PACK200(".pack200") {
62          @Override
63          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
64                                      final int compressionLevel) {
65              // One of "gz", "bzip2", "xz", "pack200", or "deflate".
66              return new CommonsCompressAction("pack200", source(renameTo), target(compressedName), deleteSource);
67          }
68      },
69      XZ(".xz") {
70          @Override
71          Action createCompressAction(final String renameTo, final String compressedName, final boolean deleteSource,
72                                      final int compressionLevel) {
73              // One of "gz", "bzip2", "xz", "pack200", or "deflate".
74              return new CommonsCompressAction("xz", source(renameTo), target(compressedName), deleteSource);
75          }
76      };
77  
78      public static FileExtension lookup(final String fileExtension) {
79          for (final FileExtension ext : values()) {
80              if (ext.isExtensionFor(fileExtension)) {
81                  return ext;
82              }
83          }
84          return null;
85      }
86  
87      public static FileExtension lookupForFile(final String fileName) {
88          for (final FileExtension ext : values()) {
89              if (fileName.endsWith(ext.extension)) {
90                  return ext;
91              }
92          }
93          return null;
94      }
95  
96      private final String extension;
97  
98      private FileExtension(final String extension) {
99          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 }