Example usage for java.security PrivilegedActionException getCause

List of usage examples for java.security PrivilegedActionException getCause

Introduction

In this page you can find the example usage for java.security PrivilegedActionException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:Main.java

static void execPrivileged(final String[] cmd_array) throws Exception {
    try {//from   w  w  w  . j  a  v a 2s  .  c  o  m
        Process process = AccessController.doPrivileged(new PrivilegedExceptionAction<Process>() {
            public Process run() throws Exception {
                return Runtime.getRuntime().exec(cmd_array);
            }
        });
        // Close unused streams to make sure the child process won't hang
        process.getInputStream().close();
        process.getOutputStream().close();
        process.getErrorStream().close();
    } catch (PrivilegedActionException e) {
        throw (Exception) e.getCause();
    }
}

From source file:Main.java

static Unsafe getSMU() {
    try {/*from   w  w w  . j  a  v a  2s. co m*/
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {
        // ignore
    }
    try {
        return java.security.AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
            Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
            for (Field f : k.getDeclaredFields()) {
                f.setAccessible(true);
                Object x = f.get(null);
                if (k.isInstance(x))
                    return k.cast(x);
            }
            throw new NoSuchFieldError("the Unsafe");
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}

From source file:org.mobicents.slee.container.util.ObjectCloner.java

public static <T> T makeDeepCopy(final T orig) {

    if (System.getSecurityManager() != null) {
        try {//w ww . j  a  v  a2  s.c o m
            return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {

                public T run() throws Exception {

                    return _makeDeepCopy(orig);
                }
            });
        } catch (PrivilegedActionException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            } else {
                throw new SLEEException("Failed to create object copy.", e);
            }
        }
    } else {
        return _makeDeepCopy(orig);
    }
}

From source file:org.rhq.bindings.ScriptEngineFactory.java

/**
 * This method is similar to the {@link #getScriptEngine(String, PackageFinder, StandardBindings)} method
 * but additionally applies a security wrapper on the returned script engine so that the scripts execute
 * with the provided java permissions./*from  w  w w  .  ja  va  2 s  .  com*/
 * 
 * @see #getScriptEngine(String, PackageFinder, StandardBindings)
 */
public static ScriptEngine getSecuredScriptEngine(final String language, final PackageFinder packageFinder,
        final StandardBindings bindings, final PermissionCollection permissions)
        throws ScriptException, IOException {
    CodeSource src = new CodeSource(new URL("http://rhq-project.org/scripting"), (Certificate[]) null);
    ProtectionDomain scriptDomain = new ProtectionDomain(src, permissions);
    AccessControlContext ctx = new AccessControlContext(new ProtectionDomain[] { scriptDomain });
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<ScriptEngine>() {
            @Override
            public ScriptEngine run() throws Exception {
                //This might seem a bit excessive but is necessary due to the 
                //change in security handling in the rhino script engine
                //that occured in Java6u27 (due to a CVE desribed here:
                //https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2011-3544)

                //In Java 6u26 and earlier, it was enough to wrap a script engine
                //in the sandbox and everything would work.

                //Java 6u27 introduced new behavior where the rhino script engine
                //remembers the access control context with which it has been 
                //constructed and combines that with the callers protection domain
                //when a script is executed. Because this class has all perms and
                //all the code in RHQ that called ScriptEngine.eval* also
                //had all perms, the scripts would never be sandboxed even if the call
                //was pushed through the SandboxedScriptEngine.

                //This means that the below wrapping is necessary for the security
                //to work in java6 pre u27 while the surrounding privileged block 
                //is necessary for the security to be applied in java6 u27 and later.
                return new SandboxedScriptEngine(getScriptEngine(language, packageFinder, bindings),
                        permissions);
            }
        }, ctx);
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof ScriptException) {
            throw (ScriptException) cause;
        } else {
            throw new ScriptException(e);
        }
    }
}

From source file:org.elasticsearch.xpack.security.authc.kerberos.SpnegoHttpClientConfigCallbackHandler.java

/**
 * Privileged Wrapper that invokes action with Subject.doAs to perform work as
 * given subject.//from   w  ww  .j av  a2s .  co  m
 *
 * @param subject {@link Subject} to be used for this work
 * @param action {@link PrivilegedExceptionAction} action for performing inside
 *            Subject.doAs
 * @param acc the {@link AccessControlContext} to be tied to the specified
 *            subject and action see
 *            {@link Subject#doAsPrivileged(Subject, PrivilegedExceptionAction, AccessControlContext)
 * @return the value returned by the PrivilegedExceptionAction's run method
 * @throws PrivilegedActionException
 */
static <T> T doAsPrivilegedWrapper(final Subject subject, final PrivilegedExceptionAction<T> action,
        final AccessControlContext acc) throws PrivilegedActionException {
    try {
        return AccessController.doPrivileged(
                (PrivilegedExceptionAction<T>) () -> Subject.doAsPrivileged(subject, action, acc));
    } catch (PrivilegedActionException pae) {
        if (pae.getCause() instanceof PrivilegedActionException) {
            throw (PrivilegedActionException) pae.getCause();
        }
        throw pae;
    }
}

From source file:org.commoncrawl.util.shared.MMapUtils.java

/**
 * Try to unmap the buffer, this method silently fails if no support
 * for that in the JVM. On Windows, this leads to the fact,
 * that mmapped files cannot be modified or deleted.
 *///from   w  ww.j a va2 s  .co  m
final static void cleanMapping(final ByteBuffer buffer) throws IOException {
    if (getUseUnmap()) {
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                public Object run() throws Exception {
                    final Method getCleanerMethod = buffer.getClass().getMethod("cleaner");
                    getCleanerMethod.setAccessible(true);
                    final Object cleaner = getCleanerMethod.invoke(buffer);
                    if (cleaner != null) {
                        cleaner.getClass().getMethod("clean").invoke(cleaner);
                    }
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            final IOException ioe = new IOException("unable to unmap the mapped buffer");
            ioe.initCause(e.getCause());
            throw ioe;
        }
    }
}

From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java

private static void privilegedConnect(CheckedRunnable<Exception> runnable) throws Exception {
    try {/*from  ww w.  ja v a2s .c  o m*/
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            runnable.run();
            return null;
        });
    } catch (PrivilegedActionException e) {
        throw (Exception) e.getCause();
    }
}

From source file:org.apache.hadoop.mapreduce.v2.util.MRApps.java

private static ClassLoader createJobClassLoader(final String appClasspath, final String[] systemClasses)
        throws IOException {
    try {/*  w w  w  .ja va2  s .c om*/
        return AccessController.doPrivileged(new PrivilegedExceptionAction<ClassLoader>() {
            @Override
            public ClassLoader run() throws MalformedURLException {
                return new ApplicationClassLoader(appClasspath, MRApps.class.getClassLoader(),
                        Arrays.asList(systemClasses));
            }
        });
    } catch (PrivilegedActionException e) {
        Throwable t = e.getCause();
        if (t instanceof MalformedURLException) {
            throw (MalformedURLException) t;
        }
        throw new IOException(e);
    }
}

From source file:net.sourceforge.safr.sample.SampleTest.java

private void runSampleMethodAs(String userId, String methodName, Object... args) throws Exception {
    MethodRunner runner = new MethodRunner(sample, methodName, args);
    Subject subject = sample.createSubjectForUser(userId);
    try {// w w w . ja  v a  2s  .  co m
        Subject.doAsPrivileged(subject, runner, null);
    } catch (PrivilegedActionException e) {
        throw (Exception) e.getCause().getCause();
    }
}

From source file:org.pentaho.di.core.auth.kerberos.LoginContextInvocationHandler.java

@Override
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
    try {/*from  w  ww.  j a v  a2  s.co  m*/
        return Subject.doAs(loginContext.getSubject(), new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                Object result = method.invoke(delegate, args);
                if (result != null) {
                    for (Class<?> iface : result.getClass().getInterfaces()) {
                        if (interfacesToDelegate.contains(iface)) {
                            result = forObject(result, loginContext, interfacesToDelegate);
                            break;
                        }
                    }
                }
                return result;
            }
        });
    } catch (PrivilegedActionException e) {
        if (e.getCause() instanceof InvocationTargetException) {
            throw ((InvocationTargetException) e.getCause()).getCause();
        }
        throw e;
    }
}