Class LibraryLoader


  • public class LibraryLoader
    extends java.lang.Object
    Class which allows to load a dynamic file as resource (for example, from a jar-file)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String libpath  
      private Logger logger  
      private static java.io.File tempDir  
    • Constructor Summary

      Constructors 
      Constructor Description
      LibraryLoader()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.io.File createTempFile​(java.lang.String name)  
      private java.lang.String fatJarLibraryPath​(java.lang.String linkage, java.lang.String flavor)
      Compute the path to the library.
      private java.io.InputStream findLibrary​(java.lang.String[] paths, java.lang.String libname)  
      private static java.lang.String getUnifiedOSName()  
      void loadLibrary​(java.lang.String libname, boolean withFlavor)
      Find the library libname as a resource, copy it to a tempfile and load it using System.load().
      private void loadLibraryFromStream​(java.lang.String libname, java.io.InputStream is)
      Load a system library from a stream.
      private java.io.InputStream tryPath​(java.lang.String path)
      Try to open a file at the given position.
      private static java.lang.String unifyOSName​(java.lang.String osname)
      Translate all those Windows to "Windows".
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private Logger logger
      • libpath

        private java.lang.String libpath
      • tempDir

        private static java.io.File tempDir
    • Constructor Detail

      • LibraryLoader

        public LibraryLoader()
    • Method Detail

      • loadLibrary

        public void loadLibrary​(java.lang.String libname,
                                boolean withFlavor)

        Find the library libname as a resource, copy it to a tempfile and load it using System.load(). The name of the library has to be the base name, it is mapped to the corresponding system name using System.mapLibraryName(). For example, the library "foo" is called "libfoo.so" under Linux and "foo.dll" under Windows, but you just have to pass "foo" the loadLibrary().

        I'm not quite sure if this doesn't open all kinds of security holes. Any ideas?

        This function reports some more information to the "org.jblas" logger at the FINE level.

        Parameters:
        libname - basename of the library
        Throws:
        java.lang.UnsatisfiedLinkError - if library cannot be founds
      • findLibrary

        private java.io.InputStream findLibrary​(java.lang.String[] paths,
                                                java.lang.String libname)
      • unifyOSName

        private static java.lang.String unifyOSName​(java.lang.String osname)
        Translate all those Windows to "Windows". ("Windows XP", "Windows Vista", "Windows 7", etc.)
      • getUnifiedOSName

        private static java.lang.String getUnifiedOSName()
      • fatJarLibraryPath

        private java.lang.String fatJarLibraryPath​(java.lang.String linkage,
                                                   java.lang.String flavor)
        Compute the path to the library. The path is basically "/" + os.name + "/" + os.arch + "/" + libname.
      • tryPath

        private java.io.InputStream tryPath​(java.lang.String path)
        Try to open a file at the given position.
      • createTempFile

        private java.io.File createTempFile​(java.lang.String name)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • loadLibraryFromStream

        private void loadLibraryFromStream​(java.lang.String libname,
                                           java.io.InputStream is)
        Load a system library from a stream. Copies the library to a temp file and loads from there.
        Parameters:
        libname - name of the library (just used in constructing the library name)
        is - InputStream pointing to the library