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.action;
18  
19  import java.io.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.util.Objects;
25  
26  import org.apache.commons.compress.compressors.CompressorException;
27  import org.apache.commons.compress.compressors.CompressorStreamFactory;
28  import org.apache.commons.compress.utils.IOUtils;
29  
30  /**
31   * Compresses a file using bzip2 compression.
32   */
33  public final class CommonsCompressAction extends AbstractAction {
34  
35      private static final int BUF_SIZE = 8102;
36  
37      /**
38       * Compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
39       */
40      private final String name;
41  
42      /**
43       * Source file.
44       */
45      private final File source;
46  
47      /**
48       * Destination file.
49       */
50      private final File destination;
51  
52      /**
53       * If true, attempt to delete file on completion.
54       */
55      private final boolean deleteSource;
56  
57      /**
58       * Creates new instance of Bzip2CompressAction.
59       * 
60       * @param name the compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
61       * @param source file to compress, may not be null.
62       * @param destination compressed file, may not be null.
63       * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
64       *            to be thrown or affect return value.
65       */
66      public CommonsCompressAction(final String name, final File source, final File destination,
67              final boolean deleteSource) {
68          Objects.requireNonNull(source, "source");
69          Objects.requireNonNull(destination, "destination");
70          this.name = name;
71          this.source = source;
72          this.destination = destination;
73          this.deleteSource = deleteSource;
74      }
75  
76      /**
77       * Compresses.
78       *
79       * @return true if successfully compressed.
80       * @throws IOException on IO exception.
81       */
82      @Override
83      public boolean execute() throws IOException {
84          return execute(name, source, destination, deleteSource);
85      }
86  
87      /**
88       * Compresses a file.
89       * 
90       * @param name the compressor name, i.e. "gz", "bzip2", "xz", "pack200", or "deflate".
91       * @param source file to compress, may not be null.
92       * @param destination compressed file, may not be null.
93       * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
94       *            to be thrown or affect return value.
95       *
96       * @return true if source file compressed.
97       * @throws IOException on IO exception.
98       */
99      public static boolean execute(final String name, final File source, final File destination,
100             final boolean deleteSource) throws IOException {
101         if (!source.exists()) {
102             return false;
103         }
104         try (final FileInputStream input = new FileInputStream(source);
105                 final BufferedOutputStream output = new BufferedOutputStream(
106                         new CompressorStreamFactory().createCompressorOutputStream(name, new FileOutputStream(
107                                 destination)))) {
108             IOUtils.copy(input, output, BUF_SIZE);
109         } catch (final CompressorException e) {
110             throw new IOException(e);
111         }
112 
113         if (deleteSource && !source.delete()) {
114             LOGGER.warn("Unable to delete " + source.toString() + '.');
115         }
116         return true;
117     }
118 
119     /**
120      * Reports exception.
121      *
122      * @param ex exception.
123      */
124     @Override
125     protected void reportException(final Exception ex) {
126         LOGGER.warn("Exception during " + name + " compression of '" + source.toString() + "'.", ex);
127     }
128 
129     @Override
130     public String toString() {
131         return CommonsCompressAction.class.getSimpleName() + '[' + source + " to " + destination //
132                 + ", deleteSource=" + deleteSource + ']';
133     }
134 
135     public String getName() {
136         return name;
137     }
138 
139     public File getSource() {
140         return source;
141     }
142 
143     public File getDestination() {
144         return destination;
145     }
146 
147     public boolean isDeleteSource() {
148         return deleteSource;
149     }
150 }