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    
037    package org.jblas.util;
038    
039    import java.io.*;
040    
041    /**
042     * Class which allows to load a dynamic file as resource (for example, from a 
043     * jar-file)
044     */
045    public class LibraryLoader {
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             */ 
057            public void loadLibrary(String libname) {
058                    libname = System.mapLibraryName(libname);
059    
060                    // We're in a static initializer and need a class. What shall we do?
061                    Class cl = getClass();
062    
063                    InputStream is = cl.getResourceAsStream("/" + libname);
064    
065            // Hm, let's see if we can find it in the build directory 
066                    if (is == null)
067                            is = cl.getResourceAsStream("/bin/" + libname);
068    
069                    if (is == null) {
070                            System.err.println("Couldn't find the resource " + libname + ".");
071                            System.exit(0);
072                    }
073    
074                    try {
075                            File tempfile = File.createTempFile("jblas", libname);
076                            tempfile.deleteOnExit();
077                            OutputStream os = new FileOutputStream(tempfile);
078    
079                            System.out.println("tempfile.getPath() = " + tempfile.getPath());
080    
081                            long savedTime = System.currentTimeMillis();
082    
083                            byte buf[] = new byte[1024];
084                            int len;
085                            while ((len = is.read(buf)) > 0) {
086                                    os.write(buf, 0, len);
087                            }
088    
089                            double seconds = (double) (System.currentTimeMillis() - savedTime) / 1e3;
090                            System.out.println("Copying took " + seconds + " seconds.");
091    
092                            os.close();
093    
094                            System.load(tempfile.getPath());
095                    } catch (IOException io) {
096                            System.err.println("Could not create the temp file: " + io.toString() + ".\n");
097                    } catch (UnsatisfiedLinkError ule) {
098                            System.err.println("Couldn't load copied link file: " + ule.toString() + ".\n");
099                    }
100            }
101    
102        /** Compute the path to the library. The path is basically
103            "/" + os.name + "/" + os.arch + "/" + libname. */
104        static public String libraryPath(String libname) {
105            return "/" + System.getProperty("os.name") + "/" + System.getProperty("os.arch") + "/" + libname;
106        }
107    
108        static public void main(String[] args) {
109            System.out.println(libraryPath(""));
110        }
111    }