diff options
author | Sven Gothel <[email protected]> | 2011-09-19 13:48:45 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-09-19 13:48:45 +0200 |
commit | 69d537e4f9e6e5d206719723094ea192ab51ef43 (patch) | |
tree | e9611865f1eec76d13fe4725dd3ad4f5023f49c7 /src | |
parent | 0711792f00e462e340e0d3731dfe71b0e8ec6022 (diff) |
Enhancement/GenericStyle:
- NativeLibrary:
- add isValidNativeLibraryName(..)
- generic style
- Platform
- add getOSAndArch(), getOSAndArch(..)
- IOUtil
- add getClassFileName(..)
- add getBasename(..)
- add getDirname(..)
- added doc
- ReflectionUtil
- generic style
Diffstat (limited to 'src')
-rwxr-xr-x | src/java/com/jogamp/common/os/NativeLibrary.java | 71 | ||||
-rw-r--r-- | src/java/com/jogamp/common/os/Platform.java | 119 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 45 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/ReflectionUtil.java | 42 |
4 files changed, 228 insertions, 49 deletions
diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java index 1df0e61..f5c3264 100755 --- a/src/java/com/jogamp/common/os/NativeLibrary.java +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -39,6 +39,7 @@ package com.jogamp.common.os; +import com.jogamp.common.util.IOUtil; import com.jogamp.gluegen.runtime.NativeLibLoader; import jogamp.common.Debug; import jogamp.common.os.MacOSXDynamicLinkerImpl; @@ -162,14 +163,14 @@ public class NativeLibrary implements DynamicLookupHelper { String macOSXLibName, boolean searchSystemPathFirst, ClassLoader loader, boolean global) { - List possiblePaths = enumerateLibraryPaths(windowsLibName, - unixLibName, - macOSXLibName, - searchSystemPathFirst, - loader); + List<String> possiblePaths = enumerateLibraryPaths(windowsLibName, + unixLibName, + macOSXLibName, + searchSystemPathFirst, + loader); // Iterate down these and see which one if any we can actually find. - for (Iterator iter = possiblePaths.iterator(); iter.hasNext(); ) { - String path = (String) iter.next(); + for (Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) { + String path = iter.next(); if (DEBUG) { System.err.println("Trying to load " + path); } @@ -232,15 +233,41 @@ public class NativeLibrary implements DynamicLookupHelper { dynLink.closeLibrary(handle); } + /** + * Comparison of prefix and suffix of the given libName's basename + * is performed case insensitive <br> + * + * @param libName the full path library name with prefix and suffix + * @param isLowerCaseAlready indicates if libName is already lower-case + * + * @return basename of libName w/o path, ie. /usr/lib/libDrinkBeer.so -> DrinkBeer on Unix systems, but null on Windows. + */ + public static String isValidNativeLibraryName(String libName, boolean isLowerCaseAlready) { + libName = IOUtil.getBasename(libName); + final String libNameLC = isLowerCaseAlready ? libName : libName.toLowerCase(); + for(int i=0; i<prefixes.length; i++) { + if (libNameLC.startsWith(prefixes[i])) { + for(int j=0; j<suffixes.length; j++) { + if (libNameLC.endsWith(suffixes[j])) { + final int s = prefixes[i].length(); + final int e = suffixes[j].length(); + return libName.substring(s, libName.length()-e); + } + } + } + } + return null; + } + /** Given the base library names (no prefixes/suffixes) for the various platforms, enumerate the possible locations and names of the indicated native library on the system. */ - private static List enumerateLibraryPaths(String windowsLibName, - String unixLibName, - String macOSXLibName, - boolean searchSystemPathFirst, - ClassLoader loader) { - List paths = new ArrayList(); + private static List<String> enumerateLibraryPaths(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader) { + List<String> paths = new ArrayList<String>(); String libName = selectName(windowsLibName, unixLibName, macOSXLibName); if (libName == null) { return paths; @@ -274,8 +301,8 @@ public class NativeLibrary implements DynamicLookupHelper { // Add entries from java.library.path String javaLibraryPath = - (String) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { + AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { return System.getProperty("java.library.path"); } }); @@ -288,8 +315,8 @@ public class NativeLibrary implements DynamicLookupHelper { // Add current working directory String userDir = - (String) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { + AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { return System.getProperty("user.dir"); } }); @@ -372,7 +399,7 @@ public class NativeLibrary implements DynamicLookupHelper { return res; } - private static void addPaths(String path, String[] baseNames, List paths) { + private static void addPaths(String path, String[] baseNames, List<String> paths) { for (int j = 0; j < baseNames.length; j++) { paths.add(path + File.separator + baseNames[j]); } @@ -384,7 +411,7 @@ public class NativeLibrary implements DynamicLookupHelper { if (loader == null) return null; if (!initializedFindLibraryMethod) { - AccessController.doPrivileged(new PrivilegedAction() { + AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { try { findLibraryMethod = ClassLoader.class.getDeclaredMethod("findLibrary", @@ -400,10 +427,10 @@ public class NativeLibrary implements DynamicLookupHelper { } if (findLibraryMethod != null) { try { - return (String) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { + return AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { try { - return findLibraryMethod.invoke(loader, new Object[] { libName }); + return (String) findLibraryMethod.invoke(loader, new Object[] { libName }); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index a1e8bd5..78daf4c 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -40,8 +40,7 @@ import jogamp.common.os.MachineDescriptionRuntime; /** * Utility class for querying platform specific properties. - * @author Michael Bien - * @author Sven Gothel + * @author Michael Bien, Sven Gothel, et. al. */ public class Platform { @@ -130,8 +129,9 @@ public class Platform { private static final MachineDescription machineDescription; + private static final String os_and_arch; + static { - // We don't seem to need an AccessController.doPrivileged() block // here as these system properties are visible even to unsigned // applets @@ -182,6 +182,8 @@ public class Platform { } OS_TYPE = getOSTypeImpl(); + os_and_arch = getOSAndArch(OS_TYPE, CPU_ARCH); + MachineDescription md = MachineDescriptionRuntime.getRuntime(); if(null == md) { MachineDescription.StaticConfig smd = MachineDescriptionRuntime.getStatic(); @@ -263,8 +265,8 @@ public class Platform { private static String getJavaRuntimeNameImpl() { // the fast path, check property Java SE instead of traversing through the ClassLoader - return (String) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { + return AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { return System.getProperty("java.runtime.name"); } }); @@ -347,6 +349,111 @@ public class Platform { } /** + * Returns the GlueGen common name for the currently running OSType and CPUType + * as implemented in the build system in 'gluegen-cpptasks-base.xml'.<br> + * + * @see #getOSAndArch(OSType, CPUType) + */ + public static String getOSAndArch() { + return os_and_arch; + } + + /** + * Returns the GlueGen common name for the given OSType and CPUType + * as implemented in the build system in 'gluegen-cpptasks-base.xml'.<br> + * + * A list of currently supported <code>os.and.arch</code> strings: + * <ul> + * <li>freebsd-i586</li> + * <li>freebsd-amd64</li> + * <li>hpux-hppa</li> + * <li>linux-amd64</li> + * <li>linux-ia64</li> + * <li>linux-i586</li> + * <li>linux-armv7</li> + * <li>android-armv7</li> + * <li>macosx-universal</li> + * <li>solaris-sparc</li> + * <li>solaris-sparcv9</li> + * <li>solaris-amd64</li> + * <li>solaris-i586</li> + * <li>windows-amd64</li> + * <li>windows-i586</li> + * </ul> + * @return + */ + public static String getOSAndArch(OSType osType, CPUType cpuType) { + String _os_and_arch; + + switch( CPU_ARCH ) { + case X86_32: + _os_and_arch = "i586"; + break; + case ARM: + _os_and_arch = "amdv7"; // TODO: sync with gluegen-cpptasks-base.xml + break; + case ARMv5: + _os_and_arch = "amdv5"; + break; + case ARMv6: + _os_and_arch = "amdv5"; + break; + case ARMv7: + _os_and_arch = "amdv7"; + break; + case SPARC_32: + _os_and_arch = "sparc"; + break; + case PPC: + _os_and_arch = "ppc"; // TODO: sync with gluegen-cpptasks-base.xml + break; + case X86_64: + _os_and_arch = "amd64"; + break; + case IA64: + _os_and_arch = "ia64"; + break; + case SPARCV9_64: + _os_and_arch = "sparcv9"; + break; + case PA_RISC2_0: + _os_and_arch = "risc2.0"; // TODO: sync with gluegen-cpptasks-base.xml + break; + default: + throw new InternalError("Complete case block"); + } + switch(OS_TYPE) { + case ANDROID: + _os_and_arch = "android-" + _os_and_arch; + break; + case MACOS: + _os_and_arch = "macosx-universal"; + break; + case WINDOWS: + _os_and_arch = "windows-" + _os_and_arch; + break; + case OPENKODE: + _os_and_arch = "openkode-" + _os_and_arch; // TODO: think about that + break; + case LINUX: + _os_and_arch = "linux-" + _os_and_arch; + break; + case FREEBSD: + _os_and_arch = "freebsd-" + _os_and_arch; + break; + case SUNOS: + _os_and_arch = "solaris-" + _os_and_arch; + break; + case HPUX: + _os_and_arch = "hpux-hppa"; // TODO: really only hppa ? + break; + default: + throw new InternalError("Complete case block"); + } + return _os_and_arch; + } + + /** * Returns the JAVA. */ public static String getJavaVendor() { @@ -411,6 +518,6 @@ public class Platform { */ public static MachineDescription getMachineDescription() { return machineDescription; - } + } } diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 79ee62a..beb3deb 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -84,6 +84,12 @@ public class IOUtil { /** * Copy the specified input stream to the specified output stream. The total * number of bytes written is returned. + * + * @param in the source + * @param out the destination + * @param totalNumBytes informal number of expected bytes, maybe used for user feedback while processing. -1 if unknown + * @return + * @throws IOException */ public static int copyStream2Stream(InputStream in, OutputStream out, int totalNumBytes) throws IOException { final byte[] buf = new byte[Platform.getMachineDescription().pageSizeInBytes()]; @@ -207,6 +213,45 @@ public class IOUtil { return toLowerCase(filename.substring(lastDot + 1)); } + public static String getClassFileName(String clazzBinName) throws IOException { + // or return clazzBinName.replace('.', File.pathSeparatorChar) + ".class"; ? + return clazzBinName.replace('.', '/') + ".class"; + } + + /** + * @param clazzBinName com.jogamp.common.util.cache.TempJarCache + * @param cl + * @return jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class + * @throws IOException + */ + public static URL getClassURL(String clazzBinName, ClassLoader cl) throws IOException { + return cl.getResource(getClassFileName(clazzBinName)); + } + + /** + * Returns the basename of the given fname w/o directory part + */ + public static String getBasename(String fname) { + fname = fname.replace('\\', '/'); // unify file seperator + int lios = fname.lastIndexOf('/'); // strip off dirname + if(lios>=0) { + fname = fname.substring(lios+1); + } + return fname; + } + + /** + * Returns unified '/' dirname including the last '/' + */ + public static String getDirname(String fname) { + fname = fname.replace('\\', '/'); // unify file seperator + int lios = fname.lastIndexOf('/'); // strip off dirname + if(lios>=0) { + fname = fname.substring(0, lios+1); + } + return fname; + } + private static String toLowerCase(String arg) { if (arg == null) { return null; diff --git a/src/java/com/jogamp/common/util/ReflectionUtil.java b/src/java/com/jogamp/common/util/ReflectionUtil.java index 7617d53..01fc736 100644 --- a/src/java/com/jogamp/common/util/ReflectionUtil.java +++ b/src/java/com/jogamp/common/util/ReflectionUtil.java @@ -45,7 +45,7 @@ public final class ReflectionUtil { public static final boolean DEBUG = Debug.debug("ReflectionUtil"); - private static final Class[] zeroTypes = new Class[0]; + private static final Class<?>[] zeroTypes = new Class[0]; /** * Returns true only if the class could be loaded. @@ -62,7 +62,7 @@ public final class ReflectionUtil { * Loads and returns the class or null. * @see Class#forName(java.lang.String, boolean, java.lang.ClassLoader) */ - public static final Class getClass(String clazzName, boolean initialize, ClassLoader cl) + public static final Class<?> getClass(String clazzName, boolean initialize, ClassLoader cl) throws JogampRuntimeException { try { return getClassImpl(clazzName, initialize, cl); @@ -71,14 +71,14 @@ public final class ReflectionUtil { } } - private static Class getClassImpl(String clazzName, boolean initialize, ClassLoader cl) throws ClassNotFoundException { + private static Class<?> getClassImpl(String clazzName, boolean initialize, ClassLoader cl) throws ClassNotFoundException { return Class.forName(clazzName, initialize, cl); } /** * @throws JogampRuntimeException if the constructor can not be delivered. */ - public static final Constructor getConstructor(String clazzName, Class[] cstrArgTypes, ClassLoader cl) + public static final Constructor<?> getConstructor(String clazzName, Class<?>[] cstrArgTypes, ClassLoader cl) throws JogampRuntimeException { try { return getConstructor(getClassImpl(clazzName, true, cl), cstrArgTypes); @@ -87,7 +87,7 @@ public final class ReflectionUtil { } } - static final String asString(Class[] argTypes) { + static final String asString(Class<?>[] argTypes) { StringBuffer args = new StringBuffer(); boolean coma = false; if(null != argTypes) { @@ -105,7 +105,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the constructor can not be delivered. */ - public static final Constructor getConstructor(Class clazz, Class ... cstrArgTypes) + public static final Constructor<?> getConstructor(Class<?> clazz, Class<?> ... cstrArgTypes) throws JogampRuntimeException { try { if(null == cstrArgTypes) { @@ -117,7 +117,7 @@ public final class ReflectionUtil { } } - public static final Constructor getConstructor(String clazzName, ClassLoader cl) + public static final Constructor<?> getConstructor(String clazzName, ClassLoader cl) throws JogampRuntimeException { return getConstructor(clazzName, null, cl); } @@ -125,7 +125,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the instance can not be created. */ - public static final Object createInstance(Constructor cstr, Object ... cstrArgs) + public static final Object createInstance(Constructor<?> cstr, Object ... cstrArgs) throws JogampRuntimeException, RuntimeException { try { @@ -148,16 +148,16 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the instance can not be created. */ - public static final Object createInstance(Class clazz, Class[] cstrArgTypes, Object ... cstrArgs) + public static final Object createInstance(Class<?> clazz, Class<?>[] cstrArgTypes, Object ... cstrArgs) throws JogampRuntimeException, RuntimeException { return createInstance(getConstructor(clazz, cstrArgTypes), cstrArgs); } - public static final Object createInstance(Class clazz, Object ... cstrArgs) + public static final Object createInstance(Class<?> clazz, Object ... cstrArgs) throws JogampRuntimeException, RuntimeException { - Class[] cstrArgTypes = null; + Class<?>[] cstrArgTypes = null; if(null!=cstrArgs) { cstrArgTypes = new Class[cstrArgs.length]; for(int i=0; i<cstrArgs.length; i++) { @@ -167,7 +167,7 @@ public final class ReflectionUtil { return createInstance(clazz, cstrArgTypes, cstrArgs); } - public static final Object createInstance(String clazzName, Class[] cstrArgTypes, Object[] cstrArgs, ClassLoader cl) + public static final Object createInstance(String clazzName, Class<?>[] cstrArgTypes, Object[] cstrArgs, ClassLoader cl) throws JogampRuntimeException, RuntimeException { try { @@ -180,7 +180,7 @@ public final class ReflectionUtil { public static final Object createInstance(String clazzName, Object[] cstrArgs, ClassLoader cl) throws JogampRuntimeException, RuntimeException { - Class[] cstrArgTypes = null; + Class<?>[] cstrArgTypes = null; if(null!=cstrArgs) { cstrArgTypes = new Class[cstrArgs.length]; for(int i=0; i<cstrArgs.length; i++) { @@ -199,7 +199,7 @@ public final class ReflectionUtil { public static final boolean instanceOf(Object obj, String clazzName) { return instanceOf(obj.getClass(), clazzName); } - public static final boolean instanceOf(Class clazz, String clazzName) { + public static final boolean instanceOf(Class<?> clazz, String clazzName) { do { if(clazz.getName().equals(clazzName)) { return true; @@ -212,11 +212,11 @@ public final class ReflectionUtil { public static final boolean implementationOf(Object obj, String faceName) { return implementationOf(obj.getClass(), faceName); } - public static final boolean implementationOf(Class clazz, String faceName) { + public static final boolean implementationOf(Class<?> clazz, String faceName) { do { - Class[] clazzes = clazz.getInterfaces(); + Class<?>[] clazzes = clazz.getInterfaces(); for(int i=clazzes.length-1; i>=0; i--) { - Class face = clazzes[i]; + Class<?> face = clazzes[i]; if(face.getName().equals(faceName)) { return true; } @@ -230,14 +230,14 @@ public final class ReflectionUtil { return instanceOf(target, "java.awt.Component"); } - public static boolean isAWTComponent(Class clazz) { + public static boolean isAWTComponent(Class<?> clazz) { return instanceOf(clazz, "java.awt.Component"); } /** * @throws JogampRuntimeException if the Method can not be found. */ - public static final Method getMethod(Class clazz, String methodName, Class ... argTypes) + public static final Method getMethod(Class<?> clazz, String methodName, Class<?> ... argTypes) throws JogampRuntimeException, RuntimeException { try { @@ -250,7 +250,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the Method can not be found. */ - public static final Method getMethod(String clazzName, String methodName, Class[] argTypes, ClassLoader cl) + public static final Method getMethod(String clazzName, String methodName, Class<?>[] argTypes, ClassLoader cl) throws JogampRuntimeException, RuntimeException { try { @@ -291,7 +291,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the instance can not be created. */ - public static final Object callStaticMethod(String clazzName, String methodName, Class[] argTypes, Object[] args, ClassLoader cl) + public static final Object callStaticMethod(String clazzName, String methodName, Class<?>[] argTypes, Object[] args, ClassLoader cl) throws JogampRuntimeException, RuntimeException { return callMethod(null, getMethod(clazzName, methodName, argTypes, cl), args); |