16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package java.nicl;
24
25 import java.io.File;
26 import jdk.internal.nicl.Errno;
27 import jdk.internal.nicl.NativeLibraryImpl;
28 import jdk.internal.nicl.types.BindingRegistry;
29 import jdk.internal.nicl.types.Function;
30 import jdk.internal.nicl.types.Type;
31 import jdk.internal.nicl.types.LayoutTypeImpl;
32
33 import java.lang.invoke.MethodHandle;
34 import java.lang.invoke.MethodType;
35 import java.nicl.types.LayoutType;
36
37 public final class NativeLibrary {
38 private static final Errno ERRNO = Errno.platformHasErrno() ? new Errno() : null;
39
40 // don't create
41 private NativeLibrary() {}
42
43 /**
44 * Create a raw, uncivilized version of the interface
45 *
46 * @param c the class to bind
47 * @param lib the library in which to look for native symbols
48 * @return
49 */
50 @Deprecated
51 public static <T> T bindRaw(Class<T> c, Library lib) {
52 SecurityManager security = System.getSecurityManager();
53 if (security != null) {
54 security.checkPermission(new RuntimePermission("java.nicl.bindRaw"));
55 }
107 * @param libs the libraries in which to look for native symbols
108 * @param symbolName the name of the symbol to look up
109 * @param methodType the type of the returned method handle
110 * @param isVarArgs true if the function is a varargs method
111 * @return a method handle which, when invoked, will call the target native function
112 * @throws NoSuchMethodException
113 * @throws IllegalAccessException
114 */
115 public static MethodHandle lookupNativeMethod(Library[] libs, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException {
116 SecurityManager security = System.getSecurityManager();
117 if (security != null) {
118 security.checkPermission(new RuntimePermission("java.nicl.lookupNative", symbolName));
119 }
120 return NativeLibraryImpl.lookupNativeMethod(libs, symbolName, methodType, isVarArgs);
121 }
122
123 public static MethodHandle lookupNativeMethod(Library lib, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException {
124 return lookupNativeMethod(new Library[] { lib }, symbolName, methodType, isVarArgs);
125 }
126
127 public static Library loadLibrary(String name) {
128 SecurityManager security = System.getSecurityManager();
129 if (security != null) {
130 security.checkLink(name);
131 }
132 return NativeLibraryImpl.loadLibrary(name);
133 }
134
135 public static Library loadLibraryFile(String name) {
136 SecurityManager security = System.getSecurityManager();
137 if (security != null) {
138 security.checkLink(name);
139 }
140 if (!(new File(name).isAbsolute())) {
141 throw new UnsatisfiedLinkError(
142 "Expecting an absolute path of the library: " + name);
143 }
144 return NativeLibraryImpl.loadLibraryFile(name);
145 }
146
147 public static Library getDefaultLibrary() {
148 SecurityManager security = System.getSecurityManager();
149 if (security != null) {
150 security.checkPermission(new RuntimePermission("java.nicl.getDefaultLibrary"));
151 }
152 return NativeLibraryImpl.getDefaultLibrary();
153 }
154
155 public static <T> LayoutType<T> createLayout(Class<T> c) {
156 return LayoutTypeImpl.create(c);
157 }
158
159 public static int errno() {
160 if (ERRNO == null) {
161 throw new UnsupportedOperationException();
162 }
163
164 return ERRNO.errno();
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package java.nicl;
24
25 import java.io.File;
26 import jdk.internal.nicl.Errno;
27 import jdk.internal.nicl.NativeLibraryImpl;
28 import jdk.internal.nicl.types.BindingRegistry;
29 import jdk.internal.nicl.types.Function;
30 import jdk.internal.nicl.types.Type;
31 import jdk.internal.nicl.types.LayoutTypeImpl;
32
33 import java.lang.invoke.MethodHandle;
34 import java.lang.invoke.MethodType;
35 import java.nicl.types.LayoutType;
36 import java.util.Objects;
37
38 public final class NativeLibrary {
39 private static final Errno ERRNO = Errno.platformHasErrno() ? new Errno() : null;
40
41 // don't create
42 private NativeLibrary() {}
43
44 /**
45 * Create a raw, uncivilized version of the interface
46 *
47 * @param c the class to bind
48 * @param lib the library in which to look for native symbols
49 * @return
50 */
51 @Deprecated
52 public static <T> T bindRaw(Class<T> c, Library lib) {
53 SecurityManager security = System.getSecurityManager();
54 if (security != null) {
55 security.checkPermission(new RuntimePermission("java.nicl.bindRaw"));
56 }
108 * @param libs the libraries in which to look for native symbols
109 * @param symbolName the name of the symbol to look up
110 * @param methodType the type of the returned method handle
111 * @param isVarArgs true if the function is a varargs method
112 * @return a method handle which, when invoked, will call the target native function
113 * @throws NoSuchMethodException
114 * @throws IllegalAccessException
115 */
116 public static MethodHandle lookupNativeMethod(Library[] libs, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException {
117 SecurityManager security = System.getSecurityManager();
118 if (security != null) {
119 security.checkPermission(new RuntimePermission("java.nicl.lookupNative", symbolName));
120 }
121 return NativeLibraryImpl.lookupNativeMethod(libs, symbolName, methodType, isVarArgs);
122 }
123
124 public static MethodHandle lookupNativeMethod(Library lib, String symbolName, MethodType methodType, boolean isVarArgs) throws NoSuchMethodException, IllegalAccessException {
125 return lookupNativeMethod(new Library[] { lib }, symbolName, methodType, isVarArgs);
126 }
127
128 /**
129 * Loads the native library specified by the <code>libname</code>
130 * argument. The <code>libname</code> argument must not contain any platform
131 * specific prefix, file extension or path.
132 *
133 * Otherwise, the libname argument is loaded from a system library
134 * location and mapped to a native library image in an implementation-
135 * dependent manner.
136 * <p>
137 *
138 * @param libname the name of the library.
139 * @exception SecurityException if a security manager exists and its
140 * <code>checkLink</code> method doesn't allow
141 * loading of the specified dynamic library
142 * @exception UnsatisfiedLinkError if either the libname argument
143 * contains a file path, the native library is not statically
144 * linked with the VM, or the library cannot be mapped to a
145 * native library image by the host system.
146 * @exception NullPointerException if <code>libname</code> is
147 * <code>null</code>
148 * @see java.lang.SecurityManager#checkLink(java.lang.String)
149 */
150 public static Library loadLibrary(String filename) {
151 Objects.requireNonNull(filename);
152 SecurityManager security = System.getSecurityManager();
153 if (security != null) {
154 security.checkLink(filename);
155 }
156 if (filename.indexOf(File.separatorChar) != -1) {
157 throw new UnsatisfiedLinkError(
158 "Directory separator should not appear in library name: " + filename);
159 }
160 return NativeLibraryImpl.loadLibrary(filename);
161 }
162
163 /**
164 * Loads the native library specified by the filename argument. The filename
165 * argument must be an absolute path name.
166 *
167 * @param filename the file to load.
168 * @exception SecurityException if a security manager exists and its
169 * <code>checkLink</code> method doesn't allow
170 * loading of the specified dynamic library
171 * @exception UnsatisfiedLinkError if either the filename is not an
172 * absolute path name, the native library is not statically
173 * linked with the VM, or the library cannot be mapped to
174 * a native library image by the host system.
175 * @exception NullPointerException if <code>filename</code> is
176 * <code>null</code>
177 * @see java.lang.SecurityManager#checkLink(java.lang.String)
178 */
179 public static Library load(String filename) {
180 Objects.requireNonNull(filename);
181 SecurityManager security = System.getSecurityManager();
182 if (security != null) {
183 security.checkLink(filename);
184 }
185 if (!(new File(filename).isAbsolute())) {
186 throw new UnsatisfiedLinkError(
187 "Expecting an absolute path of the library: " + filename);
188 }
189 return NativeLibraryImpl.load(filename);
190 }
191
192 public static Library getDefaultLibrary() {
193 SecurityManager security = System.getSecurityManager();
194 if (security != null) {
195 security.checkPermission(new RuntimePermission("java.nicl.getDefaultLibrary"));
196 }
197 return NativeLibraryImpl.getDefaultLibrary();
198 }
199
200 public static <T> LayoutType<T> createLayout(Class<T> c) {
201 return LayoutTypeImpl.create(c);
202 }
203
204 public static int errno() {
205 if (ERRNO == null) {
206 throw new UnsupportedOperationException();
207 }
208
209 return ERRNO.errno();
|