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 * @ThreadSafe
026 * @since 1.14
027 */
028public class BrotliUtils {
029
030    enum CachedAvailability {
031        DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
032    }
033
034    private static volatile CachedAvailability cachedBrotliAvailability;
035
036    static {
037        cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
038        setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
039    }
040
041    /** Private constructor to prevent instantiation of this utility class. */
042    private BrotliUtils() {
043    }
044
045
046    /**
047     * Are the classes required to support Brotli compression available?
048     * @return true if the classes required to support Brotli compression are available
049     */
050    public static boolean isBrotliCompressionAvailable() {
051        final CachedAvailability cachedResult = cachedBrotliAvailability;
052        if (cachedResult != CachedAvailability.DONT_CACHE) {
053            return cachedResult == CachedAvailability.CACHED_AVAILABLE;
054        }
055        return internalIsBrotliCompressionAvailable();
056    }
057
058    private static boolean internalIsBrotliCompressionAvailable() {
059        try {
060            Class.forName("org.brotli.dec.BrotliInputStream");
061            return true;
062        } catch (final NoClassDefFoundError | Exception error) { // NOSONAR
063            return false;
064        }
065    }
066
067    /**
068     * Whether to cache the result of the Brotli for Java check.
069     *
070     * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p>
071     * @param doCache whether to cache the result
072     */
073    public static void setCacheBrotliAvailablity(final boolean doCache) {
074        if (!doCache) {
075            cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
076        } else if (cachedBrotliAvailability == CachedAvailability.DONT_CACHE) {
077            final boolean hasBrotli = internalIsBrotliCompressionAvailable();
078            cachedBrotliAvailability = hasBrotli ? CachedAvailability.CACHED_AVAILABLE
079                : CachedAvailability.CACHED_UNAVAILABLE;
080        }
081    }
082
083    // only exists to support unit tests
084    static CachedAvailability getCachedBrotliAvailability() {
085        return cachedBrotliAvailability;
086    }
087}