Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,36 @@
@Contract(threading = ThreadingBehavior.STATELESS)
public final class Brotli4jRuntime {

private static final String BROTLI = "com.aayushatharva.brotli4j.Brotli4jLoader";
private static final String BROTLI4J_LOADER = "com.aayushatharva.brotli4j.Brotli4jLoader";

private static final String IS_AVAILABLE = "isAvailable";

private Brotli4jRuntime() {
}

/**
* @return {@code true} if {@code com.aayushatharva.brotli4j} can be loaded
* by the current class loader; {@code false} otherwise
* @return {@code true} if Brotli4j and its native library are available
* to the HttpClient class loader; {@code false} otherwise
*/
public static boolean available() {
return available(Brotli4jRuntime.class.getClassLoader());
}

/**
* Determines whether Brotli4j and its native library are available to the
* given class loader.
*
* @param classLoader the class loader used to locate Brotli4j
* @return {@code true} if Brotli4j reports that its native runtime is
* available; {@code false} otherwise
*/
static boolean available(final ClassLoader classLoader) {
try {
Class.forName(BROTLI, false, Brotli4jRuntime.class.getClassLoader());
return true;
} catch (ClassNotFoundException | LinkageError ex) {
final Class<?> loaderClass =
Class.forName(BROTLI4J_LOADER, true, classLoader);
return Boolean.TRUE.equals(
loaderClass.getMethod(IS_AVAILABLE).invoke(null));
} catch (final ReflectiveOperationException | LinkageError ex) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.impl;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.UUID;

import com.aayushatharva.brotli4j.Brotli4jLoader;
import com.aayushatharva.brotli4j.service.BrotliNativeProvider;
import org.junit.jupiter.api.Test;

class Brotli4jRuntimeTest {

private static final String LIBRARY_PATH_PROPERTY =
"brotli4j.library.path";

@Test
void unavailableWhenNativeLibraryCannotBeLoaded() throws Exception {
final URL brotli4jLocation = Brotli4jLoader.class
.getProtectionDomain()
.getCodeSource()
.getLocation();
final URL serviceLocation = BrotliNativeProvider.class
.getProtectionDomain()
.getCodeSource()
.getLocation();

final File missingLibrary = new File(
"target",
"missing-brotli-" + UUID.randomUUID());
final String previousLibraryPath = System.setProperty(
LIBRARY_PATH_PROPERTY,
missingLibrary.getAbsolutePath());

try (URLClassLoader classLoader = new URLClassLoader(
new URL[] {brotli4jLocation, serviceLocation},
null)) {

assertNotNull(Class.forName(
"com.aayushatharva.brotli4j.Brotli4jLoader",
false,
classLoader));

assertFalse(Brotli4jRuntime.available(classLoader));
} finally {
if (previousLibraryPath != null) {
System.setProperty(
LIBRARY_PATH_PROPERTY,
previousLibraryPath);
} else {
System.clearProperty(LIBRARY_PATH_PROPERTY);
}
}
}
}
Loading