PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/alvermont/terraj/fracplanet/io/JarLibraryLoader.java

https://github.com/iaefai/TerraJ
Java | 289 lines | 132 code | 42 blank | 115 comment | 17 complexity | 869f512af19bfea9b6d4e7a4354e1320 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Java Terrain and Stellar System Ports
  3. *
  4. * Copyright (C) 2006 Martin H. Smith based on work by original
  5. * authors.
  6. *
  7. * Released under the terms of the GNU General Public License
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301, USA.
  23. *
  24. * Linking TerraJ statically or dynamically with other modules is making a
  25. * combined work based on TerraJ. Thus, the terms and conditions of the
  26. * GNU General Public License cover the whole combination.
  27. *
  28. * In addition, as a special exception, the copyright holders of TerraJ
  29. * give you permission to combine this program with free software programs
  30. * or libraries that are released under the GNU LGPL and with code included
  31. * in the standard release of JOGL, Java Getopt and FreeMarker under the BSD
  32. * license (or modified versions of such code, with unchanged license) and with
  33. * Apache Commons and Log4J libraries under the Apache license (or modified versions
  34. * of such code. You may copy and distribute such a system following the terms
  35. * of the GNU GPL for TerraJ and the licenses of the other code concerned,
  36. * provided that you include the source code of that other code when and as the
  37. * GNU GPL requires distribution of source code.
  38. *
  39. * Note that people who make modified versions of TerraJ are not obligated to grant
  40. * this special exception for their modified versions; it is their choice whether
  41. * to do so. The GNU General Public License gives permission to release a modified
  42. * version without this exception; this exception also makes it possible to release
  43. * a modified version which carries forward this exception.
  44. */
  45. /*
  46. * JarLibraryLoader.java
  47. *
  48. * Created on 26 April 2006, 09:05
  49. */
  50. package com.alvermont.terraj.fracplanet.io;
  51. import java.io.File;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.io.InputStream;
  55. import java.lang.reflect.Field;
  56. import org.apache.commons.logging.Log;
  57. import org.apache.commons.logging.LogFactory;
  58. /**
  59. * Attempt to load native libraries from Jar files. Some of the code for this
  60. * came from:
  61. *
  62. * http://forum.java.sun.com/thread.jspa?forumID=406&messageID=2102477&threadID=439060
  63. *
  64. * I've modified it not to be specific to Windows. It's not a brilliant solution
  65. * but there seems to be little better that can be done given the difficulty
  66. * of platform independent native library loading.
  67. *
  68. * @author martin
  69. * @version $Id: JarLibraryLoader.java,v 1.9 2006/07/06 07:46:59 martin Exp $
  70. */
  71. public class JarLibraryLoader
  72. {
  73. /** Our logger object */
  74. private static Log log = LogFactory.getLog(JarLibraryLoader.class);
  75. /** Creates a new instance of JarLibraryLoader */
  76. public JarLibraryLoader()
  77. {
  78. }
  79. /**
  80. * Get the name of a library file on this system given a base name.
  81. *
  82. * @param name The base name of the library eg jogl
  83. * @return The name of the corresponding library file eg. libjogl.so on
  84. * Linux
  85. */
  86. private static String getLibraryName(String name)
  87. {
  88. String platform = System.getProperty("os.name");
  89. String libname = "";
  90. if (platform.startsWith("Windows"))
  91. {
  92. // no prefix
  93. }
  94. else if (platform.equalsIgnoreCase("Mac OS"))
  95. {
  96. libname = "lib";
  97. }
  98. else
  99. {
  100. libname = "lib";
  101. }
  102. return libname + name + getLibraryNameSuffix();
  103. }
  104. /**
  105. * Return the suffix (extension) of a library file on this system
  106. *
  107. * @return The name of a library file on this system e.g ".so" on Unix or
  108. * ".dll" on Windows.
  109. */
  110. private static String getLibraryNameSuffix()
  111. {
  112. String platform = System.getProperty("os.name");
  113. String suffix = "";
  114. if (platform.startsWith("Windows"))
  115. {
  116. suffix = ".dll";
  117. }
  118. else if (platform.equalsIgnoreCase("Mac OS"))
  119. {
  120. suffix = ".jnlib";
  121. }
  122. else
  123. {
  124. suffix = ".so";
  125. }
  126. return suffix;
  127. }
  128. /**
  129. * Adds our extraction path to the system library path.
  130. *
  131. * @throws NoSuchFieldException if there is a reflection error
  132. * @throws IllegalAccessException if there is an access error during reflection
  133. */
  134. public static void setupPath() throws NoSuchFieldException, IllegalAccessException
  135. {
  136. String dir = System.getProperty("user.home");
  137. dir = new File(dir).getPath();
  138. String newLibPath = dir + File.pathSeparator +
  139. System.getProperty("java.library.path");
  140. System.setProperty("java.library.path", newLibPath);
  141. Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
  142. fieldSysPath.setAccessible(true);
  143. if (fieldSysPath != null)
  144. {
  145. fieldSysPath.set(System.class.getClassLoader(), null);
  146. }
  147. log.debug("Lib path: " + newLibPath);
  148. }
  149. /**
  150. * Get the name of a library resource for this platform
  151. *
  152. * @param name The name of the library
  153. * @return The resource name to use to look up the library
  154. */
  155. private static String getLibraryResourceName(String name)
  156. {
  157. String platform = System.getProperty("os.name");
  158. log.debug("Platform is: " + platform);
  159. String path = "/lib/natives_";
  160. if (platform.startsWith("Windows"))
  161. {
  162. path += "win32/" + name + ".dll";
  163. }
  164. else if (platform.equalsIgnoreCase("Mac OS"))
  165. {
  166. path += "macos/lib" + name + ".jnlib";
  167. }
  168. else
  169. {
  170. path += platform.toLowerCase() + "/lib" + name + ".so";
  171. }
  172. log.debug("Path is: " + path);
  173. return path;
  174. }
  175. /**
  176. * Extracts a file from the jar so it can be found as a library. There
  177. * is no way to load a library directly from the jar file. Uses the
  178. * users current directory to write the jar file, which might not work,
  179. * should probably fall back to the home dir. We can't use createTempFile
  180. * for this as we have to preserve the library filename so that dependencies
  181. * can be correctly resolved. The extracted file is marked for deletion on
  182. * exit.
  183. *
  184. * @param name The name of the file to be extracted
  185. * @throws IOException if there is an error extracting the library.
  186. */
  187. public static File extractLibrary(String name) throws IOException
  188. {
  189. try
  190. {
  191. // Gets a stream to the library file
  192. File temporaryDll = null;
  193. InputStream inputStream = JarLibraryLoader.class.
  194. getResourceAsStream(getLibraryResourceName(name));
  195. if (inputStream != null)
  196. {
  197. temporaryDll = new File(System.getProperty("user.home") +
  198. File.separator + name + getLibraryNameSuffix());
  199. String dir = System.getProperty("user.home");
  200. temporaryDll = new File(dir, getLibraryName(name));
  201. temporaryDll.deleteOnExit();
  202. FileOutputStream outputStream = new FileOutputStream(temporaryDll);
  203. byte[] array = new byte[8192];
  204. for (int i = inputStream.read(array); i != -1; i = inputStream.read(array))
  205. {
  206. outputStream.write(array, 0, i);
  207. }
  208. outputStream.close();
  209. }
  210. return temporaryDll;
  211. }
  212. catch (IOException ioe)
  213. {
  214. log.error("IOException loading library", ioe);
  215. throw ioe;
  216. }
  217. }
  218. /**
  219. * Loads a native library. The native library is assumed
  220. * to be in a platform specific subdirectory of the lib directory in the jar.
  221. * The library is writen to a local temporary directory and (hopefully)
  222. * deleted at JVM exit.
  223. *
  224. * Dll extraction code taken from Gabriele Piero Nizzoli
  225. * http://www.nizzoli.net/index.php?itemid=15
  226. *
  227. * @param name The base name of the library to be loaded
  228. * @throws IOException if there is a problem loading the library
  229. */
  230. public static void loadLibrary(String name) throws IOException
  231. {
  232. try
  233. {
  234. File libFile = extractLibrary(name);
  235. if (libFile != null)
  236. {
  237. log.debug("Calling System.load on: " + libFile.getPath());
  238. System.load(libFile.getPath());
  239. }
  240. }
  241. catch (IOException ioe)
  242. {
  243. log.error("IOException loading library", ioe);
  244. throw ioe;
  245. }
  246. }
  247. }