View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.util.concurrent.ExecutorService;
17  import java.util.concurrent.Future;
18  
19  import ch.qos.logback.core.rolling.RolloverFailure;
20  import ch.qos.logback.core.spi.ContextAwareBase;
21  import ch.qos.logback.core.util.DynamicClassLoadingException;
22  import ch.qos.logback.core.util.IncompatibleClassException;
23  
24  import static ch.qos.logback.core.rolling.helper.CompressionMode.GZ_SUFFIX;
25  import static ch.qos.logback.core.rolling.helper.CompressionMode.XZ_SUFFIX;
26  import static ch.qos.logback.core.rolling.helper.CompressionMode.ZIP_SUFFIX;
27  import static ch.qos.logback.core.util.OptionHelper.instantiateByClassName;
28  
29  /**
30   * The <code>Compression</code> class implements ZIP and GZ file
31   * compression/decompression methods.
32   *
33   * @author Ceki G&uuml;lc&uuml;
34   */
35  public class Compressor extends ContextAwareBase {
36  
37      public static final String COULD_NOT_OBTAIN_COMPRESSION_STRATEGY_MESSAGE = "Could not obtain compression strategy";
38      public static final String XZ_COMPRESSION_STRATEGY_CLASS_NAME = "ch.qos.logback.core.rolling.helper.XZCompressionStrategy";
39  
40      final CompressionMode compressionMode;
41  
42      static final int BUFFER_SIZE = 8192;
43  
44      public Compressor(CompressionMode compressionMode) {
45          this.compressionMode = compressionMode;
46      }
47  
48      /**
49       * @param originalFileName
50       * @param compressedFileName
51       * @param innerEntryName       The name of the file within the zip file. Use for
52       *                             ZIP compression.
53       */
54      public void compress(String originalFileName, String compressedFileName, String innerEntryName) {
55          CompressionStrategy compressionStrategy = makeCompressionStrategy(compressionMode);
56          if (compressionStrategy == null) {
57              addWarn(COULD_NOT_OBTAIN_COMPRESSION_STRATEGY_MESSAGE);
58              return;
59          }
60          compressionStrategy.setContext(getContext());
61          compressionStrategy.compress(originalFileName, compressedFileName, innerEntryName);
62      }
63  
64      CompressionStrategy makeCompressionStrategy(CompressionMode compressionMode) {
65          switch (compressionMode) {
66          case GZ:
67              return new GZCompressionStrategy();
68          case ZIP:
69              return new ZipCompressionStrategy();
70          case XZ:
71              return dynamicInstantiation(XZ_COMPRESSION_STRATEGY_CLASS_NAME);
72          case NONE:
73              throw new UnsupportedOperationException("compress method called in NONE compression mode");
74          default:
75              return null;
76          }
77      }
78  
79      private CompressionStrategy dynamicInstantiation(String className) {
80          try {
81              return (CompressionStrategy) instantiateByClassName(className, CompressionStrategy.class, getContext());
82          } catch (IncompatibleClassException | DynamicClassLoadingException e) {
83              addError("Could not instantiate " + className, e);
84              return null;
85          }
86      }
87  
88      static public String computeFileNameStrWithoutCompSuffix(String fileNamePatternStr, CompressionMode compressionMode) {
89          int len = fileNamePatternStr.length();
90          switch (compressionMode) {
91          case GZ:
92              if (fileNamePatternStr.endsWith(GZ_SUFFIX))
93                  return fileNamePatternStr.substring(0, len - GZ_SUFFIX.length());
94              else
95                  return fileNamePatternStr;
96          case ZIP:
97              if (fileNamePatternStr.endsWith(ZIP_SUFFIX))
98                  return fileNamePatternStr.substring(0, len - ZIP_SUFFIX.length());
99              else
100                 return fileNamePatternStr;
101         case XZ:
102             if (fileNamePatternStr.endsWith(XZ_SUFFIX))
103                 return fileNamePatternStr.substring(0, len - XZ_SUFFIX.length());
104             else
105                 return fileNamePatternStr;
106         case NONE:
107             return fileNamePatternStr;
108         }
109         throw new IllegalStateException("Execution should not reach this point");
110     }
111 
112     @Override
113     public String toString() {
114         return this.getClass().getName();
115     }
116 
117     public Future<?> asyncCompress(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
118         CompressionRunnable runnable = new CompressionRunnable(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
119         ExecutorService executorService = context.getExecutorService();
120         Future<?> future = executorService.submit(runnable);
121         return future;
122     }
123 
124     class CompressionRunnable implements Runnable {
125         final String nameOfFile2Compress;
126         final String nameOfCompressedFile;
127         final String innerEntryName;
128 
129         public CompressionRunnable(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) {
130             this.nameOfFile2Compress = nameOfFile2Compress;
131             this.nameOfCompressedFile = nameOfCompressedFile;
132             this.innerEntryName = innerEntryName;
133         }
134 
135         public void run() {
136 
137             Compressor.this.compress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
138         }
139     }
140 
141 }