ce6d1bfc98248d975c179b2078b97e903a8b815c
[vpp.git] / vpp-api / java / jvpp-registry / io / fd / vpp / jvpp / NativeLibraryLoader.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.vpp.jvpp;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.StandardCopyOption;
24 import java.nio.file.attribute.PosixFilePermission;
25 import java.nio.file.attribute.PosixFilePermissions;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 /**
31  * Utility class for loading JNI libraries.
32  */
33 public final class NativeLibraryLoader {
34
35     private static final Logger LOG = Logger.getLogger(NativeLibraryLoader.class.getName());
36
37     private NativeLibraryLoader() {
38         throw new UnsupportedOperationException("This utility class cannot be instantiated.");
39     }
40
41     /**
42      * Loads JNI library using class loader of the given class.
43      *
44      * @param libName name of the library to be loaded
45      */
46     public static void loadLibrary(final String libName, final Class clazz) throws IOException {
47         java.util.Objects.requireNonNull(libName, "libName should not be null");
48         java.util.Objects.requireNonNull(clazz, "clazz should not be null");
49         try (final InputStream is = clazz.getResourceAsStream('/' + libName)) {
50             if (is == null) {
51                 throw new IOException("Failed to open library resource " + libName);
52             }
53             loadStream(libName, is);
54         }
55     }
56
57     private static void loadStream(final String libName, final InputStream is) throws IOException {
58         final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
59         final Path p = Files.createTempFile(libName, null, PosixFilePermissions.asFileAttribute(perms));
60         try {
61             Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
62             Runtime.getRuntime().load(p.toString());
63         } catch (Exception e) {
64             throw new IOException("Failed to load library " + p, e);
65         } finally {
66             try {
67                 Files.deleteIfExists(p);
68             } catch (IOException e) {
69                 LOG.log(Level.WARNING, String.format("Failed to delete temporary file %s.", p), e);
70             }
71         }
72     }
73 }