use of java.lang.reflect.UndeclaredThrowableException in project hackpad by dropbox.
the class PolicySecurityController method callWithDomain.
@Override
public Object callWithDomain(final Object securityDomain, final Context cx, Callable callable, Scriptable scope, Scriptable thisObj, Object[] args) {
// Run in doPrivileged as we might be checked for "getClassLoader"
// runtime permission
final ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return cx.getApplicationClassLoader();
}
});
final CodeSource codeSource = (CodeSource) securityDomain;
Map<ClassLoader, SoftReference<SecureCaller>> classLoaderMap;
synchronized (callers) {
classLoaderMap = callers.get(codeSource);
if (classLoaderMap == null) {
classLoaderMap = new WeakHashMap<ClassLoader, SoftReference<SecureCaller>>();
callers.put(codeSource, classLoaderMap);
}
}
SecureCaller caller;
synchronized (classLoaderMap) {
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
if (ref != null) {
caller = ref.get();
} else {
caller = null;
}
if (caller == null) {
try {
// Run in doPrivileged as we'll be checked for
// "createClassLoader" runtime permission
caller = (SecureCaller) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
Loader loader = new Loader(classLoader, codeSource);
Class<?> c = loader.defineClass(SecureCaller.class.getName() + "Impl", secureCallerImplBytecode);
return c.newInstance();
}
});
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
} catch (PrivilegedActionException ex) {
throw new UndeclaredThrowableException(ex.getCause());
}
}
}
return caller.call(callable, cx, scope, thisObj, args);
}
use of java.lang.reflect.UndeclaredThrowableException in project hackpad by dropbox.
the class Main method processSource.
/**
* Evaluate JavaScript source.
*
* @param cx the current context
* @param filename the name of the file to compile, or null
* for interactive mode.
*/
public static void processSource(Context cx, String filename) {
if (filename == null || filename.equals("-")) {
PrintStream ps = global.getErr();
if (filename == null) {
// print implementation version
ps.println(cx.getImplementationVersion());
}
String charEnc = shellContextFactory.getCharacterEncoding();
if (charEnc == null) {
charEnc = System.getProperty("file.encoding");
}
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(global.getIn(), charEnc));
} catch (UnsupportedEncodingException e) {
throw new UndeclaredThrowableException(e);
}
int lineno = 1;
boolean hitEOF = false;
while (!hitEOF) {
String[] prompts = global.getPrompts(cx);
if (filename == null)
ps.print(prompts[0]);
ps.flush();
String source = "";
// Collect lines of source to compile.
while (true) {
String newline;
try {
newline = in.readLine();
} catch (IOException ioe) {
ps.println(ioe.toString());
break;
}
if (newline == null) {
hitEOF = true;
break;
}
source = source + newline + "\n";
lineno++;
if (cx.stringIsCompilableUnit(source))
break;
ps.print(prompts[1]);
}
Script script = loadScriptFromSource(cx, source, "<stdin>", lineno, null);
if (script != null) {
Object result = evaluateScript(script, cx, global);
// Avoid printing out undefined or function definitions.
if (result != Context.getUndefinedValue() && !(result instanceof Function && source.trim().startsWith("function"))) {
try {
ps.println(Context.toString(result));
} catch (RhinoException rex) {
ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
}
}
NativeArray h = global.history;
h.put((int) h.getLength(), h, source);
}
}
ps.println();
} else if (filename.equals(mainModule)) {
try {
require.requireMain(cx, filename);
} catch (RhinoException rex) {
ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
exitCode = EXITCODE_RUNTIME_ERROR;
} catch (VirtualMachineError ex) {
// Treat StackOverflow and OutOfMemory as runtime errors
ex.printStackTrace();
String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ex.toString());
exitCode = EXITCODE_RUNTIME_ERROR;
Context.reportError(msg);
}
} else {
processFile(cx, global, filename);
}
}
use of java.lang.reflect.UndeclaredThrowableException in project hackpad by dropbox.
the class SecureCaller method callSecurely.
/**
* Call the specified callable using a protection domain belonging to the
* specified code source.
*/
static Object callSecurely(final CodeSource codeSource, Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
final Thread thread = Thread.currentThread();
// Run in doPrivileged as we might be checked for "getClassLoader"
// runtime permission
final ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return thread.getContextClassLoader();
}
});
Map<ClassLoader, SoftReference<SecureCaller>> classLoaderMap;
synchronized (callers) {
classLoaderMap = callers.get(codeSource);
if (classLoaderMap == null) {
classLoaderMap = new WeakHashMap<ClassLoader, SoftReference<SecureCaller>>();
callers.put(codeSource, classLoaderMap);
}
}
SecureCaller caller;
synchronized (classLoaderMap) {
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
if (ref != null) {
caller = ref.get();
} else {
caller = null;
}
if (caller == null) {
try {
// Run in doPrivileged as we'll be checked for
// "createClassLoader" runtime permission
caller = (SecureCaller) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
ClassLoader effectiveClassLoader;
Class<?> thisClass = getClass();
if (classLoader.loadClass(thisClass.getName()) != thisClass) {
effectiveClassLoader = thisClass.getClassLoader();
} else {
effectiveClassLoader = classLoader;
}
SecureClassLoaderImpl secCl = new SecureClassLoaderImpl(effectiveClassLoader);
Class<?> c = secCl.defineAndLinkClass(SecureCaller.class.getName() + "Impl", secureCallerImplBytecode, codeSource);
return c.newInstance();
}
});
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
} catch (PrivilegedActionException ex) {
throw new UndeclaredThrowableException(ex.getCause());
}
}
}
return caller.call(callable, cx, scope, thisObj, args);
}
use of java.lang.reflect.UndeclaredThrowableException in project hackpad by dropbox.
the class SecureCaller method loadBytecodePrivileged.
private static byte[] loadBytecodePrivileged() {
URL url = SecureCaller.class.getResource("SecureCallerImpl.clazz");
try {
InputStream in = url.openStream();
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
for (; ; ) {
int r = in.read();
if (r == -1) {
return bout.toByteArray();
}
bout.write(r);
}
} finally {
in.close();
}
} catch (IOException e) {
throw new UndeclaredThrowableException(e);
}
}
use of java.lang.reflect.UndeclaredThrowableException in project j2objc by google.
the class UndeclaredThrowableExceptionTest method test_UndeclaredThrowableException_LThrowable_LString.
/**
* {@link java.lang.reflect.UndeclaredThrowableException#UndeclaredThrowableException(java.lang.Throwable, java.lang.String)}
*/
public void test_UndeclaredThrowableException_LThrowable_LString() {
UndeclaredThrowableException e = new UndeclaredThrowableException(null, "SomeMsg");
assertNotNull(e);
assertNull(e.getCause());
assertEquals("Wrong message", "SomeMsg", e.getMessage());
}
Aggregations