/**************************************************************************/ /* /* LibLoader.java /* Copyright (C) 2001 Bjorn Bringert (bjorn@mumblebee.com) /* /* This program is free software; you can redistribute it and/or /* modify it under the terms of the GNU General Public License /* as published by the Free Software Foundation; either version 2 /* of the License, or (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, /* but WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; if not, write to the Free Software /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /* /**************************************************************************/ package com.mumblebee.util; import java.io.*; import java.net.URL; /** * Utilities for loading native code from different sources. The main use * of this class is to load native code libraries for an application * without knowing where the application files reside. * Usage example: *
 * import com.mumblebee.util.LibLoader;
 * public class Foo {
 *   static {
 *     String libname = System.mapLibraryName("foo");
 *     LibLoader.load(Foo.class, libname);
 *   }
 * ...
 * 
* * @author Bjorn Bringert */ public class LibLoader { /** Prevents instantiation. */ private LibLoader() {} /** * Locates and loads a native code library. * @param cls The class relative to which the lookup should be done * @param libname The file name of the library to look for. The following changes * are made to the library file name: if the name starts with * "/", it is unchanged; otherwise, the package name och the supplied class * is prepended to the name after converting "." to "/" * @throws UnsatisfiedLinkError If there is an error locating or loading * the library */ public static void load(Class cls, String libname) { try { loadNative(cls, libname); } catch (IOException ex) { throw new UnsatisfiedLinkError(ex.toString()); } } /** * Locates and loads a native code library. * @param cls The class relative to which the lookup should be done * @param libname The file name of the library to look for. The following changes * are made to the library file name: if the name starts with * "/", it is unchanged; otherwise, the package name och the supplied class * is prepended to the name after converting "." to "/" * @throws IOException If an I/O problem occurs */ public static void loadNative(Class cls, String name) throws IOException { String libfile = findNative(cls, name); if (libfile == null) throw new IOException("native code library " + name + " not found"); System.load(libfile); } /** * Loads a native code library. * @param url The url of the library file * @throws IOException If an I/O problem occurs */ public static void loadNative(URL url) throws IOException { System.load(findNative(url)); } /** * Loads a native code library. * @param file The library file * @throws IOException If an I/O problem occurs */ public static void loadNative(File file) throws IOException { System.load(findNative(file)); } /** * Loads a native code library from an input stream. * @param in The stream to load the library from. * @throws IOException If an I/O problem occurs */ public static void loadNative(InputStream in) throws IOException { System.load(findNative(in)); } /** * Locates but does not load a native code library. * @param cls The class relative to which the lookup should be done * @param libname The file name of the library to look for. The following changes * are made to the library file name: if the name starts with * "/", it is unchanged; otherwise, the package name och the supplied class * is prepended to the name after converting "." to "/" * @return An absolute pathname that can be passed to System.load() * @throws IOException If an I/O problem occurs */ public static String findNative(Class cls, String name) throws IOException { URL url = cls.getResource(name); if (url == null) return null; return findNative(url); } /** * Gets an absolute pathname for a native code library. * @param url The url of the library file * @return An absolute pathname that can be passed to System.load() * @throws IOException If an I/O problem occurs */ private static String findNative(URL url) throws IOException { if (url.getProtocol().equals("file")) { // uses getFile() instead of getPath() // since getPath() is not available in JDK1.2.2 return new File(url.getFile()).getAbsolutePath(); } else { return findNative(url.openStream()); } } /** * Gets an absolute pathname for a native code library. * @param file The library file * @return An absolute pathname that can be passed to System.load() * @throws IOException If an I/O problem occurs */ private static String findNative(File file) throws IOException { return file.getAbsolutePath(); } /** * Gets an absolute pathname for a native code library. * @param in The stream to load the library from. * @return An absolute pathname that can be passed to System.load() * @throws IOException If an I/O problem occurs */ private static String findNative(InputStream in) throws IOException { File libfile = File.createTempFile("tmplib", null); libfile.deleteOnExit(); IOUtil.save(in, libfile, true); return libfile.getAbsolutePath(); } }