From 299168752c2e095b13309474e955831e7fca0cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=AB=E6=A5=A0?= <651932351@qq.com> Date: Sat, 7 Oct 2023 17:34:34 +0800 Subject: [PATCH 1/2] Update sdk usage --- build.gradle | 21 ++++++++---- .../fisco/bcos/sdk/v3/client/TarsClient.java | 32 +++++++++++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 58db590ca..978049a0c 100644 --- a/build.gradle +++ b/build.gradle @@ -78,6 +78,19 @@ allprojects { } } +def getTargetPlatform() { + def platform = System.properties['os.name'].toLowerCase() + '_' + System.properties['os.arch'].toLowerCase(); + def targetPlatform = System.getProperty('targetPlatform') + if(targetPlatform != null) { + platform = targetPlatform + } + else { + logger.info("No target platform specified, use local platform: " + platform) + } + + return platform +} + sourceSets { main { java { @@ -125,7 +138,7 @@ googleJavaFormat { } dependencies { - api("org.fisco-bcos:fisco-bcos-tars-sdk:${tarsSDKVersion}") + api("org.fisco-bcos:fisco-bcos-tars-sdk-" + getTargetPlatform() + ":${tarsSDKVersion}") api("org.fisco-bcos:bcos-sdk-jni:${bcosSdkJniVersion}") { exclude group : "org.slf4j" exclude group : "com.fasterxml.jackson.core" @@ -136,14 +149,10 @@ dependencies { api("org.apache.commons:commons-lang3:${commonsLang3Version}") api("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}") api("commons-io:commons-io:${commonsIOVersion}") - // api("com.webank:key-mini-toolkit:${keyMiniToolkit}") api("com.webank:webank-blockchain-java-crypto:${webankJavaCryptoVersion}") api("com.moandjiezana.toml:toml4j:${toml4jVersion}") { exclude group: "com.google.code.gson" } -// api("org.apache.commons:commons-configuration2:${config2Version}"){ -// exclude group: "commons-logging" -// } integrationTestImplementation project integrationWasmTestImplementation project @@ -262,7 +271,7 @@ publishing { jar { // destinationDir file('dist/apps') - archiveName "fisco-bcos-" + project.name + '-' + project.version + '.jar' + archiveFileName="fisco-bcos-" + project.name + '-' + project.version + '.jar' exclude '**/*.xml' exclude '**/*.properties' diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java b/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java index d5bdfd2f6..4721d17ac 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java @@ -1,12 +1,16 @@ package org.fisco.bcos.sdk.v3.client; +import java.io.File; +import java.io.InputStream; import java.math.BigInteger; -import java.net.URL; +import java.nio.file.Files; import java.util.Objects; +import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import org.fisco.bcos.sdk.tars.Callback; @@ -92,6 +96,8 @@ public void setTransactionFactory(TransactionFactoryImpl transactionFactory) { protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) { super(groupID, configOption, nativePointer); + + loadLibrary(); String connectionString = RPCClient.toConnectionString( new StringVector(configOption.getNetworkConfig().getTarsPeers())); @@ -133,13 +139,25 @@ public void onMessage(int seq) { }; } - public static void loadLibrary() { - URL configUrl = TarsClient.class.getClassLoader().getResource(libFileName); - System.load(configUrl.getPath()); - } + private static AtomicBoolean loaded = new AtomicBoolean(false); - public static void loadLibrary(String libPath) { - System.load(libPath); + private static void loadLibrary() { + boolean inited = loaded.getAndSet(true); + if (inited) { + return; + } + try { + File jniFile = File.createTempFile(libFileName, UUID.randomUUID().toString()); + InputStream jniStream = TarsClient.class.getResourceAsStream("/" + libFileName); + Files.copy( + jniStream, + jniFile.getAbsoluteFile().toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + System.load(jniFile.getAbsolutePath()); + jniFile.deleteOnExit(); + } catch (Exception e) { + e.printStackTrace(); + } } public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) { From 8ef606c4fc59c04c7e2902dd8a5005ca22fcb11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=AB=E6=A5=A0?= <651932351@qq.com> Date: Mon, 9 Oct 2023 16:43:17 +0800 Subject: [PATCH 2/2] Support jni inside jar --- build.gradle | 15 +------------ .../fisco/bcos/sdk/v3/client/TarsClient.java | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 978049a0c..0cb474de8 100644 --- a/build.gradle +++ b/build.gradle @@ -78,19 +78,6 @@ allprojects { } } -def getTargetPlatform() { - def platform = System.properties['os.name'].toLowerCase() + '_' + System.properties['os.arch'].toLowerCase(); - def targetPlatform = System.getProperty('targetPlatform') - if(targetPlatform != null) { - platform = targetPlatform - } - else { - logger.info("No target platform specified, use local platform: " + platform) - } - - return platform -} - sourceSets { main { java { @@ -138,7 +125,7 @@ googleJavaFormat { } dependencies { - api("org.fisco-bcos:fisco-bcos-tars-sdk-" + getTargetPlatform() + ":${tarsSDKVersion}") + api("org.fisco-bcos:fisco-bcos-tars-sdk" + ":${tarsSDKVersion}") api("org.fisco-bcos:bcos-sdk-jni:${bcosSdkJniVersion}") { exclude group : "org.slf4j" exclude group : "com.fasterxml.jackson.core" diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java b/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java index 4721d17ac..5bb18a258 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java @@ -94,7 +94,8 @@ public void setTransactionFactory(TransactionFactoryImpl transactionFactory) { this.transactionFactory = transactionFactory; } - protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) { + protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) + throws Exception { super(groupID, configOption, nativePointer); loadLibrary(); @@ -141,14 +142,24 @@ public void onMessage(int seq) { private static AtomicBoolean loaded = new AtomicBoolean(false); - private static void loadLibrary() { + private static void loadLibrary() throws Exception { boolean inited = loaded.getAndSet(true); if (inited) { return; } try { File jniFile = File.createTempFile(libFileName, UUID.randomUUID().toString()); - InputStream jniStream = TarsClient.class.getResourceAsStream("/" + libFileName); + String osName = System.getProperty("os.name"); + if (osName.contains("Linux")) osName = "linux"; + else if (osName.contains("Mac OS X")) osName = "darwin"; + else if (osName.contains("Windows")) osName = "windows"; + + String osArch = System.getProperty("os.arch"); + if (osArch.contains("amd64")) osArch = "x86_64"; + + InputStream jniStream = + TarsClient.class.getResourceAsStream( + "/" + osName + "-" + osArch + "/" + libFileName); Files.copy( jniStream, jniFile.getAbsoluteFile().toPath(), @@ -157,10 +168,12 @@ private static void loadLibrary() { jniFile.deleteOnExit(); } catch (Exception e) { e.printStackTrace(); + throw e; } } - public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) { + public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) + throws Exception { logger.info( "TarsClient build, groupID: {}, configOption: {}, nativePointer: {}", groupId,