001    // --- BEGIN LICENSE BLOCK ---
002    /* 
003     * Copyright (c) 2009, Mikio L. Braun
004     * All rights reserved.
005     * 
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are
008     * met:
009     * 
010     *     * Redistributions of source code must retain the above copyright
011     *       notice, this list of conditions and the following disclaimer.
012     * 
013     *     * Redistributions in binary form must reproduce the above
014     *       copyright notice, this list of conditions and the following
015     *       disclaimer in the documentation and/or other materials provided
016     *       with the distribution.
017     * 
018     *     * Neither the name of the Technische Universit??t Berlin nor the
019     *       names of its contributors may be used to endorse or promote
020     *       products derived from this software without specific prior
021     *       written permission.
022     * 
023     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027     * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034     */
035    // --- END LICENSE BLOCK ---
036    package org.jblas.util;
037    
038    import java.io.*;
039    
040    /**
041     * Class which allows to load a dynamic file as resource (for example, from a 
042     * jar-file)
043     */
044    public class LibraryLoader {
045    
046        /** Find the library <tt>libname</tt> as a resource, copy it to a tempfile
047         * and load it using System.load(). The name of the library has to be the
048         * base name, it is mapped to the corresponding system name using
049         * System.mapLibraryName(). For example, the library "foo" is called "libfoo.so"
050         * under Linux and "foo.dll" under Windows, but you just have to pass "foo"
051         * the loadLibrary().
052         *
053         * I'm not quite sure if this doesn't open all kinds of security holes. Any ideas?
054         *
055         * @param libname basename of the library
056         * @throws UnsatisfiedLinkError if library cannot be founds
057         */
058        public void loadLibrary(String libname) {
059            libname = System.mapLibraryName(libname);
060    
061            // We're in a static initializer and need a class. What shall we do?
062            Class cl = getClass();
063    
064            // Trying to copy from here.
065            System.err.println("Trying to copy from /" + libname + ".");
066            InputStream is = cl.getResourceAsStream("/" + libname);
067    
068            // Trying to copy from "bin"
069            if (is == null) {
070                System.err.println("Trying to copy from /bin/" + libname + ".");
071                is = cl.getResourceAsStream("/bin/" + libname);
072            }
073    
074            // Trying to extract static version from the jar file. Why the static version?
075            // Because it is more likely to run.
076            if (is == null) {
077                System.err.println("Trying to copy from " + fatJarLibraryPath(libname, "static") + ".");
078                is = cl.getResourceAsStream(fatJarLibraryPath(libname, "static"));
079            }
080    
081            // Finally, let's see if we can get the dynamic version.
082            if (is == null) {
083                is = cl.getResourceAsStream(fatJarLibraryPath(libname, "dynamic"));
084                System.err.println("Trying to copy from " + fatJarLibraryPath(libname, "dynamic") + ".");
085            }
086    
087            // Oh man, have to get out of here!
088            if (is == null) {
089                throw new UnsatisfiedLinkError("Couldn't find the resource " + libname + ".");
090            }
091    
092            try {
093                File tempfile = File.createTempFile("jblas", libname);
094                tempfile.deleteOnExit();
095                OutputStream os = new FileOutputStream(tempfile);
096    
097                System.out.println("tempfile.getPath() = " + tempfile.getPath());
098    
099                long savedTime = System.currentTimeMillis();
100    
101                byte buf[] = new byte[1024];
102                int len;
103                while ((len = is.read(buf)) > 0) {
104                    os.write(buf, 0, len);
105                }
106    
107                double seconds = (double) (System.currentTimeMillis() - savedTime) / 1e3;
108                System.err.println("Copying took " + seconds + " seconds.");
109    
110                os.close();
111    
112                System.load(tempfile.getPath());
113            } catch (IOException io) {
114                System.err.println("Could not create the temp file: " + io.toString() + ".\n");
115            } catch (UnsatisfiedLinkError ule) {
116                System.err.println("Couldn't load copied link file: " + ule.toString() + ".\n");
117            }
118        }
119    
120        static public String unifyOSName(String osname) {
121            if (osname.startsWith("Windows")) {
122                return "Windows";
123            }
124            return osname;
125        }
126    
127        /** Compute the path to the library. The path is basically
128        "/" + os.name + "/" + os.arch + "/" + libname. */
129        static public String fatJarLibraryPath(String libname, String linkage) {
130            String sep = "/"; //System.getProperty("file.separator");
131            String os_name = unifyOSName(System.getProperty("os.name"));
132            String os_arch = System.getProperty("os.arch");
133            return sep + "lib" + sep + linkage + sep + os_name + sep + os_arch + sep + libname;
134        }
135    }