1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package io.netty.internal.tcnative;
34
35 import java.io.File;
36
37 public final class Library {
38
39
40 private static final String [] NAMES = {
41 "netty_tcnative",
42 "libnetty_tcnative"
43 };
44
45 private static final String PROVIDED = "provided";
46
47
48
49
50 private static Library _instance = null;
51
52 private Library() throws Exception {
53 boolean loaded = false;
54 String path = System.getProperty("java.library.path");
55 String [] paths = path.split(File.pathSeparator);
56 StringBuilder err = new StringBuilder();
57 for (int i = 0; i < NAMES.length; i++) {
58 try {
59 loadLibrary(NAMES[i]);
60 loaded = true;
61 } catch (ThreadDeath t) {
62 throw t;
63 } catch (VirtualMachineError t) {
64 throw t;
65 } catch (Throwable t) {
66 String name = System.mapLibraryName(NAMES[i]);
67 for (int j = 0; j < paths.length; j++) {
68 File fd = new File(paths[j] , name);
69 if (fd.exists()) {
70
71 throw new RuntimeException(t);
72 }
73 }
74 if (i > 0) {
75 err.append(", ");
76 }
77 err.append(t.getMessage());
78 }
79 if (loaded) {
80 break;
81 }
82 }
83 if (!loaded) {
84 throw new UnsatisfiedLinkError(err.toString());
85 }
86 }
87
88 private Library(String libraryName) {
89 if (!PROVIDED.equals(libraryName)) {
90 loadLibrary(libraryName);
91 }
92 }
93
94 private static void loadLibrary(String libraryName) {
95 System.loadLibrary(calculatePackagePrefix().replace('.', '_') + libraryName);
96 }
97
98
99
100
101
102
103 private static String calculatePackagePrefix() {
104 String maybeShaded = Library.class.getName();
105
106 String expected = "io!netty!internal!tcnative!Library".replace('!', '.');
107 if (!maybeShaded.endsWith(expected)) {
108 throw new UnsatisfiedLinkError(String.format(
109 "Could not find prefix added to %s to get %s. When shading, only adding a "
110 + "package prefix is supported", expected, maybeShaded));
111 }
112 return maybeShaded.substring(0, maybeShaded.length() - expected.length());
113 }
114
115
116
117
118 private static native boolean initialize0();
119
120 private static native boolean aprHasThreads();
121
122 private static native int aprMajorVersion();
123
124
125 private static native String aprVersionString();
126
127
128
129
130
131
132
133 public static boolean initialize() throws Exception {
134 return initialize(PROVIDED, null);
135 }
136
137
138
139
140
141
142
143
144
145 public static boolean initialize(String libraryName, String engine) throws Exception {
146 if (_instance == null) {
147 _instance = libraryName == null ? new Library() : new Library(libraryName);
148
149 if (aprMajorVersion() < 1) {
150 throw new UnsatisfiedLinkError("Unsupported APR Version (" +
151 aprVersionString() + ")");
152 }
153
154 if (!aprHasThreads()) {
155 throw new UnsatisfiedLinkError("Missing APR_HAS_THREADS");
156 }
157 }
158 return initialize0() && SSL.initialize(engine) == 0;
159 }
160 }