Package org.codehaus.commons.compiler
Class Sandbox
- java.lang.Object
-
- org.codehaus.commons.compiler.Sandbox
-
public final class Sandbox extends java.lang.Object
Executes aPrivilegedAction
orPrivilegedExceptionAction
in a context with restricted permissions. This is useful for executing "untrusted" code, e.g. user-provided expressions or scripts that were compiled with JANINO.Code example:
Permissions noPermissions = new Permissions(); Sandbox sandbox = new Sandbox(noPermissions); sandbox.confine(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { new java.io.File("xxx").delete(); return null; } });
-
-
Constructor Summary
Constructors Constructor Description Sandbox(java.security.PermissionCollection permissions)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> R
confine(java.security.PrivilegedAction<R> action)
Runs the given action, confined by the permissions configured through theconstructor
.<R> R
confine(java.security.PrivilegedExceptionAction<R> action)
-
-
-
Constructor Detail
-
Sandbox
public Sandbox(java.security.PermissionCollection permissions)
- Parameters:
permissions
- Will be applied on later calls toconfine(PrivilegedAction)
andconfine(PrivilegedExceptionAction)
-
-
Method Detail
-
confine
public <R> R confine(java.security.PrivilegedAction<R> action)
Runs the given action, confined by the permissions configured through theconstructor
.- Returns:
- The value returned by the action
-
confine
public <R> R confine(java.security.PrivilegedExceptionAction<R> action) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-