use of java.security.PrivilegedAction in project robovm by robovm.
the class Subject method doAs_PrivilegedExceptionAction.
// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static <T> T doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction<T> action, final AccessControlContext context) throws PrivilegedActionException {
AccessControlContext newContext;
final SubjectDomainCombiner combiner;
if (subject == null) {
// performance optimization
// if subject is null there is nothing to combine
combiner = null;
} else {
combiner = new SubjectDomainCombiner(subject);
}
PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {
public AccessControlContext run() {
return new AccessControlContext(context, combiner);
}
};
newContext = AccessController.doPrivileged(dccAction);
return AccessController.doPrivileged(action, newContext);
}
use of java.security.PrivilegedAction in project hazelcast by hazelcast.
the class UnsafeHelper method findUnsafe.
private static Unsafe findUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException se) {
return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
@Override
public Unsafe run() {
try {
Class<Unsafe> type = Unsafe.class;
try {
Field field = type.getDeclaredField("theUnsafe");
field.setAccessible(true);
return type.cast(field.get(type));
} catch (Exception e) {
for (Field field : type.getDeclaredFields()) {
if (type.isAssignableFrom(field.getType())) {
field.setAccessible(true);
return type.cast(field.get(type));
}
}
}
} catch (Exception e) {
throw new RuntimeException("Unsafe unavailable", e);
}
throw new RuntimeException("Unsafe unavailable");
}
});
}
}
use of java.security.PrivilegedAction in project XobotOS by xamarin.
the class LogFactory method getProperties.
/**
* Given a URL that refers to a .properties file, load that file.
* This is done under an AccessController so that this method will
* succeed when this jarfile is privileged but the caller is not.
* This method must therefore remain private to avoid security issues.
* <p>
* Null is returned if the URL cannot be opened.
*/
private static Properties getProperties(final URL url) {
PrivilegedAction action = new PrivilegedAction() {
public Object run() {
try {
InputStream stream = url.openStream();
if (stream != null) {
Properties props = new Properties();
props.load(stream);
stream.close();
return props;
}
} catch (IOException e) {
if (isDiagnosticsEnabled()) {
logDiagnostic("Unable to read URL " + url);
}
}
return null;
}
};
return (Properties) AccessController.doPrivileged(action);
}
use of java.security.PrivilegedAction in project XobotOS by xamarin.
the class LogFactory method getResources.
/**
* Given a filename, return an enumeration of URLs pointing to
* all the occurrences of that filename in the classpath.
* <p>
* This is just like ClassLoader.getResources except that the
* operation is done under an AccessController so that this method will
* succeed when this jarfile is privileged but the caller is not.
* This method must therefore remain private to avoid security issues.
* <p>
* If no instances are found, an Enumeration is returned whose
* hasMoreElements method returns false (ie an "empty" enumeration).
* If resources could not be listed for some reason, null is returned.
*/
private static Enumeration getResources(final ClassLoader loader, final String name) {
PrivilegedAction action = new PrivilegedAction() {
public Object run() {
try {
if (loader != null) {
return loader.getResources(name);
} else {
return ClassLoader.getSystemResources(name);
}
} catch (IOException e) {
if (isDiagnosticsEnabled()) {
logDiagnostic("Exception while trying to find configuration file " + name + ":" + e.getMessage());
}
return null;
} catch (NoSuchMethodError e) {
// this case.
return null;
}
}
};
Object result = AccessController.doPrivileged(action);
return (Enumeration) result;
}
use of java.security.PrivilegedAction in project XobotOS by xamarin.
the class Subject method doAs_PrivilegedExceptionAction.
// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static <T> T doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction<T> action, final AccessControlContext context) throws PrivilegedActionException {
AccessControlContext newContext;
final SubjectDomainCombiner combiner;
if (subject == null) {
// performance optimization
// if subject is null there is nothing to combine
combiner = null;
} else {
combiner = new SubjectDomainCombiner(subject);
}
PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {
public AccessControlContext run() {
return new AccessControlContext(context, combiner);
}
};
newContext = AccessController.doPrivileged(dccAction);
return AccessController.doPrivileged(action, newContext);
}
Aggregations