Skip to content

Commit

Permalink
rework of loading libs, to make shaded libs possible and having more …
Browse files Browse the repository at this point in the history
…fin granular controll and enable ARM support
  • Loading branch information
nitram509 committed Mar 15, 2016
1 parent 9e5e66f commit 89ed940
Show file tree
Hide file tree
Showing 36 changed files with 85 additions and 141 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ For a list of supported platforms, look for released ```jbrotli-native-*``` arti
##### Example of regular BrotliCompressor with custom dictionary

```java
NativeLoader.loadLibrary("brotli");
BrotliLibraryLoader.loadBrotli();

byte[] inBuf = "Brotli: a new compression algorithm for the internet. Now available for Java!".getBytes();
byte[] compressedBuf = new byte[2048];
Expand All @@ -95,7 +95,7 @@ int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuf, compressedB
##### Example of BrotliStreamCompressor using default dictionary

```java
NativeLoader.loadLibrary("brotli");
BrotliLibraryLoader.loadBrotli();

byte[] inBuf = "Brotli: a new compression algorithm for the internet. Now available for Java!".getBytes();
boolean doFlush = true;
Expand Down
2 changes: 1 addition & 1 deletion jbrotli-native/darwin/build.sh → jbrotli-native/darwin-x86-amd64/build.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

CURPATH=$(pwd)
TARGET_CLASSES_PATH="target/classes"
TARGET_CLASSES_PATH="target/classes/lib/darwin-x86-amd64"
TARGET_PATH="target"

function exitWithError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.meteogroup.jbrotli</groupId>
<artifactId>jbrotli-native-darwin</artifactId>
<artifactId>jbrotli-native-darwin-x86-amd64</artifactId>
<packaging>jar</packaging>
<version>0.3.1-SNAPSHOT</version>

Expand All @@ -20,13 +20,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.meteogroup.jbrotli.jni;

import org.scijava.nativelib.NativeLoader;
import org.testng.annotations.Test;

import java.net.URL;

public class LoadLibraryTest {

@Test
public void the_library_can_be_loaded() throws Exception {
NativeLoader.loadLibrary("brotli");
URL resource = this.getClass().getResource("/lib/darwin-x86-amd64/brotli.dylib");
Runtime.getRuntime().load(resource.getFile());
}
}
7 changes: 0 additions & 7 deletions jbrotli-native/linux-arm32-vfp-hflt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.meteogroup.jbrotli.jni;

import org.scijava.nativelib.NativeLoader;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -26,6 +25,8 @@ public class LoadLibraryTest {
@Test
public void the_library_can_be_loaded() throws Exception {
// because of cross compiling, we can't test here.
// URL resource = this.getClass().getResource("/lib/linux-x86-amd64/libbrotli.so");
// Runtime.getRuntime().load(resource.getFile());
assertThat(true).isTrue();
}
}
2 changes: 1 addition & 1 deletion jbrotli-native/linux-x86-amd64/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

CURPATH=$(pwd)
TARGET_CLASSES_PATH="target/classes"
TARGET_CLASSES_PATH="target/classes/lib/linux-x86-amd64"
TARGET_PATH="target"

exitWithError() {
Expand Down
7 changes: 0 additions & 7 deletions jbrotli-native/linux-x86-amd64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.meteogroup.jbrotli.jni;

import org.scijava.nativelib.NativeLoader;
import org.testng.annotations.Test;

import java.net.URL;

public class LoadLibraryTest {

@Test
public void the_library_can_be_loaded() throws Exception {
NativeLoader.loadLibrary("brotli");
URL resource = this.getClass().getResource("/lib/linux-x86-amd64/libbrotli.so");
Runtime.getRuntime().load(resource.getFile());
}
}
4 changes: 2 additions & 2 deletions jbrotli-native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
</profile>

<profile>
<id>darwin</id>
<id>darwin-x86-amd64</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<modules>
<module>darwin</module>
<module>darwin-x86-amd64</module>
</modules>
</profile>

Expand Down
6 changes: 4 additions & 2 deletions jbrotli-native/win32-x86-amd64/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ cd "%~dp0"
:PREPARE_FOLDERS
if not exist "%~dp0target" mkdir "%~dp0target"
if not exist "%~dp0target\classes" mkdir "%~dp0target\classes"
SET TARGET_CLASSES_PATH=%~dp0target\classes
if not exist "%~dp0target\classes\lib" mkdir "%~dp0target\classes\lib"
SET TARGET_CLASSES_PATH=%~dp0target\classes\lib\win32-x86-amd64
if not exist "%TARGET_CLASSES_PATH%" mkdir "%TARGET_CLASSES_PATH%"

:PREPARE_MAKEFILES
cd "%~dp0target"
Expand All @@ -27,7 +29,7 @@ cd "%~dp0target"
nmake || goto ERROR

:COPY_DLL_FOR_MAVEN_PACKAGING
copy /Y "%~dp0target\brotli.dll" "%TARGET_CLASSES_PATH%" || goto ERROR
copy /Y "%~dp0target\brotli.dll" "%TARGET_CLASSES_PATH%" || goto ERROR

:ENSURE_WORKING_DIRECTORY
cd %~dp0
Expand Down
7 changes: 0 additions & 7 deletions jbrotli-native/win32-x86-amd64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
8 changes: 5 additions & 3 deletions jbrotli-native/win32-x86/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ REM Requirements
REM --------------
REM o Java 1.8 JDK installed, needs JAVA_HOME set
REM o cmake 3.0 + installed and available via PATH
REM o nmake installed (comes e.g. with Visual Studio), call "vcvarsall.bat x86" before to activate 64bit tools
REM o nmake installed (comes e.g. with Visual Studio), call "vcvarsall.bat x86" before to activate 32bit tools
REM

:ENSURE_WORKING_DIRECTORY
Expand All @@ -16,7 +16,9 @@ cd "%~dp0"
:PREPARE_FOLDERS
if not exist "%~dp0target" mkdir "%~dp0target"
if not exist "%~dp0target\classes" mkdir "%~dp0target\classes"
SET TARGET_CLASSES_PATH=%~dp0target\classes
if not exist "%~dp0target\classes\lib" mkdir "%~dp0target\classes\lib"
SET TARGET_CLASSES_PATH=%~dp0target\classes\lib\win32-x86
if not exist "%TARGET_CLASSES_PATH%" mkdir "%TARGET_CLASSES_PATH%"

:PREPARE_MAKEFILES
cd "%~dp0target"
Expand All @@ -27,7 +29,7 @@ cd "%~dp0target"
nmake || goto ERROR

:COPY_DLL_FOR_MAVEN_PACKAGING
copy /Y "%~dp0target\brotli.dll" "%TARGET_CLASSES_PATH%" || goto ERROR
copy /Y "%~dp0target\brotli.dll" "%TARGET_CLASSES_PATH%" || goto ERROR

:ENSURE_WORKING_DIRECTORY
cd %~dp0
Expand Down
7 changes: 0 additions & 7 deletions jbrotli-native/win32-x86/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.meteogroup.jbrotli.jni;

import org.scijava.nativelib.NativeLoader;
import org.testng.annotations.Test;

import java.net.URL;

public class LoadLibraryTest {

@Test
public void the_library_can_be_loaded() throws Exception {
NativeLoader.loadLibrary("brotli");
URL resource = this.getClass().getResource("/lib/win32-x86/brotli.dll");
Runtime.getRuntime().load(resource.getFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.meteogroup.jbrotli.Brotli;
import org.meteogroup.jbrotli.BrotliCompressor;
import org.meteogroup.jbrotli.BrotliStreamCompressor;
import org.meteogroup.jbrotli.libloader.BrotliLibraryLoader;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
Expand All @@ -27,7 +28,6 @@
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.scijava.nativelib.NativeLoader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -49,7 +49,7 @@ public class Brotli_vs_Gzip_Benchmark {

@Setup
public void init() throws IOException {
NativeLoader.loadLibrary("brotli");
BrotliLibraryLoader.loadBrotli();

brotliParameter = new Brotli.Parameter(Brotli.Mode.GENERIC, 5, Brotli.DEFAULT_LGWIN, Brotli.DEFAULT_LGBLOCK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.meteogroup.jbrotli.servlet;

import org.meteogroup.jbrotli.Brotli;
import org.scijava.nativelib.NativeLoader;
import org.meteogroup.jbrotli.libloader.BrotliLibraryLoader;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -51,8 +51,8 @@ public class BrotliServletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
try {
NativeLoader.loadLibrary("brotli");
} catch (IOException e) {
BrotliLibraryLoader.loadBrotli();
} catch (UnsatisfiedLinkError | IllegalStateException | SecurityException e) {
throw new ServletException(e);
}
applyFilterConfig(filterConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.meteogroup.jbrotli.Brotli;
import org.meteogroup.jbrotli.BrotliDeCompressor;
import org.scijava.nativelib.NativeLoader;
import org.meteogroup.jbrotli.libloader.BrotliLibraryLoader;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
Expand All @@ -35,7 +35,7 @@ public class BrotliServletOutputStreamTest {

@BeforeClass
public void loadLibrary() throws Exception {
NativeLoader.loadLibrary("brotli");
BrotliLibraryLoader.loadBrotli();
}

@BeforeMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.meteogroup.jbrotli.servlet;

import org.meteogroup.jbrotli.Brotli;
import org.scijava.nativelib.NativeLoader;
import org.meteogroup.jbrotli.libloader.BrotliLibraryLoader;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -36,7 +36,7 @@ public class BrotliServletResponseWrapperTest {

@BeforeClass
public void loadLibrary() throws Exception {
NativeLoader.loadLibrary("brotli");
BrotliLibraryLoader.loadBrotli();
}

@BeforeMethod
Expand Down
10 changes: 2 additions & 8 deletions jbrotli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</profile>

<profile>
<id>darwin</id>
<id>darwin-x86-amd64</id>
<activation>
<os>
<family>mac</family>
Expand All @@ -81,7 +81,7 @@
<dependencies>
<dependency>
<groupId>org.meteogroup.jbrotli</groupId>
<artifactId>jbrotli-native-darwin</artifactId>
<artifactId>jbrotli-native-darwin-x86-amd64</artifactId>
<version>${version.jbrotli.native}</version>
</dependency>
</dependencies>
Expand Down Expand Up @@ -192,12 +192,6 @@

<dependencies>

<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.1.3</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@

package org.meteogroup.jbrotli.libloader;

import org.meteogroup.jbrotli.Brotli;
import org.meteogroup.jbrotli.BrotliStreamCompressor;

import java.io.*;
import java.nio.file.Files;

public class BrotliLibraryLoader {

private static final String BROTLI_LIB_NAME = "brotli";
Expand All @@ -35,7 +29,6 @@ public class BrotliLibraryLoader {
*/
public static synchronized void loadBrotli() throws UnsatisfiedLinkError, IllegalStateException, SecurityException {
LibraryLoader libraryLoader = new LibraryLoader(BROTLI_LIB_NAME);
if (libraryLoader.tryAlreadyLoaded()) return;
if (libraryLoader.trySystemLibraryLoading()) return;
if (libraryLoader.tryLoadingFromTemporaryFolder()) return;
String details = libraryLoader.getResult().asFormattedString();
Expand Down
Loading

0 comments on commit 89ed940

Please sign in to comment.