Made sure no dependency from core LWJGL to LWJGLInstaller exists

This commit is contained in:
Elias Naur 2006-07-03 10:21:44 +00:00
parent 5a83c8de35
commit 85ca7c65bb
6 changed files with 93 additions and 130 deletions

View File

@ -42,7 +42,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.lwjgl.applet.LWJGLInstaller;
/**
* <p>
@ -280,7 +279,7 @@ public class LWJGLUtil {
* @return the current platform type
*/
public static int getPlatform() {
String osName = System.getProperty("os.name");
String osName = getPrivilegedProperty("os.name");
if (osName.startsWith("Windows")) {
return PLATFORM_WINDOWS;
@ -358,16 +357,13 @@ public class LWJGLUtil {
}
// add Installer path
if (LWJGLInstaller.installed) {
possible_paths.add(LWJGLInstaller.installDirectory + File.separator + platform_lib_name);
String alternative_path = getPrivilegedProperty("org.lwjgl.librarypath");
if (alternative_path != null) {
possible_paths.add(alternative_path + File.separator + platform_lib_name);
}
// Add all possible paths from java.library.path
String java_library_path = (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("java.library.path");
}
});
String java_library_path = getPrivilegedProperty("java.library.path");
StringTokenizer st = new StringTokenizer(java_library_path, File.pathSeparator);
while (st.hasMoreTokens()) {
@ -376,11 +372,7 @@ public class LWJGLUtil {
}
//add current path
String current_dir = (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("user.dir");
}
});
String current_dir = getPrivilegedProperty("user.dir");
possible_paths.add(current_dir + File.separator + platform_lib_name);
//add pure library (no path, let OS search)
@ -393,6 +385,14 @@ public class LWJGLUtil {
return paths;
}
private static String getPrivilegedProperty(final String property_name) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(property_name);
}
});
}
/**
* Tries to locate named library from the current ClassLoader
* This method exists because native libraries are loaded from native code, and as such
@ -460,7 +460,7 @@ public class LWJGLUtil {
* specific code and will not work for any other platform.
*/
public static boolean isMacOSXEqualsOrBetterThan(int major_required, int minor_required) {
String os_version = System.getProperty("os.version");
String os_version = getPrivilegedProperty("os.version");
StringTokenizer version_tokenizer = new StringTokenizer(os_version, ".");
int major;
int minor;

View File

@ -39,7 +39,6 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import org.lwjgl.applet.LWJGLInstaller;
import org.lwjgl.input.Mouse;
/**
@ -58,31 +57,28 @@ public final class Sys {
/** The implementation instance to delegate platform specific behavior to */
private final static SysImplementation implementation;
/**
* utility loadlibrary to load the native library using elevated priviledges
* @param name Name of library to load, or full path if usingPath is true
* @param usingPath true if using the full path to the native
*/
private static void loadLibrary(final String name, final boolean usingPath) {
private static void loadLibrary(final String lib_name) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if(usingPath) {
System.load(name);
String library_path = System.getProperty("org.lwjgl.librarypath");
if (library_path != null) {
System.load(library_path + File.separator +
System.mapLibraryName(lib_name));
} else {
System.loadLibrary(name);
System.loadLibrary(lib_name);
}
return null;
}
});
}
}
static {
implementation = createImplementation();
String[] library_names = implementation.getNativeLibraryNames();
UnsatisfiedLinkError last_load_error = null;
for (int i = 0; i < library_names.length; i++) {
try {
loadLibrary(library_names[i], false);
loadLibrary(library_names[i]);
last_load_error = null;
break;
} catch (UnsatisfiedLinkError e) {
@ -90,19 +86,6 @@ public final class Sys {
}
}
// failed normal loading - check installer
if (last_load_error != null && LWJGLInstaller.installed) {
for (int i = 0; i < library_names.length; i++) {
try {
loadLibrary(LWJGLInstaller.installDirectory + File.separator + System.mapLibraryName(library_names[i]), true);
last_load_error = null;
break;
} catch (UnsatisfiedLinkError e) {
last_load_error = e;
}
}
}
// check for error
if (last_load_error != null) {
throw last_load_error;

View File

@ -41,8 +41,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
/**
@ -71,9 +71,6 @@ public class LWJGLInstaller {
/** Whether to hook uninstall rutine. Must be called prior to installation */
public static boolean disableUninstall = false;
/** Directory that was installed into */
public static String installDirectory;
/** Buffer used when copying files */
private static final byte[] COPY_BUFFER = new byte[4096];
@ -94,53 +91,55 @@ public class LWJGLInstaller {
* will always be present in the users temp dir.
*
* @see java.lang.ClassLoader#getResource(String)
* @return true if the installation was successfull
*/
public static boolean tempInstall() throws LWJGLException {
public static void tempInstall() throws Exception {
// only need to install once
if (installed) {
return true;
return;
}
// libraries to validate and install
String[] libraries = PLATFORM_FILES[LWJGLUtil.getPlatform() - 1];
// Validate the certificates of the native files
validateCertificates();
// install shutdown installer hook
if(!disableUninstall) {
try {
// libraries to validate and install
String[] libraries = PLATFORM_FILES[LWJGLUtil.getPlatform() - 1];
// Validate the certificates of the native files
validateCertificates();
// install shutdown installer hook
if(!disableUninstall) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
uninstall();
}
});
return null;
}
});
}
// create a temporary dir for the native files
String user_temp_dir = getPriviledgedString("java.io.tmpdir");
final String path = createTemporaryDir(user_temp_dir);
// extract natives
for (int i = 0; i < libraries.length; i++) {
String library = System.mapLibraryName(libraries[i]);
extract(library, path);
}
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
LWJGLInstaller.uninstall();
}
});
System.setProperty("org.lwjgl.librarypath", path);
return null;
}
});
} catch (Exception e) {
LWJGLUtil.log("Failed extraction e = " + e.getMessage());
uninstall();
throw e;
}
// create a temporary dir for the native files
String user_temp_dir = getPriviledgedString("java.io.tmpdir");
String path = createTemporaryDir(user_temp_dir);
if(path == null) {
throw new LWJGLException("Failed creation of temporary directory in " + user_temp_dir);
}
// extract natives
for (int i = 0; i < libraries.length; i++) {
String library = System.mapLibraryName(libraries[i]);
if(!extract(library, path)) {
LWJGLUtil.log("Failed extract of " + library + " to " + path);
uninstall();
return false;
}
}
installDirectory = path;
return installed = true;
}
/**
@ -150,9 +149,9 @@ public class LWJGLInstaller {
* before the "real" LWJGL jar, containing native libraries with unwanted code.
* By forcing all the native libraries to have the same certificate as the signed
* installer, we can also be sure that the native libraries indeed are correct.
* @throws LWJGLException If we encounter a certificate mismatch
* @throws Exception If we encounter a certificate mismatch
*/
private static void validateCertificates() throws LWJGLException {
private static void validateCertificates() throws Exception {
/* TODO */
}
@ -161,15 +160,14 @@ public class LWJGLInstaller {
*
* @param file File to extract
* @param path Path to extract to
* @return true if the file was extracted successdully
*/
private static boolean extract(final String file, final String path) throws LWJGLException {
return (Boolean) AccessController.doPrivileged(new PrivilegedAction() {
private static void extract(final String file, final String path) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
// check for existing file, and get out
File out = new File(path + File.separator + file);
if (out.exists()) {
return false;
return null;
}
// create the new file and copy it to its destination
@ -183,12 +181,12 @@ public class LWJGLInstaller {
// ===========================================
if (os == null) {
LWJGLUtil.log("Unable to write to outputstream at " + out.getAbsolutePath());
return false;
return null;
}
if (is == null) {
LWJGLUtil.log("Unable to read classpath inputstream from " + in);
return false;
return null;
}
// -------------------------------------------
@ -196,9 +194,9 @@ public class LWJGLInstaller {
copyFile(is, os);
} catch (IOException ioe) {
LWJGLUtil.log("Exception while extracting " + file + ": " + ioe.getMessage());
return false;
return null;
}
return true;
return null;
}
});
}
@ -225,23 +223,19 @@ public class LWJGLInstaller {
* called '.lwjglinstaller' will also be created in the directory.
* @return Name of temp directory or null if directory creation failed
*/
static String createTemporaryDir(final String user_temp_dir) {
return (String) AccessController.doPrivileged(new PrivilegedAction() {
static String createTemporaryDir(final String user_temp_dir) throws Exception {
return (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
// create the temp directory
File tempDir = new File(user_temp_dir + File.separator + "lwjgl-" + System.currentTimeMillis());
if(!tempDir.mkdir()) {
return null;
throw new IOException("Failed to create directory: " + tempDir);
}
// add the watermark file
// TODO: Write some info to the file ?
try {
File watermark = new File(tempDir.getAbsolutePath() + File.separator + ".lwjglinstaller");
watermark.createNewFile();
} catch (IOException ioe) {
return null;
}
File watermark = new File(tempDir.getAbsolutePath() + File.separator + ".lwjglinstaller");
watermark.createNewFile();
return tempDir.getAbsolutePath();
}
});

View File

@ -37,7 +37,6 @@ import java.security.PrivilegedAction;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.applet.LWJGLInstaller;
/**
* <p>
@ -56,30 +55,23 @@ class ILNative {
/** Version of IL */
public static final String VERSION = "1.0beta2";
/**
* utility loadlibrary to load the native library using elevated priviledges
* @param name Name of library to load, or full path if usingPath is true
* @param usingPath true if using the full path to the native
*/
private static void loadLibrary(final String name, final boolean usingPath) {
private static void loadLibrary(final String lib_name) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if(usingPath) {
System.load(name);
String library_path = System.getProperty("org.lwjgl.librarypath");
if (library_path != null) {
System.load(library_path + File.separator +
System.mapLibraryName(lib_name));
} else {
System.loadLibrary(name);
System.loadLibrary(lib_name);
}
return null;
}
});
}
}
static {
if (LWJGLInstaller.installed) {
loadLibrary(LWJGLInstaller.installDirectory + File.separator + System.mapLibraryName(JNI_LIBRARY_NAME), true);
} else {
loadLibrary(JNI_LIBRARY_NAME, false);
}
loadLibrary(JNI_LIBRARY_NAME);
// check for mismatch
String nativeVersion = getNativeLibraryVersion();

View File

@ -41,7 +41,6 @@ import java.util.HashMap;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.applet.LWJGLInstaller;
/**
* <br>
@ -209,11 +208,7 @@ public class FMOD {
}
initialized = true;
if (LWJGLInstaller.installed) {
loadLibrary(LWJGLInstaller.installDirectory + File.separator + System.mapLibraryName(JNI_LIBRARY_NAME), true);
} else {
loadLibrary(JNI_LIBRARY_NAME, false);
}
loadLibrary(JNI_LIBRARY_NAME);
// check for mismatch
String nativeVersion = getNativeLibraryVersion();
@ -229,13 +224,15 @@ public class FMOD {
}
}
private static void loadLibrary(final String name, final boolean usingPath) {
private static void loadLibrary(final String lib_name) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if(usingPath) {
System.load(name);
String library_path = System.getProperty("org.lwjgl.librarypath");
if (library_path != null) {
System.load(library_path + File.separator +
System.mapLibraryName(lib_name));
} else {
System.loadLibrary(name);
System.loadLibrary(lib_name);
}
return null;
}

View File

@ -44,19 +44,16 @@ public class AppletTest extends Applet {
Test test = null;
@Override
public void destroy() {
super.destroy();
System.out.println("*** destroy ***");
}
@Override
public void start() {
super.start();
System.out.println("*** start ***");
}
@Override
public void stop() {
super.stop();
System.out.println("*** stop ***");