001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.compressors.brotli;
020
021import org.apache.commons.compress.utils.OsgiUtils;
022
023/**
024 * Utility code for the Brotli compression format.
025 *
026 * @ThreadSafe
027 * @since 1.14
028 */
029public class BrotliUtils {
030
031    enum CachedAvailability {
032        DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
033    }
034
035    private static volatile CachedAvailability cachedBrotliAvailability;
036
037    static {
038        cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
039        setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
040    }
041
042    // only exists to support unit tests
043    static CachedAvailability getCachedBrotliAvailability() {
044        return cachedBrotliAvailability;
045    }
046
047    private static boolean internalIsBrotliCompressionAvailable() {
048        try {
049            Class.forName("org.brotli.dec.BrotliInputStream");
050            return true;
051        } catch (final NoClassDefFoundError | Exception error) { // NOSONAR
052            return false;
053        }
054    }
055
056    /**
057     * Are the classes required to support Brotli compression available?
058     *
059     * @return true if the classes required to support Brotli compression are available
060     */
061    public static boolean isBrotliCompressionAvailable() {
062        final CachedAvailability cachedResult = cachedBrotliAvailability;
063        if (cachedResult != CachedAvailability.DONT_CACHE) {
064            return cachedResult == CachedAvailability.CACHED_AVAILABLE;
065        }
066        return internalIsBrotliCompressionAvailable();
067    }
068
069    /**
070     * Whether to cache the result of the Brotli for Java check.
071     *
072     * <p>
073     * This defaults to {@code false} in an OSGi environment and {@code true} otherwise.
074     * </p>
075     *
076     * @param doCache whether to cache the result
077     */
078    public static void setCacheBrotliAvailablity(final boolean doCache) {
079        if (!doCache) {
080            cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
081        } else if (cachedBrotliAvailability == CachedAvailability.DONT_CACHE) {
082            final boolean hasBrotli = internalIsBrotliCompressionAvailable();
083            cachedBrotliAvailability = hasBrotli ? CachedAvailability.CACHED_AVAILABLE : CachedAvailability.CACHED_UNAVAILABLE;
084        }
085    }
086
087    /** Private constructor to prevent instantiation of this utility class. */
088    private BrotliUtils() {
089    }
090}