|
5 | 5 | package com.amazonaws.services.lambda.runtime.api.client.runtimeapi;
|
6 | 6 |
|
7 | 7 | import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.InvocationRequest;
|
8 |
| - |
9 |
| -import java.io.FileNotFoundException; |
10 |
| -import java.io.InputStream; |
11 |
| -import java.nio.file.Files; |
12 |
| -import java.nio.file.Paths; |
13 |
| -import java.nio.file.StandardCopyOption; |
14 |
| -import java.util.ArrayList; |
15 |
| -import java.util.List; |
| 8 | +import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.JniHelper; |
16 | 9 |
|
17 | 10 | import static com.amazonaws.services.lambda.runtime.api.client.runtimeapi.LambdaRuntimeApiClientImpl.USER_AGENT;
|
18 | 11 |
|
|
21 | 14 | * interactions with the Runtime API.
|
22 | 15 | */
|
23 | 16 | class NativeClient {
|
24 |
| - private static final String NATIVE_LIB_PATH = "/tmp/.libaws-lambda-jni.so"; |
25 |
| - public static final String NATIVE_CLIENT_JNI_PROPERTY = "com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI"; |
26 | 17 | static void init() {
|
27 |
| - loadJNILib(); |
| 18 | + JniHelper.load(); |
28 | 19 | initializeClient(USER_AGENT.getBytes());
|
29 | 20 | }
|
30 |
| - |
31 |
| - private static void loadJNILib() { |
32 |
| - String jniLib = System.getProperty(NATIVE_CLIENT_JNI_PROPERTY); |
33 |
| - if (jniLib != null) { |
34 |
| - System.load(jniLib); |
35 |
| - } else { |
36 |
| - String[] libsToTry = new String[]{ |
37 |
| - "libaws-lambda-jni.linux-x86_64.so", |
38 |
| - "libaws-lambda-jni.linux-aarch_64.so", |
39 |
| - "libaws-lambda-jni.linux_musl-x86_64.so", |
40 |
| - "libaws-lambda-jni.linux_musl-aarch_64.so" |
41 |
| - }; |
42 |
| - unpackAndLoadNativeLibrary(libsToTry); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - |
47 |
| - /** |
48 |
| - * Unpacks JNI library from the JAR to a temporary location and tries to load it using System.load() |
49 |
| - * Implementation based on AWS CRT |
50 |
| - * (ref. <a href="https://github.com/awslabs/aws-crt-java/blob/0e9c3db8b07258b57c2503cfc47c787ccef10670/src/main/java/software/amazon/awssdk/crt/CRT.java#L106-L134">...</a>) |
51 |
| - * |
52 |
| - * @param libsToTry - array of native libraries to try |
53 |
| - */ |
54 |
| - static void unpackAndLoadNativeLibrary(String[] libsToTry) { |
55 |
| - |
56 |
| - List<String> errorMessages = new ArrayList<>(); |
57 |
| - for (String libToTry : libsToTry) { |
58 |
| - try (InputStream inputStream = NativeClient.class.getResourceAsStream( |
59 |
| - Paths.get("/jni", libToTry).toString())) { |
60 |
| - if (inputStream == null) { |
61 |
| - throw new FileNotFoundException("Specified file not in the JAR: " + libToTry); |
62 |
| - } |
63 |
| - Files.copy(inputStream, Paths.get(NATIVE_LIB_PATH), StandardCopyOption.REPLACE_EXISTING); |
64 |
| - System.load(NATIVE_LIB_PATH); |
65 |
| - return; |
66 |
| - } catch (UnsatisfiedLinkError | Exception e) { |
67 |
| - errorMessages.add(e.getMessage()); |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - for (int i = 0; i < libsToTry.length; ++i) { |
72 |
| - System.err.println("Failed to load the native runtime interface client library " + libsToTry[i] + |
73 |
| - ". Exception: " + errorMessages.get(i)); |
74 |
| - } |
75 |
| - System.exit(-1); |
76 |
| - } |
77 |
| - |
| 21 | + |
78 | 22 | static native void initializeClient(byte[] userAgent);
|
79 | 23 |
|
80 | 24 | static native InvocationRequest next();
|
|
0 commit comments