Skip to content

Commit

Permalink
Support jni inside jar (FISCO-BCOS#826)
Browse files Browse the repository at this point in the history
* Update sdk usage

* Support jni inside jar
  • Loading branch information
morebtcg authored and kyonRay committed Oct 12, 2023
1 parent fe8987f commit 5fa8c65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
8 changes: 2 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ googleJavaFormat {
}

dependencies {
api("org.fisco-bcos:fisco-bcos-tars-sdk:${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"
Expand All @@ -136,14 +136,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
Expand Down Expand Up @@ -262,7 +258,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'

Expand Down
49 changes: 40 additions & 9 deletions src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -90,8 +94,11 @@ 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();
String connectionString =
RPCClient.toConnectionString(
new StringVector(configOption.getNetworkConfig().getTarsPeers()));
Expand Down Expand Up @@ -133,16 +140,40 @@ 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() throws Exception {
boolean inited = loaded.getAndSet(true);
if (inited) {
return;
}
try {
File jniFile = File.createTempFile(libFileName, UUID.randomUUID().toString());
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(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
System.load(jniFile.getAbsolutePath());
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,
Expand Down

0 comments on commit 5fa8c65

Please sign in to comment.