From e93ab02d1e52fbc09a1e077c63f232ec1154f93e Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Mon, 20 May 2024 16:23:47 +0200 Subject: [PATCH] LibHelper utility added --- CHANGELOG.md | 4 ++ .../org/fugerit/java/core/lang/LibHelper.java | 49 +++++++++++++++++++ .../fugerit/java/core/lang/TestLibHelper.java | 26 ++++++++++ .../core/lang/lib_helper/test_ll_fug1.txt | 0 .../core/lang/lib_helper/test_ll_fug2.txt | 0 5 files changed, 79 insertions(+) create mode 100644 fj-core/src/main/java/org/fugerit/java/core/lang/LibHelper.java create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/lang/TestLibHelper.java create mode 100644 fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug1.txt create mode 100644 fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug2.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index b743e403..458c45a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- LibHelper utility + ## [8.6.1] - 2024-05-14 ### Added diff --git a/fj-core/src/main/java/org/fugerit/java/core/lang/LibHelper.java b/fj-core/src/main/java/org/fugerit/java/core/lang/LibHelper.java new file mode 100644 index 00000000..e8b698f5 --- /dev/null +++ b/fj-core/src/main/java/org/fugerit/java/core/lang/LibHelper.java @@ -0,0 +1,49 @@ +package org.fugerit.java.core.lang; + +import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.core.cfg.ConfigRuntimeException; + +import java.io.File; +import java.io.FilenameFilter; + +@Slf4j +public class LibHelper { + + private LibHelper() {} + + private static final String LLP = "java.library.path"; + + public static int ll(String name ) { + return ll( name, 1 ); + } + + public static int ll(String name, int checkCount ) { + return ll( name, (f, n) -> n.contains(name), checkCount ); + } + + public static int ll(String name, FilenameFilter ffn, int checkCount ) { + String libPath = System.getProperty( LLP ); + log.info( "loading library path [{}]", libPath ); + int count = 0; + if ( libPath != null ) { + String[] listPath = libPath.split(File.pathSeparator ); + for ( int k=0; k checkCount ) { + throw new ConfigRuntimeException( String.format( "%s is too many library paths : %s", name, count ) ); + } else if ( count > 1 ) { + log.info( "loading library [{}]", name ); + System.loadLibrary( name ); + } else { + log.info( "skip loading library [{}]", name ); + } + return count; + } + +} diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/lang/TestLibHelper.java b/fj-core/src/test/java/test/org/fugerit/java/core/lang/TestLibHelper.java new file mode 100644 index 00000000..56f501be --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/lang/TestLibHelper.java @@ -0,0 +1,26 @@ +package test.org.fugerit.java.core.lang; + +import org.fugerit.java.core.cfg.ConfigRuntimeException; +import org.fugerit.java.core.lang.LibHelper; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class TestLibHelper { + + private static final String LLP = "java.library.path"; + + @Test + public void testCount0() throws IOException { + File libraryPath = new File( "src/test/resources/core/lang/lib_helper" ); + String llPath = System.getProperty( LLP ); + System.setProperty( LLP, llPath+File.pathSeparator+libraryPath.getCanonicalPath() ); + int count1 = LibHelper.ll( "test_ll_fug1" ); + Assert.assertEquals( 1, count1 ); + Assert.assertThrows( ConfigRuntimeException.class, () -> LibHelper.ll( "test_ll_fug" ) ); + Assert.assertEquals( 0, LibHelper.ll( "test_ll_not_found" ) ); + } + +} diff --git a/fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug1.txt b/fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug1.txt new file mode 100644 index 00000000..e69de29b diff --git a/fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug2.txt b/fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug2.txt new file mode 100644 index 00000000..e69de29b