OLD | NEW |
| (Empty) |
1 /* | |
2 * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 * contributor license agreements. See the NOTICE file distributed with | |
4 * this work for additional information regarding copyright ownership. | |
5 * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 * (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 | |
18 package org.apache.tomcat.jni; | |
19 | |
20 import java.io.File; | |
21 | |
22 /** Library | |
23 * | |
24 * @author Mladen Turk | |
25 */ | |
26 public final class Library { | |
27 | |
28 /* Default library names */ | |
29 private static final String [] NAMES = {"netty-tcnative", "libnetty-tcnative
", "netty-tcnative-1", "libnetty-tcnative-1"}; | |
30 /* | |
31 * A handle to the unique Library singleton instance. | |
32 */ | |
33 private static Library _instance = null; | |
34 | |
35 private Library() throws Exception { | |
36 boolean loaded = false; | |
37 String path = System.getProperty("java.library.path"); | |
38 String [] paths = path.split(File.pathSeparator); | |
39 StringBuilder err = new StringBuilder(); | |
40 for (int i = 0; i < NAMES.length; i++) { | |
41 try { | |
42 System.loadLibrary(NAMES[i]); | |
43 loaded = true; | |
44 } catch (ThreadDeath | VirtualMachineError t) { | |
45 throw t; | |
46 } catch (Throwable t) { | |
47 String name = System.mapLibraryName(NAMES[i]); | |
48 for (int j = 0; j < paths.length; j++) { | |
49 java.io.File fd = new java.io.File(paths[j] , name); | |
50 if (fd.exists()) { | |
51 // File exists but failed to load | |
52 throw t; | |
53 } | |
54 } | |
55 if (i > 0) { | |
56 err.append(", "); | |
57 } | |
58 err.append(t.getMessage()); | |
59 } | |
60 if (loaded) { | |
61 break; | |
62 } | |
63 } | |
64 if (!loaded) { | |
65 StringBuilder names = new StringBuilder(); | |
66 for (String name : NAMES) { | |
67 names.append(name); | |
68 names.append(", "); | |
69 } | |
70 throw new LibraryNotFoundError(names.substring(0, names.length() -2)
, err.toString()); | |
71 } | |
72 } | |
73 | |
74 private Library(String libraryName) | |
75 { | |
76 if (!"provided".equals(libraryName)) { | |
77 System.loadLibrary(libraryName); | |
78 } | |
79 } | |
80 | |
81 /* create global TCN's APR pool | |
82 * This has to be the first call to TCN library. | |
83 */ | |
84 private static native boolean initialize(); | |
85 /* destroy global TCN's APR pool | |
86 * This has to be the last call to TCN library. | |
87 */ | |
88 public static native void terminate(); | |
89 /* Internal function for loading APR Features */ | |
90 private static native boolean has(int what); | |
91 /* Internal function for loading APR Features */ | |
92 private static native int version(int what); | |
93 /* Internal function for loading APR sizes */ | |
94 private static native int size(int what); | |
95 | |
96 /* TCN_MAJOR_VERSION */ | |
97 public static int TCN_MAJOR_VERSION = 0; | |
98 /* TCN_MINOR_VERSION */ | |
99 public static int TCN_MINOR_VERSION = 0; | |
100 /* TCN_PATCH_VERSION */ | |
101 public static int TCN_PATCH_VERSION = 0; | |
102 /* TCN_IS_DEV_VERSION */ | |
103 public static int TCN_IS_DEV_VERSION = 0; | |
104 /* APR_MAJOR_VERSION */ | |
105 public static int APR_MAJOR_VERSION = 0; | |
106 /* APR_MINOR_VERSION */ | |
107 public static int APR_MINOR_VERSION = 0; | |
108 /* APR_PATCH_VERSION */ | |
109 public static int APR_PATCH_VERSION = 0; | |
110 /* APR_IS_DEV_VERSION */ | |
111 public static int APR_IS_DEV_VERSION = 0; | |
112 | |
113 /* TCN_VERSION_STRING */ | |
114 public static native String versionString(); | |
115 /* APR_VERSION_STRING */ | |
116 public static native String aprVersionString(); | |
117 | |
118 /* APR Feature Macros */ | |
119 public static boolean APR_HAVE_IPV6 = false; | |
120 public static boolean APR_HAS_SHARED_MEMORY = false; | |
121 public static boolean APR_HAS_THREADS = false; | |
122 public static boolean APR_HAS_SENDFILE = false; | |
123 public static boolean APR_HAS_MMAP = false; | |
124 public static boolean APR_HAS_FORK = false; | |
125 public static boolean APR_HAS_RANDOM = false; | |
126 public static boolean APR_HAS_OTHER_CHILD = false; | |
127 public static boolean APR_HAS_DSO = false; | |
128 public static boolean APR_HAS_SO_ACCEPTFILTER = false; | |
129 public static boolean APR_HAS_UNICODE_FS = false; | |
130 public static boolean APR_HAS_PROC_INVOKED = false; | |
131 public static boolean APR_HAS_USER = false; | |
132 public static boolean APR_HAS_LARGE_FILES = false; | |
133 public static boolean APR_HAS_XTHREAD_FILES = false; | |
134 public static boolean APR_HAS_OS_UUID = false; | |
135 /* Are we big endian? */ | |
136 public static boolean APR_IS_BIGENDIAN = false; | |
137 /* APR sets APR_FILES_AS_SOCKETS to 1 on systems where it is possible | |
138 * to poll on files/pipes. | |
139 */ | |
140 public static boolean APR_FILES_AS_SOCKETS = false; | |
141 /* This macro indicates whether or not EBCDIC is the native character set. | |
142 */ | |
143 public static boolean APR_CHARSET_EBCDIC = false; | |
144 /* Is the TCP_NODELAY socket option inherited from listening sockets? | |
145 */ | |
146 public static boolean APR_TCP_NODELAY_INHERITED = false; | |
147 /* Is the O_NONBLOCK flag inherited from listening sockets? | |
148 */ | |
149 public static boolean APR_O_NONBLOCK_INHERITED = false; | |
150 | |
151 | |
152 public static int APR_SIZEOF_VOIDP; | |
153 public static int APR_PATH_MAX; | |
154 public static int APRMAXHOSTLEN; | |
155 public static int APR_MAX_IOVEC_SIZE; | |
156 public static int APR_MAX_SECS_TO_LINGER; | |
157 public static int APR_MMAP_THRESHOLD; | |
158 public static int APR_MMAP_LIMIT; | |
159 | |
160 /* return global TCN's APR pool */ | |
161 public static native long globalPool(); | |
162 | |
163 /** | |
164 * Setup any APR internal data structures. This MUST be the first function | |
165 * called for any APR library. | |
166 * @param libraryName the name of the library to load | |
167 */ | |
168 public static boolean initialize(String libraryName) | |
169 throws Exception | |
170 { | |
171 if (_instance == null) { | |
172 if (libraryName == null) | |
173 _instance = new Library(); | |
174 else | |
175 _instance = new Library(libraryName); | |
176 TCN_MAJOR_VERSION = version(0x01); | |
177 TCN_MINOR_VERSION = version(0x02); | |
178 TCN_PATCH_VERSION = version(0x03); | |
179 TCN_IS_DEV_VERSION = version(0x04); | |
180 APR_MAJOR_VERSION = version(0x11); | |
181 APR_MINOR_VERSION = version(0x12); | |
182 APR_PATCH_VERSION = version(0x13); | |
183 APR_IS_DEV_VERSION = version(0x14); | |
184 | |
185 APR_SIZEOF_VOIDP = size(1); | |
186 APR_PATH_MAX = size(2); | |
187 APRMAXHOSTLEN = size(3); | |
188 APR_MAX_IOVEC_SIZE = size(4); | |
189 APR_MAX_SECS_TO_LINGER = size(5); | |
190 APR_MMAP_THRESHOLD = size(6); | |
191 APR_MMAP_LIMIT = size(7); | |
192 | |
193 APR_HAVE_IPV6 = has(0); | |
194 APR_HAS_SHARED_MEMORY = has(1); | |
195 APR_HAS_THREADS = has(2); | |
196 APR_HAS_SENDFILE = has(3); | |
197 APR_HAS_MMAP = has(4); | |
198 APR_HAS_FORK = has(5); | |
199 APR_HAS_RANDOM = has(6); | |
200 APR_HAS_OTHER_CHILD = has(7); | |
201 APR_HAS_DSO = has(8); | |
202 APR_HAS_SO_ACCEPTFILTER = has(9); | |
203 APR_HAS_UNICODE_FS = has(10); | |
204 APR_HAS_PROC_INVOKED = has(11); | |
205 APR_HAS_USER = has(12); | |
206 APR_HAS_LARGE_FILES = has(13); | |
207 APR_HAS_XTHREAD_FILES = has(14); | |
208 APR_HAS_OS_UUID = has(15); | |
209 APR_IS_BIGENDIAN = has(16); | |
210 APR_FILES_AS_SOCKETS = has(17); | |
211 APR_CHARSET_EBCDIC = has(18); | |
212 APR_TCP_NODELAY_INHERITED = has(19); | |
213 APR_O_NONBLOCK_INHERITED = has(20); | |
214 if (APR_MAJOR_VERSION < 1) { | |
215 throw new UnsatisfiedLinkError("Unsupported APR Version (" + | |
216 aprVersionString() + ")"); | |
217 } | |
218 if (!APR_HAS_THREADS) { | |
219 throw new UnsatisfiedLinkError("Missing APR_HAS_THREADS"); | |
220 } | |
221 } | |
222 return initialize(); | |
223 } | |
224 } | |
OLD | NEW |