reworked detection of native paths to shared implementation in new class LWJGLUtil

This commit is contained in:
Brian Matzon 2005-03-28 12:44:33 +00:00
parent a39b25e3cb
commit 97f919869e
5 changed files with 167 additions and 328 deletions

View File

@ -0,0 +1,144 @@
/*
* Copyright (c) 2002-2005 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* $Id$
* <p>
* Internal library methods
* </p>
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
*/
public class LWJGLUtil {
/**
* Locates the paths required by a library.
*
* @param libNames List of library names to look for, in the form of Local Library name, Platform library name.
* At least 6 names must be passed. 2 for each supported platform in the following order: Windows, Linux, MacOSX.
* @param classloader The classloader to ask for librariy paths
* @return Paths to located libraries, if any
*/
public static String[] getLibraryPaths(String[] libNames, ClassLoader classloader) throws LWJGLException {
// need to pass path of possible locations of IL to native side
List possible_paths = new ArrayList();
String osName = System.getProperty("os.name");
String libname;
String platform_lib_name;
if (osName.startsWith("Win")) {
libname = libNames[0];
platform_lib_name = libNames[1];
} else if (osName.startsWith("Lin")) {
libname = libNames[2];
platform_lib_name = libNames[3];
} else if (osName.startsWith("Mac")) {
libname = libNames[4];
platform_lib_name = libNames[5];
} else {
throw new LWJGLException("Unknown platform: " + osName);
}
// Add all possible paths from java.library.path
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
}
String classloader_path = LWJGLUtil.getPathFromClassLoader(libname, classloader);
if (classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = LWJGLUtil.getPathFromClassLoader("lwjgl", classloader);
if (lwjgl_classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
//add cwd path
possible_paths.add(platform_lib_name);
//create needed string array
String[] paths = new String[possible_paths.size()];
possible_paths.toArray(paths);
return paths;
}
/**
* Tries to locate named library from the current ClassLoader
* This method exists because native libraries are loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. It therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
* @param libname Name of library to search for
* @param classloader Classloader to use
* @return Absolute path to library if found, otherwise null
*/
public static String getPathFromClassLoader(String libname, ClassLoader classloader) {
try {
Sys.log("getPathFromClassLoader: searching for: " + libname);
Object o = classloader;
Class c = o.getClass();
while (c != null) {
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class});
findLibrary.setAccessible(true);
Object[] arguments = new Object[] {libname};
return (String) findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
Sys.log("Failure locating " + e + " using classloader:" + e);
}
return null;
}
}

View File

@ -47,6 +47,7 @@ import java.util.StringTokenizer;
import org.lwjgl.BufferChecks;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLUtil;
/**
* $Id$
@ -585,7 +586,10 @@ public class IL {
return;
}
String[] illPaths = getILPaths();
String[] illPaths = LWJGLUtil.getLibraryPaths(new String[]{
"DevIL", "DevIL.dll",
"IL", "libIL.so",
"IL", "IL"}, IL.class.getClassLoader());
nCreate(illPaths);
created = true;
@ -621,85 +625,4 @@ public class IL {
* Native method the destroy the IL
*/
protected static native void nDestroy();
private static String[] getILPaths() throws LWJGLException {
// need to pass path of possible locations of IL to native side
List possible_paths = new ArrayList();
String osName = System.getProperty("os.name");
String libname;
String platform_lib_name;
if (osName.startsWith("Win")) {
libname = "DevIL";
platform_lib_name = "DevIL.dll";
} else if (osName.startsWith("Lin")) {
libname = "DevIL";
platform_lib_name = "libIL.so";
} else if (osName.startsWith("Mac")) {
libname = "DevIL";
platform_lib_name = "libIL.dylib";
} else {
throw new LWJGLException("Unknown platform: " + osName);
}
// Add all possible paths from java.library.path
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
}
String classloader_path = getPathFromClassLoader(libname);
if (classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl");
if (lwjgl_classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
//add cwd path
possible_paths.add(platform_lib_name);
//create needed string array
String[] ilPaths = new String[possible_paths.size()];
possible_paths.toArray(ilPaths);
return ilPaths;
}
/**
* Tries to locate Devil from the current ClassLoader
* This method exists because Devil is loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. Devil therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
* @param libname Name of library to search for
* @return Absolute path to library if found, otherwise null
*/
private static String getPathFromClassLoader(String libname) {
try {
Sys.log("getPathFromClassLoader: searching for: " + libname);
Object o = IL.class.getClassLoader();
Class c = o.getClass();
while (c != null) {
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class});
findLibrary.setAccessible(true);
Object[] arguments = new Object[] { libname};
return (String) findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
Sys.log("Failure locating DevIL using classloader:" + e);
}
return null;
}
}

View File

@ -41,6 +41,8 @@ import java.util.StringTokenizer;
import org.lwjgl.BufferChecks;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.LWJGLUtil;
/**
* $Id$
@ -168,7 +170,10 @@ public class ILU {
throw new LWJGLException("Cannot create ILU without having created IL instance");
}
String[] iluPaths = getILUPaths();
String[] iluPaths = LWJGLUtil.getLibraryPaths(new String[]{
"ILU", "ILU.dll",
"ILU", "libILU.so",
"ILU", "ILU"}, ILU.class.getClassLoader());
nCreate(iluPaths);
created = true;
@ -208,85 +213,4 @@ public class ILU {
* Native method the destroy the ILU
*/
protected static native void nDestroy();
private static String[] getILUPaths() throws LWJGLException {
// need to pass path of possible locations of ILU to native side
List possible_paths = new ArrayList();
String osName = System.getProperty("os.name");
String libname;
String platform_lib_name;
if (osName.startsWith("Win")) {
libname = "ILU";
platform_lib_name = "ILU.dll";
} else if (osName.startsWith("Lin")) {
libname = "ILU";
platform_lib_name = "libILU.so";
} else if (osName.startsWith("Mac")) {
libname = "ILU";
platform_lib_name = "libILU.dylib";
} else {
throw new LWJGLException("Unknown platform: " + osName);
}
// Add all possible paths from java.library.path
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
}
String classloader_path = getPathFromClassLoader(libname);
if (classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl");
if (lwjgl_classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
//add cwd path
possible_paths.add(platform_lib_name);
//create needed string array
String[] iluPaths = new String[possible_paths.size()];
possible_paths.toArray(iluPaths);
return iluPaths;
}
/**
* Tries to locate ILU from the current ClassLoader
* This method exists because ILU is loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. ILU therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
* @param libname Name of library to search for
* @return Absolute path to library if found, otherwise null
*/
private static String getPathFromClassLoader(String libname) {
try {
Sys.log("getPathFromClassLoader: searching for: " + libname);
Object o = ILU.class.getClassLoader();
Class c = o.getClass();
while (c != null) {
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class});
findLibrary.setAccessible(true);
Object[] arguments = new Object[] { libname};
return (String) findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
Sys.log("Failure locating ILU using classloader:" + e);
}
return null;
}
}

View File

@ -41,6 +41,7 @@ import java.util.StringTokenizer;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLUtil;
/**
* $Id$
@ -133,7 +134,10 @@ public class ILUT {
throw new LWJGLException("Cannot create ILUT without having created IL instance");
}
String[] ilutPaths = getILUTPaths();
String[] ilutPaths = LWJGLUtil.getLibraryPaths(new String[]{
"ILUT", "ILUT.dll",
"ILUT", "libILUT.so",
"ILUT", "IL"}, ILU.class.getClassLoader());
nCreate(ilutPaths);
created = true;
@ -173,85 +177,4 @@ public class ILUT {
* Native method the destroy the ILUT
*/
protected static native void nDestroy();
private static String[] getILUTPaths() throws LWJGLException {
// need to pass path of possible locations of IL to native side
List possible_paths = new ArrayList();
String osName = System.getProperty("os.name");
String libname;
String platform_lib_name;
if (osName.startsWith("Win")) {
libname = "ILUT";
platform_lib_name = "ILUT.dll";
} else if (osName.startsWith("Lin")) {
libname = "ILUT";
platform_lib_name = "libILUT.so";
} else if (osName.startsWith("Mac")) {
libname = "ILUT";
platform_lib_name = "libILUT.dylib";
} else {
throw new LWJGLException("Unknown platform: " + osName);
}
// Add all possible paths from java.library.path
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
}
String classloader_path = getPathFromClassLoader(libname);
if (classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl");
if (lwjgl_classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
//add cwd path
possible_paths.add(platform_lib_name);
//create needed string array
String[] ilutPaths = new String[possible_paths.size()];
possible_paths.toArray(ilutPaths);
return ilutPaths;
}
/**
* Tries to locate ILUT from the current ClassLoader
* This method exists because ILUT is loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. ILUT therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
* @param libname Name of library to search for
* @return Absolute path to library if found, otherwise null
*/
private static String getPathFromClassLoader(String libname) {
try {
Sys.log("getPathFromClassLoader: searching for: " + libname);
Object o = ILUT.class.getClassLoader();
Class c = o.getClass();
while (c != null) {
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class});
findLibrary.setAccessible(true);
Object[] arguments = new Object[] { libname};
return (String) findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
Sys.log("Failure locating ILUT using classloader:" + e);
}
return null;
}
}

View File

@ -40,6 +40,9 @@ import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.devil.ILU;
import org.lwjgl.LWJGLUtil;
/**
* $Id$
@ -131,56 +134,6 @@ public final class AL {
create();
}
private static String[] getOALPaths() throws LWJGLException {
// need to pass path of possible locations of OAL to native side
List possible_paths = new ArrayList();
String osName = System.getProperty("os.name");
String libname;
String platform_lib_name;
if (osName.startsWith("Win")) {
libname = "lwjglaudio";
platform_lib_name = "lwjglaudio.dll";
} else if (osName.startsWith("Lin")) {
libname = "openal";
platform_lib_name = "libopenal.so";
} else if (osName.startsWith("Mac")) {
libname = "openal";
platform_lib_name = "openal.dylib";
} else {
throw new LWJGLException("Unknown platform: "+osName);
}
// Add all possible paths from java.library.path
String java_library_path = System.getProperty("java.library.path");
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
}
String classloader_path = getPathFromClassLoader(libname);
if (classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl");
if (lwjgl_classloader_path != null) {
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
+ File.separator + platform_lib_name);
}
//add cwd path
possible_paths.add(platform_lib_name);
//create needed string array
String[] oalPaths = new String[possible_paths.size()];
possible_paths.toArray(oalPaths);
return oalPaths;
}
/**
* Creates an OpenAL instance. The empty create will cause OpenAL to
* open the default device, and create a context using default values.
@ -189,7 +142,10 @@ public final class AL {
if (created) {
return;
}
String[] oalPaths = getOALPaths();
String[] oalPaths = LWJGLUtil.getLibraryPaths(new String[]{
"lwjglaudio", "lwjglaudio.dll",
"openal", "openal.so",
"openal", "openal.dylib"}, AL.class.getClassLoader());
nCreate(oalPaths);
created = true;
@ -240,37 +196,6 @@ public final class AL {
nDestroy();
created = false;
}
/**
* Tries to locate OpenAL from the current ClassLoader
* This method exists because OpenAL is loaded from native code, and as such
* is exempt from ClassLoader library loading rutines. OpenAL therefore always fails.
* We therefore invoke the protected method of the ClassLoader to see if it can
* locate it.
*
* @param libname Name of library to search for
* @return Absolute path to library if found, otherwise null
*/
private static String getPathFromClassLoader(String libname) {
try {
Sys.log("getPathFromClassLoader: searching for: " + libname);
Object o = AL.class.getClassLoader();
Class c = o.getClass();
while (c != null) {
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class });
findLibrary.setAccessible(true);
Object[] arguments = new Object[] { libname };
return (String)findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
Sys.log("Failure locating OpenAL using classloader:" + e);
}
return null;
}
private static native void resetNativeStubs(Class clazz);