Search in sources :

Example 71 with UndeclaredThrowableException

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);
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) SecureClassLoader(java.security.SecureClassLoader) CodeSource(java.security.CodeSource) PrivilegedActionException(java.security.PrivilegedActionException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SoftReference(java.lang.ref.SoftReference) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecureClassLoader(java.security.SecureClassLoader)

Example 72 with UndeclaredThrowableException

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);
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) PrintStream(java.io.PrintStream) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Function(org.mozilla.javascript.Function) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BufferedReader(java.io.BufferedReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException)

Example 73 with UndeclaredThrowableException

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);
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SoftReference(java.lang.ref.SoftReference) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecureClassLoader(java.security.SecureClassLoader)

Example 74 with UndeclaredThrowableException

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);
    }
}
Also used : InputStream(java.io.InputStream) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 75 with UndeclaredThrowableException

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());
}
Also used : UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Aggregations

UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)121 IOException (java.io.IOException)36 InvocationTargetException (java.lang.reflect.InvocationTargetException)14 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)14 Test (org.junit.Test)12 BufferedReader (java.io.BufferedReader)10 InputStreamReader (java.io.InputStreamReader)10 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PollStatus (org.opennms.netmgt.poller.PollStatus)9 HashMap (java.util.HashMap)8 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)7 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)7 BadRequestException (org.apache.hadoop.yarn.webapp.BadRequestException)7 Method (java.lang.reflect.Method)6 AccessControlException (java.security.AccessControlException)6 SQLException (java.sql.SQLException)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)6