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.apache.bsf.BSFManager.java

/**
 * Apply the given anonymous function of the given language to the given
 * parameters and return the resulting value.
 *
 * @param lang language identifier//from   w w  w.ja  v  a  2 s.  c  o  m
 * @param source (context info) the source of this expression
 (e.g., filename)
 * @param lineNo (context info) the line number in source for expr
 * @param columnNo (context info) the column number in source for expr
 * @param funcBody the multi-line, value returning script to evaluate
 * @param paramNames the names of the parameters above assumes
 * @param arguments values of the above parameters
 *
 * @exception BSFException if anything goes wrong while running the script
 */
public Object apply(String lang, String source, int lineNo, int columnNo, Object funcBody, Vector paramNames,
        Vector arguments) throws BSFException {
    logger.debug("BSFManager:apply");

    final BSFEngine e = loadScriptingEngine(lang);
    final String sourcef = source;
    final int lineNof = lineNo, columnNof = columnNo;
    final Object funcBodyf = funcBody;
    final Vector paramNamesf = paramNames;
    final Vector argumentsf = arguments;
    Object result = null;

    try {
        final Object resultf = AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                return e.apply(sourcef, lineNof, columnNof, funcBodyf, paramNamesf, argumentsf);
            }
        });
        result = resultf;
    } catch (PrivilegedActionException prive) {

        logger.error("Exception: ", prive);
        throw (BSFException) prive.getException();
    }

    return result;
}

From source file:org.apache.jasper.runtime.PageContextImpl.java

public void include(final String relativeUrlPath, final boolean flush) throws ServletException, IOException {
    if (System.getSecurityManager() != null) {
        try {//from  w  w  w  .j  av a  2  s . c o  m
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() throws Exception {
                    doInclude(relativeUrlPath, flush);
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            Exception ex = e.getException();
            if (ex instanceof IOException) {
                throw (IOException) ex;
            } else {
                throw (ServletException) ex;
            }
        }
    } else {
        doInclude(relativeUrlPath, flush);
    }
}

From source file:org.apache.jasper.runtime.PageContextImpl.java

public void forward(final String relativeUrlPath) throws ServletException, IOException {
    if (System.getSecurityManager() != null) {
        try {//from   ww w.  ja v a2 s  . c  o m
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() throws Exception {
                    doForward(relativeUrlPath);
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            Exception ex = e.getException();
            if (ex instanceof IOException) {
                throw (IOException) ex;
            } else {
                throw (ServletException) ex;
            }
        }
    } else {
        doForward(relativeUrlPath);
    }
}

From source file:org.apache.jasper.runtime.PageContextImpl.java

public void handlePageException(final Throwable t) throws IOException, ServletException {
    if (t == null)
        throw new NullPointerException("null Throwable");

    if (System.getSecurityManager() != null) {
        try {/*  w  w  w .  j a  v a2s . c  o  m*/
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() throws Exception {
                    doHandlePageException(t);
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            Exception ex = e.getException();
            if (ex instanceof IOException) {
                throw (IOException) ex;
            } else {
                throw (ServletException) ex;
            }
        }
    } else {
        doHandlePageException(t);
    }

}

From source file:org.apache.bsf.BSFManager.java

/**
 * Load a scripting engine based on the lang string identifying it.
 *
 * @param lang string identifying language
 * @exception BSFException if the language is unknown (i.e., if it
 *            has not been registered) with a reason of
 *            REASON_UNKNOWN_LANGUAGE. If the language is known but
 *            if the interface can't be created for some reason, then
 *            the reason is set to REASON_OTHER_ERROR and the actual
 *            exception is passed on as well.
 *//*from   w  w w .  j  a  v a2s .  c  o  m*/
public BSFEngine loadScriptingEngine(String lang) throws BSFException {
    logger.debug("BSFManager:loadScriptingEngine");

    // if its already loaded return that
    BSFEngine eng = (BSFEngine) loadedEngines.get(lang);
    if (eng != null) {
        return eng;
    }

    // is it a registered language?
    String engineClassName = (String) registeredEngines.get(lang);
    if (engineClassName == null) {
        logger.error("unsupported language: " + lang);
        throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE, "unsupported language: " + lang);
    }

    // create the engine and initialize it. if anything goes wrong
    // except.
    try {
        Class engineClass = (classLoader == null) ? Class.forName(engineClassName)
                : classLoader.loadClass(engineClassName);
        final BSFEngine engf = (BSFEngine) engineClass.newInstance();
        final BSFManager thisf = this;
        final String langf = lang;
        final Vector dbf = declaredBeans;
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                engf.initialize(thisf, langf, dbf);
                return null;
            }
        });
        eng = engf;
        loadedEngines.put(lang, eng);
        pcs.addPropertyChangeListener(eng);
        return eng;
    } catch (PrivilegedActionException prive) {

        logger.error("Exception :", prive);
        throw (BSFException) prive.getException();
    } catch (Throwable t) {

        logger.error("Exception :", t);
        throw new BSFException(BSFException.REASON_OTHER_ERROR, "unable to load language: " + lang, t);
    }
}

From source file:org.apache.openjpa.event.TCPRemoteCommitProvider.java

/**
 * Sets the list of addresses of peers to which this provider will
 * send events to. The peers are semicolon-separated <code>names</code>
 * list in the form of "myhost1:portA;myhost2:portB".
 *///  ww w  . j ava 2s.  c  o  m
public void setAddresses(String names) throws UnknownHostException {
    // NYI. Could look for equivalence of addresses and avoid
    // changing those that didn't change.

    _addressesLock.lock();
    try {
        for (Iterator iter = _addresses.iterator(); iter.hasNext();) {
            ((HostAddress) iter.next()).close();
        }
        String[] toks = Strings.split(names, ";", 0);
        _addresses = new ArrayList(toks.length);

        InetAddress localhost = InetAddress.getLocalHost();
        String localhostName = localhost.getHostName();

        for (int i = 0; i < toks.length; i++) {
            String host = toks[i];
            String hostname;
            int tmpPort;
            int colon = host.indexOf(':');
            if (colon != -1) {
                hostname = host.substring(0, colon);
                tmpPort = Integer.parseInt(host.substring(colon + 1));
            } else {
                hostname = host;
                tmpPort = DEFAULT_PORT;
            }
            InetAddress tmpAddress = AccessController.doPrivileged(J2DoPrivHelper.getByNameAction(hostname));

            // bleair: For each address we would rather make use of
            // the jdk1.4 isLinkLocalAddress () || isLoopbackAddress ().
            // (Though in practice on win32 they don't work anyways!)
            // Instead we will check hostname. Not perfect, but
            // it will match often enough (people will typically
            // use the DNS machine names and be cutting/pasting.)
            if (localhostName.equals(hostname)) {
                // This string matches the hostname for for ourselves, we
                // don't actually need to send ourselves messages.
                if (log.isTraceEnabled()) {
                    log.trace(s_loc.get("tcp-address-asself", tmpAddress.getHostName() + ":" + tmpPort));
                }
            } else {
                HostAddress newAddress = new HostAddress(host);
                _addresses.add(newAddress);
                if (log.isTraceEnabled()) {
                    log.trace(s_loc.get("tcp-address-set",
                            newAddress._address.getHostName() + ":" + newAddress._port));
                }
            }
        }
    } catch (PrivilegedActionException pae) {
        throw (UnknownHostException) pae.getException();
    } finally {
        _addressesLock.unlock();
    }
}

From source file:org.apache.catalina.core.ApplicationDispatcher.java

/**
 * Forward this request and response to another resource for processing.
 * Any runtime exception, IOException, or ServletException thrown by the
 * called servlet will be propogated to the caller.
 *
 * @param request The servlet request to be forwarded
 * @param response The servlet response to be forwarded
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet exception occurs
 *//*from  w w w .  j  a va2  s  .c o  m*/
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    if (System.getSecurityManager() != null) {
        try {
            PrivilegedForward dp = new PrivilegedForward(request, response);
            AccessController.doPrivileged(dp);
        } catch (PrivilegedActionException pe) {
            Exception e = pe.getException();
            if (e instanceof ServletException)
                throw (ServletException) e;
            throw (IOException) e;
        }
    } else {
        doForward(request, response);
    }
}

From source file:org.apache.catalina.core.ApplicationDispatcher.java

/**
 * Include the response from another resource in the current response.
 * Any runtime exception, IOException, or ServletException thrown by the
 * called servlet will be propogated to the caller.
 *
 * @param request The servlet request that is including this one
 * @param response The servlet response to be appended to
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet exception occurs
 *///from  w  w  w. j av  a  2 s  . com
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    if (System.getSecurityManager() != null) {
        try {
            PrivilegedInclude dp = new PrivilegedInclude(request, response);
            AccessController.doPrivileged(dp);
        } catch (PrivilegedActionException pe) {
            Exception e = pe.getException();

            if (e instanceof ServletException)
                throw (ServletException) e;
            throw (IOException) e;
        }
    } else {
        doInclude(request, response);
    }
}

From source file:org.apache.zeppelin.realm.kerberos.KerberosRealm.java

/**
 * It enforces the the Kerberos SPNEGO authentication sequence returning an
 * {@link AuthenticationToken} only after the Kerberos SPNEGO sequence has
 * completed successfully.//w  ww . j  a  v a2  s . c o  m
 *
 * @param request  the HTTP client request.
 * @param response the HTTP client response.
 * @return an authentication token if the Kerberos SPNEGO sequence is complete
 * and valid, <code>null</code> if it is in progress (in this case the handler
 * handles the response to the client).
 * @throws IOException             thrown if an IO error occurred.
 * @throws AuthenticationException thrown if Kerberos SPNEGO sequence failed.
 */
public AuthenticationToken authenticate(HttpServletRequest request, final HttpServletResponse response)
        throws IOException, AuthenticationException {
    AuthenticationToken token = null;
    String authorization = request.getHeader(KerberosAuthenticator.AUTHORIZATION);

    if (authorization == null || !authorization.startsWith(KerberosAuthenticator.NEGOTIATE)) {
        response.setHeader(KerberosAuthenticator.WWW_AUTHENTICATE, KerberosAuthenticator.NEGOTIATE);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        if (authorization == null) {
            LOG.trace("SPNEGO starting for url: {}", request.getRequestURL());
        } else {
            LOG.warn("'" + KerberosAuthenticator.AUTHORIZATION + "' does not start with '"
                    + KerberosAuthenticator.NEGOTIATE + "' :  {}", authorization);
        }
    } else {
        authorization = authorization.substring(KerberosAuthenticator.NEGOTIATE.length()).trim();
        final Base64 base64 = new Base64(0);
        final byte[] clientToken = base64.decode(authorization);
        try {
            final String serverPrincipal = KerberosUtil.getTokenServerName(clientToken);
            if (!serverPrincipal.startsWith("HTTP/")) {
                throw new IllegalArgumentException(
                        "Invalid server principal " + serverPrincipal + "decoded from client request");
            }
            token = Subject.doAs(serverSubject, new PrivilegedExceptionAction<AuthenticationToken>() {
                @Override
                public AuthenticationToken run() throws Exception {
                    return runWithPrincipal(serverPrincipal, clientToken, base64, response);
                }
            });
        } catch (PrivilegedActionException ex) {
            if (ex.getException() instanceof IOException) {
                throw (IOException) ex.getException();
            } else {
                throw new AuthenticationException(ex.getException());
            }
        } catch (Exception ex) {
            throw new AuthenticationException(ex);
        }
    }
    return token;
}

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

/**
 * This method provides a Java2 Security compliant way to obtain the InputStream
 * for a given URLConnection object. This is needed as a given URLConnection object
 * may be an instance of a FileURLConnection object which would require access 
 * permissions if Java2 Security was enabled.
 *///from   w  ww.j  a v a2s  .  co m
private InputStream getInputStream(URLConnection urlCon) throws Exception {
    final URLConnection finalURLCon = urlCon;
    InputStream is = null;
    try {
        is = (InputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws IOException {
                return finalURLCon.getInputStream();
            }
        });
    } catch (PrivilegedActionException e) {
        throw e.getException();
    }
    return is;
}