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:org.atricore.idbus.kernel.main.util.ClassFinderImpl.java

/**
 * Return the class for this name//ww  w  .  ja v  a2s  .  c  o  m
 *
 * @return Class
 */
private static Class forName(final String className, final boolean initialize, final ClassLoader classloader) {
    // NOTE: This method must remain private because it uses AccessController
    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);
        }
        throw new RuntimeException(e.getException());
    }

    return cl;
}

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

/**
 * Copy a file. Return false if <code>from</code> does not exist.
 *///from  ww w  . ja v a 2s. c om
public static boolean copy(File from, File to) throws IOException {
    if (from == null || to == null
            || !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(from))).booleanValue())
        return false;

    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = AccessController.doPrivileged(J2DoPrivHelper.newFileInputStreamAction(from));
        BufferedInputStream inbuf = new BufferedInputStream(in);
        out = AccessController.doPrivileged(J2DoPrivHelper.newFileOutputStreamAction(to));
        BufferedOutputStream outbuf = new BufferedOutputStream(out);
        for (int b; (b = inbuf.read()) != -1; outbuf.write(b))
            ;
        outbuf.flush();
        return true;
    } catch (PrivilegedActionException pae) {
        throw (FileNotFoundException) pae.getException();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (Exception e) {
            }
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
            }
    }
}

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

/**
 * Return the file for the given package. If the given base directory
 * matches the given package structure, it will be used as-is. If not,
 * the package structure will be added beneath the base directory. If
 * the base directory is null, the current working directory will be
 * used as the base./*ww  w .j  a v  a2  s .  co m*/
 */
public static File getPackageFile(File base, String pkg, boolean mkdirs) {
    if (base == null)
        base = new File(AccessController.doPrivileged(J2DoPrivHelper.getPropertyAction("user.dir")));
    if (StringUtils.isEmpty(pkg)) {
        if (mkdirs && !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(base))).booleanValue())
            AccessController.doPrivileged(J2DoPrivHelper.mkdirsAction(base));
        return base;
    }

    pkg = pkg.replace('.', File.separatorChar);
    File file = null;
    try {
        if ((AccessController.doPrivileged(J2DoPrivHelper.getCanonicalPathAction(base))).endsWith(pkg))
            file = base;
        else
            file = new File(base, pkg);
    } catch (PrivilegedActionException pae) {
        throw new NestableRuntimeException((IOException) pae.getException());
    } catch (IOException ioe) {
        throw new NestableRuntimeException(ioe);
    }

    if (mkdirs && !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(file))).booleanValue())
        AccessController.doPrivileged(J2DoPrivHelper.mkdirsAction(file));
    return file;
}

From source file:org.apache.axis2.jaxws.message.databinding.impl.ClassFinderImpl.java

/**
 * Return the class for this name/* ww w. ja  v  a 2 s  .c  o  m*/
 *
 * @return Class
 */
private static Class forName(final String className, final boolean initialize, final ClassLoader classloader) {
    // NOTE: This method must remain private because it uses AccessController
    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);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:org.apache.catalina.security.SecurityUtil.java

/**
 * Perform work as a particular </code>Subject</code>. Here the work
 * will be granted to a <code>null</code> subject. 
 *
 * @param methodName the method to apply the security restriction
 * @param targetObject the <code>Servlet</code> on which the method will
 * be called./*  www  .  java2s  .  c o m*/
 * @param targetType <code>Class</code> array used to instanciate a 
 * <code>Method</code> object.
 * @param targetArgumentst <code>Object</code> array contains the 
 * runtime parameters instance.
 * @param principal the <code>Principal</code> to which the security 
 * privilege apply..
 */
private static void execute(final Method method, final Object targetObject, final Object[] targetArguments,
        Principal principal) throws java.lang.Exception {

    try {
        Subject subject = null;
        PrivilegedExceptionAction pea = new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                method.invoke(targetObject, targetArguments);
                return null;
            }
        };

        // The first argument is always the request object
        if (targetArguments != null && targetArguments[0] instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) targetArguments[0];

            HttpSession session = request.getSession(false);
            if (session != null) {
                subject = (Subject) session.getAttribute(Globals.SUBJECT_ATTR);

                if (subject == null) {
                    subject = new Subject();
                    session.setAttribute(Globals.SUBJECT_ATTR, subject);
                }
            }
        }

        Subject.doAsPrivileged(subject, pea, null);
    } catch (PrivilegedActionException pe) {
        Throwable e = ((InvocationTargetException) pe.getException()).getTargetException();

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("SecurityUtil.doAsPrivilege"), e);
        }

        if (e instanceof UnavailableException)
            throw (UnavailableException) e;
        else if (e instanceof ServletException)
            throw (ServletException) e;
        else if (e instanceof IOException)
            throw (IOException) e;
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new ServletException(e.getMessage(), e);
    }
}

From source file:org.apache.axis2.jaxws.message.databinding.impl.DataSourceBlockImpl.java

/**
 * Return the class for this name/*ww w  . ja  va 2 s.c o  m*/
 * @return Class
 */
private static Class forName(final String className) throws ClassNotFoundException {
    // NOTE: This method must remain private because it uses AccessController
    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);
        }
        throw (ClassNotFoundException) e.getException();
    }

    return cl;
}

From source file:org.apache.axis2.jaxws.runtime.description.marshal.impl.AnnotationBuilder.java

/** @return ClassLoader */
static ClassLoader getContextClassLoader() {
    // NOTE: This method must remain private because it uses AccessController
    ClassLoader cl = null;//from ww w .java 2  s .co  m
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return Thread.currentThread().getContextClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw (RuntimeException) e.getException();
    }

    return cl;
}

From source file:com.zimbra.cs.account.ldap.LdapGalSearch.java

private static void galSearchKrb5(GalSearchParams params) throws ServiceException {

    try {//  w  ww. ja  v a 2 s .  co  m
        String krb5Principal = params.getConfig().getKerberosPrincipal();
        String krb5Keytab = params.getConfig().getKerberosKeytab();
        Krb5Login.performAs(krb5Principal, krb5Keytab, new GalSearchAction(params));
    } catch (LoginException le) {
        throw ServiceException.FAILURE("login failed, unable to search GAL", le);
    } catch (PrivilegedActionException pae) {
        // e.getException() should be an instance of NamingException,
        // as only "checked" exceptions will be wrapped in a PrivilegedActionException.
        Exception e = pae.getException();
        if (e instanceof ServiceException)
            throw (ServiceException) e;
        else // huh?
            throw ServiceException.FAILURE("caught exception, unable to search GAL", e);
    }
}

From source file:com.zimbra.cs.account.ldap.LdapGalSearch.java

private static void searchLdapGalKrb5(GalParams.ExternalGalParams galParams, GalOp galOp, String query,
        int maxResults, LdapGalMapRules rules, String token, SearchGalResult result) throws ServiceException {

    try {//from www .jav a2s.c om
        LdapGalCredential credential = galParams.credential();
        Krb5Login.performAs(credential.getKrb5Principal(), credential.getKrb5Keytab(),
                new SearchGalAction(galParams, galOp, query, maxResults, rules, token, result));
    } catch (LoginException le) {
        throw ServiceException.FAILURE("login failed, unable to search GAL", le);
    } catch (PrivilegedActionException pae) {
        // e.getException() should be an instance of NamingException,
        // as only "checked" exceptions will be wrapped in a PrivilegedActionException.
        Exception e = pae.getException();
        if (e instanceof ServiceException)
            throw (ServiceException) e;
        else // huh?
            throw ServiceException.FAILURE("caught exception, unable to search GAL", e);
    }
}

From source file:org.apache.openjpa.persistence.PersistenceProductDerivation.java

private static List<URL> getResourceURLs(String rsrc, ClassLoader loader) throws IOException {
    List<URL> answer = null;
    if (RSRC_DEFAULT.equals(rsrc) && defaultPersistenceFiles != null) {
        answer = new ArrayList();
        answer.addAll(defaultPersistenceFiles);
    }/*from  w  w w . j a  v  a 2s  .co  m*/
    Enumeration<URL> urls = null;
    try {
        urls = (Enumeration) AccessController.doPrivileged(J2DoPrivHelper.getResourcesAction(loader, rsrc));
        if (!urls.hasMoreElements()) {
            if (!rsrc.startsWith("META-INF"))
                urls = (Enumeration) AccessController
                        .doPrivileged(J2DoPrivHelper.getResourcesAction(loader, "META-INF/" + rsrc));
        }
    } catch (PrivilegedActionException pae) {
        throw (IOException) pae.getException();
    }

    if (urls.hasMoreElements()) {
        if (answer == null) {
            answer = Collections.list(urls);
        } else {
            answer.addAll(Collections.list(urls));
        }
    }

    return answer;
}