Added AccessController.doPrivileged where needed

This commit is contained in:
Elias Naur 2005-05-30 16:21:05 +00:00
parent fd36f96df9
commit f1705b7fde
10 changed files with 210 additions and 76 deletions

View File

@ -37,6 +37,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
/**
* $Id$
* <p>
@ -52,8 +57,8 @@ public class LWJGLUtil {
public static final int PLATFORM_WINDOWS = 3;
/** Debug flag. */
public static final boolean DEBUG = Boolean.getBoolean("org.lwjgl.util.Debug");
public static final boolean DEBUG = getPrivilegedBoolean("org.lwjgl.util.Debug");
/**
* Get the current platform
*/
@ -102,15 +107,15 @@ public class LWJGLUtil {
throw new LWJGLException("Unknown platform: " + getPlatform());
}
String classloader_path = LWJGLUtil.getPathFromClassLoader(libname, classloader);
String classloader_path = getPathFromClassLoader(libname, classloader);
if (classloader_path != null) {
LWJGLUtil.log("getPathFromClassLoader: Path found: " + classloader_path);
log("getPathFromClassLoader: Path found: " + classloader_path);
possible_paths.add(classloader_path);
}
String lwjgl_classloader_path = LWJGLUtil.getPathFromClassLoader("lwjgl", classloader);
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl", classloader);
if (lwjgl_classloader_path != null) {
LWJGLUtil.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
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);
}
@ -119,7 +124,12 @@ public class LWJGLUtil {
possible_paths.add(platform_lib_name);
// Add all possible paths from java.library.path
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
String java_library_path = (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("java.library.path");
}
});
StringTokenizer st = new StringTokenizer(java_library_path, File.pathSeparator);
while (st.hasMoreTokens()) {
String path = st.nextToken();
possible_paths.add(path + File.separator + platform_lib_name);
@ -145,32 +155,51 @@ public class LWJGLUtil {
*/
public static String getPathFromClassLoader(String libname, ClassLoader classloader) {
try {
LWJGLUtil.log("getPathFromClassLoader: searching for: " + libname);
log("getPathFromClassLoader: searching for: " + libname);
Object o = classloader;
Class c = o.getClass();
while (c != null) {
final Class clazz = c;
try {
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class});
findLibrary.setAccessible(true);
Method findLibrary = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
Method m = clazz.getDeclaredMethod("findLibrary", new Class[]{String.class});
m.setAccessible(true);
return m;
}
});
Object[] arguments = new Object[] {libname};
return (String) findLibrary.invoke(o, arguments);
} catch (NoSuchMethodException e) {
return (String)findLibrary.invoke(o, arguments);
} catch (PrivilegedActionException e) {
c = c.getSuperclass();
}
}
} catch (Exception e) {
LWJGLUtil.log("Failure locating " + e + " using classloader:" + e);
log("Failure locating " + e + " using classloader:" + e);
}
return null;
}
/**
* Gets a boolean property as a privileged action. Helper method
* for native.
*/
private static boolean getPrivilegedBoolean(final String property_name) {
Boolean value = (Boolean)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new Boolean(Boolean.getBoolean(property_name));
}
});
return value.booleanValue();
}
/**
* Prints the given message to System.err if DEBUG is true.
*
* @param msg Message to print
*/
public static void log(String msg) {
if (LWJGLUtil.DEBUG) {
if (DEBUG) {
System.err.println(msg);
}
}

View File

@ -33,6 +33,10 @@ package org.lwjgl;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
/**
* $Id$
*
@ -44,17 +48,23 @@ class LinuxSysImplementation extends J2SESysImplementation {
java.awt.Toolkit.getDefaultToolkit(); // This will make sure libjawt.so is loaded
}
public boolean openURL(String url) {
public boolean openURL(final String url) {
// Linux may as well resort to pure Java hackery, as there's no Linux native way of doing it
// right anyway.
String[] browsers = {"firefox", "mozilla", "opera", "konqueror", "nautilus", "galeon", "netscape"};
for (int i = 0; i < browsers.length; i ++) {
final String browser = browsers[i];
try {
Runtime.getRuntime().exec(new String[] { browsers[i], url });
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
Runtime.getRuntime().exec(new String[] { browser, url });
return null;
}
});
return true;
} catch (IOException e) {
} catch (PrivilegedActionException e) {
// Ignore
e.printStackTrace(System.err);
}

View File

@ -33,6 +33,9 @@ package org.lwjgl;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
/**
* $Id$
*
@ -42,11 +45,21 @@ import java.lang.reflect.Method;
class MacOSXSysImplementation extends J2SESysImplementation {
public boolean openURL(String url) {
try {
Class com_apple_eio_FileManager = Class.forName("com.apple.eio.FileManager");
Method openURL_method = com_apple_eio_FileManager.getMethod("openURL", new Class[]{String.class});
Method openURL_method = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
try {
Class com_apple_eio_FileManager = Class.forName("com.apple.eio.FileManager");
return com_apple_eio_FileManager.getMethod("openURL", new Class[]{String.class});
} catch (Exception e) {
LWJGLUtil.log("Exception occurred while trying to invoke browser: " + e);
return null;
}
}
});
openURL_method.invoke(null, new Object[]{url});
return true;
} catch (Exception e) {
LWJGLUtil.log("Exception occurred while trying to invoke browser: " + e);
return false;
}
}

View File

@ -37,6 +37,10 @@ import java.net.URL;
import org.lwjgl.input.Mouse;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
/**
* $Id$
* <p>
@ -58,7 +62,12 @@ public final class Sys {
static {
implementation = createImplementation();
System.loadLibrary(LIBRARY_NAME);
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
System.loadLibrary(LIBRARY_NAME);
return null;
}
});
String native_version = implementation.getNativeLibraryVersion();
if (!native_version.equals(VERSION))
throw new LinkageError("Version mismatch: jar version is '" + VERSION +
@ -176,11 +185,19 @@ public final class Sys {
// Attempt to use Webstart if we have it available
try {
// Lookup the javax.jnlp.BasicService object
Class serviceManagerClass = Class.forName("javax.jnlp.ServiceManager");
Method lookupMethod = serviceManagerClass.getMethod("lookup", new Class[] {String.class});
final Class serviceManagerClass = Class.forName("javax.jnlp.ServiceManager");
Method lookupMethod = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
return serviceManagerClass.getMethod("lookup", new Class[] {String.class});
}
});
Object basicService = lookupMethod.invoke(serviceManagerClass, new Object[] {"javax.jnlp.BasicService"});
Class basicServiceClass = Class.forName("javax.jnlp.BasicService");
Method showDocumentMethod = basicServiceClass.getMethod("showDocument", new Class[] {URL.class});
final Class basicServiceClass = Class.forName("javax.jnlp.BasicService");
Method showDocumentMethod = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
return basicServiceClass.getMethod("showDocument", new Class[] {URL.class});
}
});
try {
Boolean ret = (Boolean) showDocumentMethod.invoke(basicService, new Object[] {new URL(url)});
return ret.booleanValue();

View File

@ -47,6 +47,9 @@ import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.HashSet;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
@ -98,9 +101,14 @@ public final class Display {
try {
current_mode = initial_mode = display_impl.init();
LWJGLUtil.log("Initial mode: " + initial_mode);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
reset();
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
reset();
}
});
return null;
}
});
} catch (LWJGLException e) {
@ -656,10 +664,22 @@ public final class Display {
return display_impl;
}
/**
* Gets a boolean property as a privileged action.
*/
static boolean getPrivilegedBoolean(final String property_name) {
Boolean value = (Boolean)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new Boolean(Boolean.getBoolean(property_name));
}
});
return value.booleanValue();
}
private static void initControls() {
// Automatically create mouse, keyboard and controller
if (!Boolean.getBoolean("org.lwjgl.opengl.Display.noinput")) {
if (!Mouse.isCreated() && !Boolean.getBoolean("org.lwjgl.opengl.Display.nomouse")) {
if (!getPrivilegedBoolean("org.lwjgl.opengl.Display.noinput")) {
if (!Mouse.isCreated() && !getPrivilegedBoolean("org.lwjgl.opengl.Display.nomouse")) {
try {
Mouse.create();
} catch (LWJGLException e) {
@ -670,7 +690,7 @@ public final class Display {
}
}
}
if (!Keyboard.isCreated() && !Boolean.getBoolean("org.lwjgl.opengl.Display.nokeyboard")) {
if (!Keyboard.isCreated() && !getPrivilegedBoolean("org.lwjgl.opengl.Display.nokeyboard")) {
try {
Keyboard.create();
} catch (LWJGLException e) {

View File

@ -38,6 +38,11 @@ import java.util.Set;
import java.util.StringTokenizer;
import java.util.WeakHashMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.Sys;
@ -93,7 +98,11 @@ public final class GLContext {
* with a name dependent on the current platform
*/
static long getPlatformSpecificFunctionAddress(String function_prefix, String[] os_prefixes, String[] os_function_prefixes, String function) {
String os_name = System.getProperty("os.name");
String os_name = (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("os.name");
}
});
for (int i = 0; i < os_prefixes.length; i++)
if (os_name.startsWith(os_prefixes[i])) {
String platform_function_name = function.replaceFirst(function_prefix, os_function_prefixes[i]);
@ -167,12 +176,17 @@ public final class GLContext {
* Helper method to ContextCapabilities. It will try to initialize the native stubs,
* and remove the given extension name from the extension set if the initialization fails.
*/
static void initNativeStubs(Class extension_class, Set supported_extensions, String ext_name) {
static void initNativeStubs(final Class extension_class, Set supported_extensions, String ext_name) {
resetNativeStubs(extension_class);
if (supported_extensions.contains(ext_name)) {
try {
Method init_stubs_method = extension_class.getDeclaredMethod("initNativeStubs", null);
init_stubs_method.invoke(null, null);
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
Method init_stubs_method = extension_class.getDeclaredMethod("initNativeStubs", null);
init_stubs_method.invoke(null, null);
return null;
}
});
} catch (Exception e) {
LWJGLUtil.log("Failed to initialize extension " + extension_class + " - exception: " + e);
supported_extensions.remove(ext_name);

View File

@ -35,6 +35,9 @@ import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
@ -45,9 +48,13 @@ import org.lwjgl.LWJGLUtil;
* @version $Revision$
*/
final class LinuxCanvasImplementation implements AWTCanvasImplementation {
static int getScreenFromDevice(GraphicsDevice device) throws LWJGLException {
static int getScreenFromDevice(final GraphicsDevice device) throws LWJGLException {
try {
Method getScreen_method = device.getClass().getMethod("getScreen", null);
Method getScreen_method = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
return device.getClass().getMethod("getScreen", null);
}
});
Integer screen = (Integer)getScreen_method.invoke(device, null);
return screen.intValue();
} catch (Exception e) {
@ -55,9 +62,13 @@ final class LinuxCanvasImplementation implements AWTCanvasImplementation {
}
}
private static int getVisualIDFromConfiguration(GraphicsConfiguration configuration) throws LWJGLException {
private static int getVisualIDFromConfiguration(final GraphicsConfiguration configuration) throws LWJGLException {
try {
Method getVisual_method = configuration.getClass().getMethod("getVisual", null);
Method getVisual_method = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
return configuration.getClass().getMethod("getVisual", null);
}
});
Integer visual = (Integer)getVisual_method.invoke(configuration, null);
return visual.intValue();
} catch (Exception e) {

View File

@ -57,6 +57,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.input.Keyboard;
@ -96,9 +101,13 @@ final class MacOSXDisplay implements DisplayImplementation {
public void destroyWindow() {
if (frame != null) {
if (MacOSXFrame.getDevice().getFullScreenWindow() == frame)
MacOSXFrame.getDevice().setFullScreenWindow(null);
// setView(null);
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
if (MacOSXFrame.getDevice().getFullScreenWindow() == frame)
MacOSXFrame.getDevice().setFullScreenWindow(null);
return null;
}
});
if (frame.isDisplayable())
frame.dispose();
frame = null;
@ -450,26 +459,24 @@ final class MacOSXDisplay implements DisplayImplementation {
public MacOSXApplicationListener() {
try {
/* Get the com.apple.eawt.Application class */
Class com_apple_eawt_Application = Class.forName("com.apple.eawt.Application");
/* Call the static Application.getApplication() method */
Object application = com_apple_eawt_Application.getMethod("getApplication", null).invoke(null, null);
/* Create a proxy implementing com.apple.eawt.ApplicationListener */
Class com_apple_eawt_ApplicationListener = Class.forName("com.apple.eawt.ApplicationListener");
Object listener_proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {com_apple_eawt_ApplicationListener}, this);
/* Invoke the method application.addApplicationListener(proxy) */
Method addApplicationListener = com_apple_eawt_Application.getMethod("addApplicationListener", new Class[]{com_apple_eawt_ApplicationListener});
addApplicationListener.invoke(application, new Object[]{listener_proxy});
/* Finally, get the handleQuit method we want to react to */
Class com_apple_eawt_ApplicationEvent = Class.forName("com.apple.eawt.ApplicationEvent");
handleQuit = com_apple_eawt_ApplicationListener.getMethod("handleQuit", new Class[]{com_apple_eawt_ApplicationEvent});
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
handleQuit = (Method)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
/* Get the com.apple.eawt.Application class */
Class com_apple_eawt_Application = Class.forName("com.apple.eawt.Application");
/* Call the static Application.getApplication() method */
Object application = com_apple_eawt_Application.getMethod("getApplication", null).invoke(null, null);
/* Create a proxy implementing com.apple.eawt.ApplicationListener */
Class com_apple_eawt_ApplicationListener = Class.forName("com.apple.eawt.ApplicationListener");
Object listener_proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {com_apple_eawt_ApplicationListener}, MacOSXApplicationListener.this);
/* Invoke the method application.addApplicationListener(proxy) */
Method addApplicationListener = com_apple_eawt_Application.getMethod("addApplicationListener", new Class[]{com_apple_eawt_ApplicationListener});
addApplicationListener.invoke(application, new Object[]{listener_proxy});
/* Finally, get the handleQuit method we want to react to */
Class com_apple_eawt_ApplicationEvent = Class.forName("com.apple.eawt.ApplicationEvent");
return com_apple_eawt_ApplicationListener.getMethod("handleQuit", new Class[]{com_apple_eawt_ApplicationEvent});
}
});
} catch (PrivilegedActionException e) {
throw new RuntimeException(e);
}
}

View File

@ -48,6 +48,10 @@ import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
import org.lwjgl.LWJGLException;
final class MacOSXFrame extends Frame implements WindowListener, ComponentListener {
@ -62,25 +66,34 @@ final class MacOSXFrame extends Frame implements WindowListener, ComponentListen
private boolean minimized;
private boolean should_warp_cursor;
MacOSXFrame(DisplayMode mode, java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException {
MacOSXFrame(DisplayMode mode, final java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException {
setResizable(false);
addWindowListener(this);
addComponentListener(this);
canvas = new MacOSXGLCanvas();
add(canvas, BorderLayout.CENTER);
boolean undecorated = Boolean.getBoolean("org.lwjgl.opengl.Window.undecorated");
boolean undecorated = Display.getPrivilegedBoolean("org.lwjgl.opengl.Window.undecorated");
setUndecorated(fullscreen || undecorated);
if ( fullscreen ) {
getDevice().setFullScreenWindow(this);
getDevice().setDisplayMode(requested_mode);
java.awt.DisplayMode real_mode = getDevice().getDisplayMode();
/** For some strange reason, the display mode is sometimes silently capped even though the mode is reported as supported */
if ( requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight() ) {
getDevice().setFullScreenWindow(null);
if (isDisplayable())
dispose();
throw new LWJGLException("AWT capped mode: requested mode = " + requested_mode.getWidth() + "x" + requested_mode.getHeight() +
" but got " + real_mode.getWidth() + " " + real_mode.getHeight());
try {
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
getDevice().setFullScreenWindow(MacOSXFrame.this);
getDevice().setDisplayMode(requested_mode);
java.awt.DisplayMode real_mode = getDevice().getDisplayMode();
/** For some strange reason, the display mode is sometimes silently capped even though the mode is reported as supported */
if ( requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight() ) {
getDevice().setFullScreenWindow(null);
if (isDisplayable())
dispose();
throw new LWJGLException("AWT capped mode: requested mode = " + requested_mode.getWidth() + "x" + requested_mode.getHeight() +
" but got " + real_mode.getWidth() + " " + real_mode.getHeight());
}
return null;
}
});
} catch (PrivilegedActionException e) {
throw new LWJGLException(e);
}
}
pack();

View File

@ -314,9 +314,9 @@ void ext_InitializeClass(JNIEnv *env, jclass clazz, ExtGetProcAddressPROC gpa, i
bool getBooleanProperty(JNIEnv *env, const char* propertyName) {
jstring property = NewStringNative(env, propertyName);
jclass booleanClass = (*env)->FindClass(env, "java/lang/Boolean");
jmethodID getBoolean = (*env)->GetStaticMethodID(env, booleanClass, "getBoolean", "(Ljava/lang/String;)Z");
return (*env)->CallStaticBooleanMethod(env, booleanClass, getBoolean, property) ? true : false;
jclass org_lwjgl_LWJGLUtil_class = (*env)->FindClass(env, "org/lwjgl/LWJGLUtil");
jmethodID getBoolean = (*env)->GetStaticMethodID(env, org_lwjgl_LWJGLUtil_class, "getPrivilegedBoolean", "(Ljava/lang/String;)Z");
return (*env)->CallStaticBooleanMethod(env, org_lwjgl_LWJGLUtil_class, getBoolean, property) ? true : false;
}
JavaVM *getJVM() {