Skip to content

Commit

Permalink
LibHelper utility added
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed May 20, 2024
1 parent 2172bc2 commit 42cdf3b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions fj-core/src/main/java/org/fugerit/java/core/lang/LibHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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";

private static final String OSN = "os.name";

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<listPath.length; k++ ) {
File folder = new File( listPath[k] );
if ( folder.isDirectory() ) {
count+=folder.list(ffn).length;
}
}
}
log.info( "name : {}, checkCount : {}, count : {} ", name, checkCount, count );
if ( count > 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;
}

}
Original file line number Diff line number Diff line change
@@ -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" ) );
}

}
Empty file.
Empty file.

0 comments on commit 42cdf3b

Please sign in to comment.