]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/src/org/simantics/fastlz/impl/OS.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.fastlz / src / org / simantics / fastlz / impl / OS.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.fastlz.impl;\r
13 \r
14 import java.io.Closeable;\r
15 import java.io.File;\r
16 import java.io.FileNotFoundException;\r
17 import java.io.FileOutputStream;\r
18 import java.io.IOException;\r
19 import java.io.InputStream;\r
20 import java.net.URL;\r
21 \r
22 import org.simantics.fastlz.ARCHType;\r
23 import org.simantics.fastlz.OSType;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public class OS {\r
29 \r
30     /**\r
31      * Extracts the specified source file in the specified bundle into the\r
32      * specified local directory.\r
33      * \r
34      * @param url the source URL to stream the resource from\r
35      * @param targetFile the target file to write the resource to\r
36      * @param deleteOnExit <code>true</code> to use {@link File#deleteOnExit()}\r
37      *        on the resulting file. Note that this does not guarantee that the\r
38      *        file is deleted when the JVM exits\r
39      * @return the resulting file\r
40      * @throws FileNotFoundException\r
41      */\r
42     public static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {\r
43         FileOutputStream os = null;\r
44         InputStream is = null;\r
45         try {\r
46             if (targetFile.exists())\r
47                 targetFile.delete();\r
48 \r
49             is = url.openStream();\r
50             int read;\r
51             byte [] buffer = new byte [16384];\r
52             os = new FileOutputStream (targetFile);\r
53             while ((read = is.read (buffer)) != -1) {\r
54                 os.write(buffer, 0, read);\r
55             }\r
56             os.close ();\r
57             is.close ();\r
58 \r
59             return targetFile;\r
60         } finally {\r
61             uncheckedClose(os);\r
62             uncheckedClose(is);\r
63         }\r
64     }\r
65 \r
66     public static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {\r
67         String tmpDirStr = System.getProperty("java.io.tmpdir");\r
68         if (tmpDirStr == null)\r
69             throw new NullPointerException("java.io.tmpdir property is null");\r
70         File tmpDir = new File(tmpDirStr);\r
71         File libFile = new File(tmpDir, libName);\r
72         return copyResource(libURL, libFile);\r
73     }\r
74 \r
75     public static void uncheckedClose(Closeable closeable) {\r
76         try {\r
77             if (closeable != null)\r
78                 closeable.close();\r
79         } catch (IOException e) {\r
80             //ignore\r
81         }\r
82     }\r
83 \r
84     public static String formOsArchSuffix() {\r
85         String osName = System.getProperty("os.name");\r
86         String osArch = System.getProperty("os.arch");\r
87         OSType os = OSType.calculate();\r
88         ARCHType arch = ARCHType.calculate();\r
89 \r
90         if (os == OSType.UNKNOWN)\r
91             throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library");\r
92         if (arch == ARCHType.UNKNOWN)\r
93             throw new UnsatisfiedLinkError("unknown architecture '" + osArch + "' cannot load native fastlz library");\r
94 \r
95         String lib = "";\r
96         switch (os) {\r
97             case APPLE:\r
98                 lib += "-darwin";\r
99                 switch (arch) {\r
100                     case PPC:\r
101                         lib += "-ppc";\r
102                         break;\r
103                     case PPC_64:\r
104                         lib += "-ppc_64";\r
105                         break;\r
106                     case X86:\r
107                         lib += "-x86";\r
108                         break;\r
109                     case X86_64:\r
110                         lib += "-x86_64";\r
111                         break;\r
112                     default:\r
113                         throw new UnsatisfiedLinkError("Unsupported architecture for Apple OS: " + osArch);\r
114                 }\r
115                 break;\r
116             case LINUX:\r
117                 lib += "-linux";\r
118                 switch (arch) {\r
119                     case X86:\r
120                         lib += "-x86";\r
121                         break;\r
122                     case X86_64:\r
123                         lib += "-x86_64";\r
124                         break;\r
125                     default:\r
126                         throw new UnsatisfiedLinkError("Unsupported architecture for Linux OS: " + osArch);\r
127                 }\r
128                 break;\r
129             case SUN:\r
130                 lib += "-sun";\r
131                 switch (arch) {\r
132                     case SPARC:\r
133                         lib += "-sparc";\r
134                         break;\r
135                     case X86_64:\r
136                         lib += "-x86_64";\r
137                         break;\r
138                     default:\r
139                         throw new UnsatisfiedLinkError("Unsupported architecture for Sun OS: " + osArch);\r
140                 }\r
141                 break;\r
142             case WINDOWS:\r
143                 lib += "-windows";\r
144                 switch (arch) {\r
145                     case X86:\r
146                         lib += "-x86";\r
147                         break;\r
148                     case X86_64:\r
149                         lib += "-x86_64";\r
150                         break;\r
151                     default:\r
152                         throw new UnsatisfiedLinkError("Unsupported architecture for Windows: " + osArch);\r
153                 }\r
154                 break;\r
155             default:\r
156                 throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
157         }\r
158         return lib;\r
159     }\r
160 \r
161     public static String resolveLibName() {\r
162         String lib = "fastlz";\r
163         lib = System.mapLibraryName(lib + formOsArchSuffix());\r
164         return lib;\r
165     }\r
166 \r
167 }\r