View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.compressors.brotli;
20  
21  import org.apache.commons.compress.utils.OsgiUtils;
22  
23  /**
24   * Utility code for the Brotli compression format.
25   *
26   * @ThreadSafe
27   * @since 1.14
28   */
29  public class BrotliUtils {
30  
31      enum CachedAvailability {
32          DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
33      }
34  
35      private static volatile CachedAvailability cachedBrotliAvailability;
36  
37      static {
38          cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
39          setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
40      }
41  
42      // only exists to support unit tests
43      static CachedAvailability getCachedBrotliAvailability() {
44          return cachedBrotliAvailability;
45      }
46  
47      private static boolean internalIsBrotliCompressionAvailable() {
48          try {
49              Class.forName("org.brotli.dec.BrotliInputStream");
50              return true;
51          } catch (final NoClassDefFoundError | Exception error) { // NOSONAR
52              return false;
53          }
54      }
55  
56      /**
57       * Are the classes required to support Brotli compression available?
58       *
59       * @return true if the classes required to support Brotli compression are available
60       */
61      public static boolean isBrotliCompressionAvailable() {
62          final CachedAvailability cachedResult = cachedBrotliAvailability;
63          if (cachedResult != CachedAvailability.DONT_CACHE) {
64              return cachedResult == CachedAvailability.CACHED_AVAILABLE;
65          }
66          return internalIsBrotliCompressionAvailable();
67      }
68  
69      /**
70       * Whether to cache the result of the Brotli for Java check.
71       *
72       * <p>
73       * This defaults to {@code false} in an OSGi environment and {@code true} otherwise.
74       * </p>
75       *
76       * @param doCache whether to cache the result
77       */
78      public static void setCacheBrotliAvailablity(final boolean doCache) {
79          if (!doCache) {
80              cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
81          } else if (cachedBrotliAvailability == CachedAvailability.DONT_CACHE) {
82              final boolean hasBrotli = internalIsBrotliCompressionAvailable();
83              cachedBrotliAvailability = hasBrotli ? CachedAvailability.CACHED_AVAILABLE : CachedAvailability.CACHED_UNAVAILABLE;
84          }
85      }
86  
87      /** Private constructor to prevent instantiation of this utility class. */
88      private BrotliUtils() {
89      }
90  }