]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/util/Native.java
c793f89fd1049157dae96b0304a189bb325655f7
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / util / Native.java
1 package net.jpountz.util;
2
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 import java.io.File;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 /** FOR INTERNAL USE ONLY */
23 public enum Native {
24   ;
25
26   private enum OS {
27     // Even on Windows, the default compiler from cpptasks (gcc) uses .so as a shared lib extension
28     WINDOWS("win32", "dll"), LINUX("linux", "so"), MAC("darwin", "dylib"), SOLARIS("solaris", "so");
29     public final String name, libExtension;
30
31     private OS(String name, String libExtension) {
32       this.name = name;
33       this.libExtension = libExtension;
34     }
35   }
36
37   private static String arch() {
38     return System.getProperty("os.arch");
39   }
40
41   private static OS os() {
42     String osName = System.getProperty("os.name");
43     if (osName.contains("Linux")) {
44       return OS.LINUX;
45     } else if (osName.contains("Mac")) {
46       return OS.MAC;
47     } else if (osName.contains("Windows")) {
48       return OS.WINDOWS;
49     } else if (osName.contains("Solaris") || osName.contains("SunOS")) {
50       return OS.SOLARIS;
51     } else {
52       throw new UnsupportedOperationException("Unsupported operating system: "
53           + osName);
54     }
55   }
56
57   private static String resourceName() {
58     OS os = os();
59     String packagePrefix = Native.class.getPackage().getName().replace('.', '/');
60
61     return "/" + packagePrefix + "/" + os.name + "/" + arch() + "/liblz4-java." + os.libExtension;
62   }
63
64   private static boolean loaded = false;
65
66   public static synchronized boolean isLoaded() {
67     return loaded;
68   }
69
70   public static synchronized void load() {
71     if (loaded) {
72       return;
73     }
74     String resourceName = resourceName();
75     InputStream is = Native.class.getResourceAsStream(resourceName);
76     if (is == null) {
77       throw new UnsupportedOperationException("Unsupported OS/arch, cannot find " + resourceName + ". Please try building from source.");
78     }
79     File tempLib;
80     try {
81       tempLib = File.createTempFile("liblz4-java", "." + os().libExtension);
82       // copy to tempLib
83       FileOutputStream out = new FileOutputStream(tempLib);
84       try {
85         byte[] buf = new byte[4096];
86         while (true) {
87           int read = is.read(buf);
88           if (read == -1) {
89             break;
90           }
91           out.write(buf, 0, read);
92         }
93         try {
94           out.close();
95           out = null;
96         } catch (IOException e) {
97           // ignore
98         }
99         System.load(tempLib.getAbsolutePath());
100         loaded = true;
101       } finally {
102         try {
103           if (out != null) {
104             out.close();
105           }
106         } catch (IOException e) {
107           // ignore
108         }
109         if (tempLib != null && tempLib.exists()) {
110           if (!loaded) {
111             tempLib.delete();
112           } else {
113             // try to delete on exit, does it work on Windows?
114             tempLib.deleteOnExit();
115           }
116         }
117       }
118     } catch (IOException e) {
119         throw new ExceptionInInitializerError("Cannot unpack liblz4-java");
120     }
121   }
122
123 }