|
1 | 1 | package org.fisco.bcos.sdk.v3.client;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.InputStream; |
3 | 5 | import java.math.BigInteger;
|
4 |
| -import java.net.URL; |
| 6 | +import java.nio.file.Files; |
5 | 7 | import java.util.Objects;
|
| 8 | +import java.util.UUID; |
6 | 9 | import java.util.concurrent.ArrayBlockingQueue;
|
7 | 10 | import java.util.concurrent.ConcurrentHashMap;
|
8 | 11 | import java.util.concurrent.ThreadPoolExecutor;
|
9 | 12 | import java.util.concurrent.TimeUnit;
|
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
10 | 14 | import java.util.concurrent.atomic.AtomicInteger;
|
11 | 15 | import java.util.stream.Collectors;
|
12 | 16 | import org.fisco.bcos.sdk.tars.Callback;
|
@@ -90,8 +94,11 @@ public void setTransactionFactory(TransactionFactoryImpl transactionFactory) {
|
90 | 94 | this.transactionFactory = transactionFactory;
|
91 | 95 | }
|
92 | 96 |
|
93 |
| - protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) { |
| 97 | + protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) |
| 98 | + throws Exception { |
94 | 99 | super(groupID, configOption, nativePointer);
|
| 100 | + |
| 101 | + loadLibrary(); |
95 | 102 | String connectionString =
|
96 | 103 | RPCClient.toConnectionString(
|
97 | 104 | new StringVector(configOption.getNetworkConfig().getTarsPeers()));
|
@@ -133,16 +140,40 @@ public void onMessage(int seq) {
|
133 | 140 | };
|
134 | 141 | }
|
135 | 142 |
|
136 |
| - public static void loadLibrary() { |
137 |
| - URL configUrl = TarsClient.class.getClassLoader().getResource(libFileName); |
138 |
| - System.load(configUrl.getPath()); |
139 |
| - } |
| 143 | + private static AtomicBoolean loaded = new AtomicBoolean(false); |
140 | 144 |
|
141 |
| - public static void loadLibrary(String libPath) { |
142 |
| - System.load(libPath); |
| 145 | + private static void loadLibrary() throws Exception { |
| 146 | + boolean inited = loaded.getAndSet(true); |
| 147 | + if (inited) { |
| 148 | + return; |
| 149 | + } |
| 150 | + try { |
| 151 | + File jniFile = File.createTempFile(libFileName, UUID.randomUUID().toString()); |
| 152 | + String osName = System.getProperty("os.name"); |
| 153 | + if (osName.contains("Linux")) osName = "linux"; |
| 154 | + else if (osName.contains("Mac OS X")) osName = "darwin"; |
| 155 | + else if (osName.contains("Windows")) osName = "windows"; |
| 156 | + |
| 157 | + String osArch = System.getProperty("os.arch"); |
| 158 | + if (osArch.contains("amd64")) osArch = "x86_64"; |
| 159 | + |
| 160 | + InputStream jniStream = |
| 161 | + TarsClient.class.getResourceAsStream( |
| 162 | + "/" + osName + "-" + osArch + "/" + libFileName); |
| 163 | + Files.copy( |
| 164 | + jniStream, |
| 165 | + jniFile.getAbsoluteFile().toPath(), |
| 166 | + java.nio.file.StandardCopyOption.REPLACE_EXISTING); |
| 167 | + System.load(jniFile.getAbsolutePath()); |
| 168 | + jniFile.deleteOnExit(); |
| 169 | + } catch (Exception e) { |
| 170 | + e.printStackTrace(); |
| 171 | + throw e; |
| 172 | + } |
143 | 173 | }
|
144 | 174 |
|
145 |
| - public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) { |
| 175 | + public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) |
| 176 | + throws Exception { |
146 | 177 | logger.info(
|
147 | 178 | "TarsClient build, groupID: {}, configOption: {}, nativePointer: {}",
|
148 | 179 | groupId,
|
|
0 commit comments