Skip to content

Commit e93ab02

Browse files
committed
LibHelper utility added
1 parent 2172bc2 commit e93ab02

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- LibHelper utility
13+
1014
## [8.6.1] - 2024-05-14
1115

1216
### Added
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.fugerit.java.core.lang;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
5+
6+
import java.io.File;
7+
import java.io.FilenameFilter;
8+
9+
@Slf4j
10+
public class LibHelper {
11+
12+
private LibHelper() {}
13+
14+
private static final String LLP = "java.library.path";
15+
16+
public static int ll(String name ) {
17+
return ll( name, 1 );
18+
}
19+
20+
public static int ll(String name, int checkCount ) {
21+
return ll( name, (f, n) -> n.contains(name), checkCount );
22+
}
23+
24+
public static int ll(String name, FilenameFilter ffn, int checkCount ) {
25+
String libPath = System.getProperty( LLP );
26+
log.info( "loading library path [{}]", libPath );
27+
int count = 0;
28+
if ( libPath != null ) {
29+
String[] listPath = libPath.split(File.pathSeparator );
30+
for ( int k=0; k<listPath.length; k++ ) {
31+
File folder = new File( listPath[k] );
32+
if ( folder.isDirectory() ) {
33+
count+=folder.list(ffn).length;
34+
}
35+
}
36+
}
37+
log.info( "name : {}, checkCount : {}, count : {} ", name, checkCount, count );
38+
if ( count > checkCount ) {
39+
throw new ConfigRuntimeException( String.format( "%s is too many library paths : %s", name, count ) );
40+
} else if ( count > 1 ) {
41+
log.info( "loading library [{}]", name );
42+
System.loadLibrary( name );
43+
} else {
44+
log.info( "skip loading library [{}]", name );
45+
}
46+
return count;
47+
}
48+
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test.org.fugerit.java.core.lang;
2+
3+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
4+
import org.fugerit.java.core.lang.LibHelper;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
public class TestLibHelper {
12+
13+
private static final String LLP = "java.library.path";
14+
15+
@Test
16+
public void testCount0() throws IOException {
17+
File libraryPath = new File( "src/test/resources/core/lang/lib_helper" );
18+
String llPath = System.getProperty( LLP );
19+
System.setProperty( LLP, llPath+File.pathSeparator+libraryPath.getCanonicalPath() );
20+
int count1 = LibHelper.ll( "test_ll_fug1" );
21+
Assert.assertEquals( 1, count1 );
22+
Assert.assertThrows( ConfigRuntimeException.class, () -> LibHelper.ll( "test_ll_fug" ) );
23+
Assert.assertEquals( 0, LibHelper.ll( "test_ll_not_found" ) );
24+
}
25+
26+
}

fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug1.txt

Whitespace-only changes.

fj-core/src/test/resources/core/lang/lib_helper/test_ll_fug2.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)