package org.jboss.arquillian.drone.webdriver.utils;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
/**
*
*/
public class PropertySecurityAction {
// TODO: make the class protected
public static String getProperty(final String key) {
try {
String value = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
public String run() {
return System.getProperty(key);
}
});
return value;
}
// Unwrap
catch (final PrivilegedActionException pae) {
final Throwable t = pae.getCause();
// Rethrow
if (t instanceof SecurityException) {
throw (SecurityException) t;
}
if (t instanceof NullPointerException) {
throw (NullPointerException) t;
} else if (t instanceof IllegalArgumentException) {
throw (IllegalArgumentException) t;
} else {
// No other checked Exception thrown by System.getProperty
try {
throw (RuntimeException) t;
}
// Just in case we've really messed up
catch (final ClassCastException cce) {
throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
}
}
}
}
public static String setProperty(final String key, final String value) {
try {
String oldValue = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
public String run() {
if (value == null) {
return System.clearProperty(key);
}
return System.setProperty(key, value);
}
});
return oldValue;
}
// Unwrap
catch (final PrivilegedActionException pae) {
final Throwable t = pae.getCause();
// Rethrow
if (t instanceof SecurityException) {
throw (SecurityException) t;
}
if (t instanceof NullPointerException) {
throw (NullPointerException) t;
} else if (t instanceof IllegalArgumentException) {
throw (IllegalArgumentException) t;
} else {
// No other checked Exception thrown by System.getProperty
try {
throw (RuntimeException) t;
}
// Just in case we've really messed up
catch (final ClassCastException cce) {
throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
}
}
}
}
}