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.beangle.model.persist.hibernate.internal.ChainedClassLoader.java

public Class<?> loadClass(final String name) throws ClassNotFoundException {

    if (System.getSecurityManager() != null) {
        try {//from  ww  w.  j av a 2 s.c  om
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {

                public Class<?> run() throws Exception {
                    return doLoadClass(name);
                }
            });
        } catch (PrivilegedActionException pae) {
            throw (ClassNotFoundException) pae.getException();
        }
    } else {
        return doLoadClass(name);
    }
}

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

public Thread newThread(final Runnable r) {
    if (threadGroup == null) {
        try {//from   ww  w  . j  av a  2  s .  co  m
            threadGroup = (ThreadGroup) AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() {
                    return new ThreadGroup("JAX-WS Default Executor Group " + groupNumber++);
                }
            });
        } catch (PrivilegedActionException e) {
            if (log.isDebugEnabled()) {
                log.debug("Exception thrown from AccessController: " + e);
            }
            throw ExceptionFactory.makeWebServiceException(e.getException());
        }
    }

    threadNumber++;
    Thread returnThread = null;
    try {
        returnThread = (Thread) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() {
                Thread newThread = new Thread(threadGroup, r);
                newThread.setDaemon(true);
                return newThread;
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return returnThread;
}

From source file:org.apache.stanbol.commons.sphinx.impl.ModelProviderImpl.java

/**
 * Lookup an Sphinx data file via the {@link #dataFileProvider}
 * @param modelName the name of the model
 * @return the stream or <code>null</code> if not found
 * @throws IOException an any error while opening the model file
 *//*from w  w w  . ja v  a2 s  .  c  o m*/
protected InputStream lookupModelStream(final String modelName) throws IOException {
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {
            @Override
            public InputStream run() throws IOException {
                return dataFileProvider.getInputStream(bundleSymbolicName, modelName, null);
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
}

From source file:org.solmix.runtime.support.spring.ContainerApplicationContext.java

public ContainerApplicationContext(String[] cfgFiles, ApplicationContext parent, boolean includeDefault,
        NamespaceHandlerResolver nshResolver) {
    super(new String[0], false, parent);
    this.cfgFiles = cfgFiles;
    this.nshResolver = nshResolver;
    this.includeDefault = includeDefault;
    try {//from ww w.  j a  v  a  2  s  .c  o m
        AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {

            @Override
            public Boolean run() throws Exception {
                refresh();
                return Boolean.TRUE;
            }

        });
    } catch (PrivilegedActionException e) {
        if (e.getException() instanceof RuntimeException) {
            throw (RuntimeException) e.getException();
        }
    }
}

From source file:org.eclipse.gemini.blueprint.extender.internal.support.NamespacePlugins.java

public InputSource resolveEntity(final String publicId, final String systemId)
        throws SAXException, IOException {
    if (System.getSecurityManager() != null) {
        try {/*  w w  w  .j  a v a 2  s.c om*/
            return AccessController.doPrivileged(new PrivilegedExceptionAction<InputSource>() {

                public InputSource run() throws Exception {
                    return doResolveEntity(publicId, systemId);
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception cause = pae.getException();
            handleInputSourceException(cause);
        }
    } else {
        try {
            return doResolveEntity(publicId, systemId);
        } catch (Exception ex) {
            handleInputSourceException(ex);
        }
    }

    return null;
}

From source file:org.eclipse.ecr.runtime.api.login.LoginComponent.java

@Override
public LoginContext loginAs(final String username) throws LoginException {
    // login as system user is a privileged action
    try {//from  w  w w.  ja va  2s .  c om
        return AccessController.doPrivileged(new PrivilegedExceptionAction<LoginContext>() {
            @Override
            public LoginContext run() throws LoginException {
                SecurityManager sm = System.getSecurityManager();
                if (sm != null) {
                    sm.checkPermission(new SystemLoginPermission());
                }
                return systemLogin(username);
            }
        });
    } catch (PrivilegedActionException e) {
        throw (LoginException) e.getException();
    }
}

From source file:com.cloudera.alfredo.server.KerberosAuthenticationHandler.java

/**
 * Initializes the authentication handler instance.
 * <p/>/*from   ww  w.ja  va2 s.c o  m*/
 * It creates a Kerberos context using the principal and keytab specified in the configuration.
 * <p/>
 * This method is invoked by the {@link AuthenticationFilter#init} method.
 *
 * @param config configuration properties to initialize the handler.
 *
 * @throws ServletException thrown if the handler could not be initialized.
 */
@Override
public void init(Properties config) throws ServletException {
    try {
        principal = config.getProperty(PRINCIPAL, principal);
        if (principal == null || principal.trim().length() == 0) {
            throw new ServletException("Principal not defined in configuration");
        }
        keytab = config.getProperty(KEYTAB, keytab);
        if (keytab == null || keytab.trim().length() == 0) {
            throw new ServletException("Keytab not defined in configuration");
        }
        if (!new File(keytab).exists()) {
            throw new ServletException("Keytab does not exist: " + keytab);
        }

        Set<Principal> principals = new HashSet<Principal>();
        principals.add(new KerberosPrincipal(principal));
        Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());

        KerberosConfiguration kerberosConfiguration = new KerberosConfiguration(keytab, principal);

        loginContext = new LoginContext("", subject, null, kerberosConfiguration);
        loginContext.login();

        Subject serverSubject = loginContext.getSubject();
        try {
            gssManager = Subject.doAs(serverSubject, new PrivilegedExceptionAction<GSSManager>() {

                @Override
                public GSSManager run() throws Exception {
                    return GSSManager.getInstance();
                }
            });
        } catch (PrivilegedActionException ex) {
            throw ex.getException();
        }
        LOG.info("Initialized, principal [{}] from keytab [{}]", principal, keytab);
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}

From source file:org.apache.hadoop.security.authentication.client.KerberosAuthenticator.java

/**
 * Implements the SPNEGO authentication sequence interaction using the current default principal
 * in the Kerberos cache (normally set via kinit).
 *
 * @param token the authentication token being used for the user.
 *
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication error occurred.
 *//*ww  w .  ja  v  a2 s. c  om*/
private void doSpnegoSequence(AuthenticatedURL.Token token) throws IOException, AuthenticationException {
    try {
        AccessControlContext context = AccessController.getContext();
        Subject subject = Subject.getSubject(context);
        if (subject == null) {
            subject = new Subject();
            LoginContext login = new LoginContext("", subject, null, new KerberosConfiguration());
            login.login();
        }
        Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                GSSContext gssContext = null;
                try {
                    GSSManager gssManager = GSSManager.getInstance();
                    String servicePrincipal = "HTTP/" + KerberosAuthenticator.this.url.getHost();
                    Oid oid = KerberosUtil.getOidInstance("NT_GSS_KRB5_PRINCIPAL");
                    GSSName serviceName = gssManager.createName(servicePrincipal, oid);
                    oid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID");
                    gssContext = gssManager.createContext(serviceName, oid, null, GSSContext.DEFAULT_LIFETIME);
                    gssContext.requestCredDeleg(true);
                    gssContext.requestMutualAuth(true);

                    byte[] inToken = new byte[0];
                    byte[] outToken;
                    boolean established = false;

                    // Loop while the context is still not established
                    while (!established) {
                        outToken = gssContext.initSecContext(inToken, 0, inToken.length);
                        if (outToken != null) {
                            sendToken(outToken);
                        }

                        if (!gssContext.isEstablished()) {
                            inToken = readToken();
                        } else {
                            established = true;
                        }
                    }
                } finally {
                    if (gssContext != null) {
                        gssContext.dispose();
                        gssContext = null;
                    }
                }
                return null;
            }
        });
    } catch (PrivilegedActionException ex) {
        throw new AuthenticationException(ex.getException());
    } catch (LoginException ex) {
        throw new AuthenticationException(ex);
    }
    AuthenticatedURL.extractToken(conn, token);
}

From source file:org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler.java

/**
 * Initializes the authentication handler instance.
 * <p/>/* www.j  av a  2s  .c o m*/
 * It creates a Kerberos context using the principal and keytab specified in the configuration.
 * <p/>
 * This method is invoked by the {@link AuthenticationFilter#init} method.
 *
 * @param config configuration properties to initialize the handler.
 *
 * @throws ServletException thrown if the handler could not be initialized.
 */
@Override
public void init(Properties config) throws ServletException {
    try {
        principal = config.getProperty(PRINCIPAL, principal);
        if (principal == null || principal.trim().length() == 0) {
            throw new ServletException("Principal not defined in configuration");
        }
        keytab = config.getProperty(KEYTAB, keytab);
        if (keytab == null || keytab.trim().length() == 0) {
            throw new ServletException("Keytab not defined in configuration");
        }
        if (!new File(keytab).exists()) {
            throw new ServletException("Keytab does not exist: " + keytab);
        }

        Set<Principal> principals = new HashSet<Principal>();
        principals.add(new KerberosPrincipal(principal));
        Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());

        KerberosConfiguration kerberosConfiguration = new KerberosConfiguration(keytab, principal);

        LOG.info("Login using keytab " + keytab + ", for principal " + principal);
        loginContext = new LoginContext("", subject, null, kerberosConfiguration);
        loginContext.login();

        Subject serverSubject = loginContext.getSubject();
        try {
            gssManager = Subject.doAs(serverSubject, new PrivilegedExceptionAction<GSSManager>() {

                @Override
                public GSSManager run() throws Exception {
                    return GSSManager.getInstance();
                }
            });
        } catch (PrivilegedActionException ex) {
            throw ex.getException();
        }
        LOG.info("Initialized, principal [{}] from keytab [{}]", principal, keytab);
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}

From source file:com.cloudera.alfredo.client.KerberosAuthenticator.java

/**
 * Implements the SPNEGO authentication sequence interaction using the current default principal
 * in the Kerberos cache (normally set via kinit).
 *
 * @param token the authencation token being used for the user.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication error occurred.
 *///  w  w  w .  j av  a  2  s  .c o  m
private void doSpnegoSequence(AuthenticatedURL.Token token) throws IOException, AuthenticationException {
    try {
        AccessControlContext context = AccessController.getContext();
        Subject subject = Subject.getSubject(context);
        if (subject == null) {
            subject = new Subject();
            LoginContext login = new LoginContext("", subject);
            login.login();
        }
        Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                GSSContext gssContext = null;
                try {
                    GSSManager gssManager = GSSManager.getInstance();
                    String servicePrincipal = "HTTP/" + KerberosAuthenticator.this.url.getHost();
                    GSSName serviceName = gssManager.createName(servicePrincipal,
                            GSSUtil.NT_GSS_KRB5_PRINCIPAL);
                    gssContext = gssManager.createContext(serviceName, GSSUtil.GSS_KRB5_MECH_OID, null,
                            GSSContext.DEFAULT_LIFETIME);
                    gssContext.requestCredDeleg(true);
                    gssContext.requestMutualAuth(true);

                    byte[] inToken = new byte[0];
                    byte[] outToken;
                    boolean established = false;

                    // Loop while the context is still not established
                    while (!established) {
                        outToken = gssContext.initSecContext(inToken, 0, inToken.length);
                        if (outToken != null) {
                            sendToken(outToken);
                        }

                        if (!gssContext.isEstablished()) {
                            inToken = readToken();
                        } else {
                            established = true;
                        }
                    }
                } finally {
                    if (gssContext != null) {
                        gssContext.dispose();
                    }
                }
                return null;
            }
        });
    } catch (PrivilegedActionException ex) {
        throw new AuthenticationException(ex.getException());
    } catch (LoginException ex) {
        throw new AuthenticationException(ex);
    }
    AuthenticatedURL.extractToken(conn, token);
}