Example usage for java.security PrivilegedActionException getException

List of usage examples for java.security PrivilegedActionException getException

Introduction

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

Prototype

public Exception getException() 

Source Link

Document

Returns the exception thrown by the privileged computation that resulted in this PrivilegedActionException .

Usage

From source file:SecuritySupport.java

static FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
    try {//ww w.  ja v  a 2s .  c o m
        return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream(file);
            }
        });
    } catch (PrivilegedActionException e) {
        throw (FileNotFoundException) e.getException();
    }
}

From source file:org.apache.jxtadoop.security.authorize.ServiceAuthorizationManager.java

/**
 * Check if the given {@link Subject} has all of necessary {@link Permission} 
 * set./*from  ww  w .j a v a  2s  . c o  m*/
 * 
 * @param user <code>Subject</code> to be authorized
 * @param permissions <code>Permission</code> set
 * @throws AuthorizationException if the authorization failed
 */
private static void checkPermission(final Subject user, final Permission... permissions)
        throws AuthorizationException {
    try {
        Subject.doAs(user, new PrivilegedExceptionAction<Void>() {
            public Void run() throws Exception {
                try {
                    for (Permission permission : permissions) {
                        AccessController.checkPermission(permission);
                    }
                } catch (AccessControlException ace) {
                    LOG.info("Authorization failed for " + UserGroupInformation.getCurrentUGI(), ace);
                    throw new AuthorizationException(ace);
                }
                return null;
            }
        });
    } catch (PrivilegedActionException e) {
        throw new AuthorizationException(e.getException());
    }
}

From source file:org.codice.ddf.configuration.migration.AccessUtils.java

/**
 * Performs the specified action with privileges enabled. The action is performed with <i>all</i>
 * of the permissions possessed by this class' (or by the migration framework's) protection
 * domain.//ww  w .ja va2  s.c  o m
 *
 * <p>If the action's {@link ThrowingSupplier#get} method throws an <i>unchecked</i> exception, it
 * will propagate through this method.
 *
 * <p><i>Note:</i> Any DomainCombiner associated with the current AccessControlContext will be
 * ignored while the action is performed.
 *
 * @param <T> the type of the value returned by the action
 * @param <E> the type of exceptions thrown by the action
 * @param action the action to be performed
 * @return the value returned by the action's {@code run} method
 * @throws E if the specified action's threw the exception
 * @throws IllegalArgumentException if <code>action</code> is <code>null</code>
 */
public static <T, E extends Exception> T doPrivileged(ThrowingSupplier<T, E> action) throws E {
    Validate.notNull(action, AccessUtils.INVALID_NULL_ACTION);
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
            @Override
            public T run() throws Exception {
                return action.get();
            }
        });
    } catch (PrivilegedActionException pe) {
        final Exception e = pe.getException();

        if (e instanceof RuntimeException) { // should never happen but just to be safe!
            throw (RuntimeException) e;
        } else { // by design, the action is declared to only throw E
            throw (E) e;
        }
    }
}

From source file:org.codice.ddf.configuration.migration.AccessUtils.java

/**
 * Performs the specified action with privileges enabled. The action is performed with <i>all</i>
 * of the permissions possessed by this class' (or by the migration framework's) protection
 * domain./*from w  w w.j  a va  2s  .  c o m*/
 *
 * <p>If the action's {@link ThrowingSupplier#get} method throws an <i>unchecked</i> exception, it
 * will propagate through this method.
 *
 * <p><i>Note:</i> Any DomainCombiner associated with the current AccessControlContext will be
 * ignored while the action is performed.
 *
 * @param <E> the type of exceptions thrown by the action
 * @param action the action to be performed
 * @throws E if the specified action's threw the exception
 * @throws IllegalArgumentException if <code>action</code> is <code>null</code>
 */
public static <E extends Exception> void doPrivileged(ThrowingRunnable<E> action) throws E {
    Validate.notNull(action, AccessUtils.INVALID_NULL_ACTION);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                action.run();
                return null;
            }
        });
    } catch (PrivilegedActionException pe) {
        final Exception e = pe.getException();

        if (e instanceof RuntimeException) { // should never happen but just to be safe!
            throw (RuntimeException) e;
        } else { // by design, the action is declared to only throw E
            throw (E) e;
        }
    }
}

From source file:org.apache.axis2.jaxws.utility.XmlEnumUtils.java

/**
 * @param e enumeration class/*from  w  w w. j  a v  a 2 s  . co  m*/
 * @param convObject Object of conversion type
 * @return Object of enum
 */
public static Object fromValue(final Class e, final Object convObject) {
    Object enumValue = null;
    if (log.isDebugEnabled()) {
        log.debug("fromValue for " + JavaUtils.getObjectIdentity(convObject));
    }
    try {
        enumValue = AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws InvocationTargetException, IllegalAccessException {
                Method m = getConversionMethod(e);
                return m.invoke(null, new Object[] { convObject });
            }
        });
    } catch (PrivilegedActionException pae) {
        throw ExceptionFactory.makeWebServiceException(pae.getException());
    } finally {
        if (log.isDebugEnabled()) {
            log.debug("getEnumValue is" + JavaUtils.getObjectIdentity(enumValue));
        }
    }
    return enumValue;
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * @return ClassLoader/*  w  w  w  .  j a  v  a2 s.  c o  m*/
 */
public static ClassLoader getClassLoader(final Class cls) {
    ClassLoader cl = null;
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return cls.getClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * Return the class for this name/*from  ww w . j  a  va2  s . co m*/
 *
 * @return Class
 */
public static Class forName(final String className) throws ClassNotFoundException {
    Class cl = null;
    try {
        cl = (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return Class.forName(className);
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e.getMessage(), e);
        }
        throw (ClassNotFoundException) e.getException();
    }

    return cl;
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * Return the class for this name/*from  w  w  w.j  a v  a 2  s  .co m*/
 *
 * @return Class
 */
public static Class forName(final String className, final boolean initialize, final ClassLoader classloader)
        throws ClassNotFoundException {
    Class cl = null;
    try {
        cl = (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return Class.forName(className, initialize, classloader);
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e.getMessage(), e);
        }
        throw (ClassNotFoundException) e.getException();
    }

    return cl;
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * @return ClassLoader/* w w  w .  j  av  a 2 s .c om*/
 */
public static ClassLoader getContextClassLoader(final ClassLoader classLoader) {
    ClassLoader cl;
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return classLoader != null ? classLoader : Thread.currentThread().getContextClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e.getMessage(), e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:org.apache.openjpa.lib.util.Files.java

/**
 * Return an output stream to the stream(stdout or stderr) or file named
 * by the given string./* w  w w .j a v  a  2s.  c  o  m*/
 *
 * @see #getFile
 */
public static OutputStream getOutputStream(String file, ClassLoader loader) {
    if (file == null)
        return null;
    if ("stdout".equals(file))
        return System.out;
    if ("stderr".equals(file))
        return System.err;
    try {
        return AccessController.doPrivileged(J2DoPrivHelper.newFileOutputStreamAction(getFile(file, loader)));
    } catch (PrivilegedActionException pae) {
        throw new NestableRuntimeException(pae.getException());
    } catch (IOException ioe) {
        throw new NestableRuntimeException(ioe);
    }
}