Search in sources :

Example 1 with CommunicationException

use of javax.naming.CommunicationException in project hadoop by apache.

the class TestDNS method testRDNS.

/**
   * TestCase: get our local address and reverse look it up
   */
@Test
public void testRDNS() throws Exception {
    InetAddress localhost = getLocalIPAddr();
    try {
        String s = DNS.reverseDns(localhost, null);
        LOG.info("Local reverse DNS hostname is " + s);
    } catch (NameNotFoundException | CommunicationException e) {
        if (!localhost.isLinkLocalAddress() || localhost.isLoopbackAddress()) {
            //these addresses probably won't work with rDNS anyway, unless someone
            //has unusual entries in their DNS server mapping 1.0.0.127 to localhost
            LOG.info("Reverse DNS failing as due to incomplete networking", e);
            LOG.info("Address is " + localhost + " Loopback=" + localhost.isLoopbackAddress() + " Linklocal=" + localhost.isLinkLocalAddress());
        }
    }
}
Also used : CommunicationException(javax.naming.CommunicationException) NameNotFoundException(javax.naming.NameNotFoundException) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with CommunicationException

use of javax.naming.CommunicationException in project hadoop by apache.

the class TestLdapGroupsMapping method testGetGroupsWithLdapDown.

@Test
public void testGetGroupsWithLdapDown() throws IOException, NamingException {
    // This mocks the case where Ldap server is down, and always throws CommunicationException 
    when(getContext().search(anyString(), anyString(), any(Object[].class), any(SearchControls.class))).thenThrow(new CommunicationException("Connection is closed"));
    // Ldap server is down, no groups should be retrieved
    doTestGetGroups(Arrays.asList(new String[] {}), LdapGroupsMapping.RECONNECT_RETRY_COUNT);
}
Also used : CommunicationException(javax.naming.CommunicationException) SearchControls(javax.naming.directory.SearchControls) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 3 with CommunicationException

use of javax.naming.CommunicationException in project tomcat by apache.

the class JNDIRealm method getPrincipal.

@Override
protected Principal getPrincipal(String username, GSSCredential gssCredential) {
    DirContext context = null;
    Principal principal = null;
    try {
        // Ensure that we have a directory context available
        context = open();
        // time before giving up.
        try {
            // Authenticate the specified username if possible
            principal = getPrincipal(context, username, gssCredential);
        } catch (CommunicationException | ServiceUnavailableException e) {
            // log the exception so we know it's there.
            containerLog.info(sm.getString("jndiRealm.exception.retry"), e);
            // close the connection so we know it will be reopened.
            if (context != null)
                close(context);
            // open a new directory context.
            context = open();
            // Try the authentication again.
            principal = getPrincipal(context, username, gssCredential);
        }
        // Release this context
        release(context);
        // Return the authenticated Principal (if any)
        return principal;
    } catch (NamingException e) {
        // Log the problem for posterity
        containerLog.error(sm.getString("jndiRealm.exception"), e);
        // Close the connection so that it gets reopened next time
        if (context != null)
            close(context);
        // Return "not authenticated" for this request
        return null;
    }
}
Also used : CommunicationException(javax.naming.CommunicationException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Principal(java.security.Principal)

Example 4 with CommunicationException

use of javax.naming.CommunicationException in project neo4j by neo4j.

the class LdapRealm method getLdapContextUsingStartTls.

private LdapContext getLdapContextUsingStartTls(LdapContextFactory ldapContextFactory, Object principal, Object credentials) throws NamingException {
    JndiLdapContextFactory jndiLdapContextFactory = (JndiLdapContextFactory) ldapContextFactory;
    Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, jndiLdapContextFactory.getContextFactoryClassName());
    env.put(Context.PROVIDER_URL, jndiLdapContextFactory.getUrl());
    LdapContext ctx = null;
    try {
        ctx = new InitialLdapContext(env, null);
        StartTlsRequest startTlsRequest = new StartTlsRequest();
        StartTlsResponse tls = (StartTlsResponse) ctx.extendedOperation(startTlsRequest);
        tls.negotiate();
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, jndiLdapContextFactory.getAuthenticationMechanism());
        ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, principal);
        ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
        ctx.reconnect(ctx.getConnectControls());
        return ctx;
    } catch (IOException e) {
        LdapUtils.closeContext(ctx);
        securityLog.error(withRealm("Failed to negotiate TLS connection with '%s': ", server(jndiLdapContextFactory), e));
        throw new CommunicationException(e.getMessage());
    } catch (Throwable t) {
        LdapUtils.closeContext(ctx);
        securityLog.error(withRealm("Unexpected failure to negotiate TLS connection with '%s': ", server(jndiLdapContextFactory), t));
        throw t;
    }
}
Also used : StartTlsResponse(javax.naming.ldap.StartTlsResponse) CommunicationException(javax.naming.CommunicationException) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) IOException(java.io.IOException) StartTlsRequest(javax.naming.ldap.StartTlsRequest) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext) JndiLdapContextFactory(org.apache.shiro.realm.ldap.JndiLdapContextFactory)

Example 5 with CommunicationException

use of javax.naming.CommunicationException in project jdk8u_jdk by JetBrains.

the class Connections method get.

/**
     * Retrieves a PooledConnection from this list of connections.
     * Use an existing one if one is idle, or create one if the list's
     * max size hasn't been reached. If max size has been reached, wait
     * for a PooledConnection to be returned, or one to be removed (thus
     * not reaching the max size any longer).
     *
     * @param timeout if > 0, msec to wait until connection is available
     * @param factory creates the PooledConnection if one needs to be created
     *
     * @return A non-null PooledConnection
     * @throws NamingException PooledConnection cannot be created, because this
     * thread was interrupted while it waited for an available connection,
     * or if it timed out while waiting, or the creation of a connection
     * resulted in an error.
     */
synchronized PooledConnection get(long timeout, PooledConnectionFactory factory) throws NamingException {
    PooledConnection conn;
    long start = (timeout > 0 ? System.currentTimeMillis() : 0);
    long waittime = timeout;
    d("get(): before");
    while ((conn = getOrCreateConnection(factory)) == null) {
        if (timeout > 0 && waittime <= 0) {
            throw new CommunicationException("Timeout exceeded while waiting for a connection: " + timeout + "ms");
        }
        try {
            d("get(): waiting");
            if (waittime > 0) {
                // Wait until one is released or removed
                wait(waittime);
            } else {
                wait();
            }
        } catch (InterruptedException e) {
            throw new InterruptedNamingException("Interrupted while waiting for a connection");
        }
        // Check whether we timed out
        if (timeout > 0) {
            long now = System.currentTimeMillis();
            waittime = timeout - (now - start);
        }
    }
    d("get(): after");
    return conn;
}
Also used : CommunicationException(javax.naming.CommunicationException) InterruptedNamingException(javax.naming.InterruptedNamingException)

Aggregations

CommunicationException (javax.naming.CommunicationException)9 NamingException (javax.naming.NamingException)4 Principal (java.security.Principal)2 ServiceUnavailableException (javax.naming.ServiceUnavailableException)2 DirContext (javax.naming.directory.DirContext)2 InitialDirContext (javax.naming.directory.InitialDirContext)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 SQLException (java.sql.SQLException)1 Hashtable (java.util.Hashtable)1 InterruptedNamingException (javax.naming.InterruptedNamingException)1 NameNotFoundException (javax.naming.NameNotFoundException)1 SearchControls (javax.naming.directory.SearchControls)1 InitialLdapContext (javax.naming.ldap.InitialLdapContext)1 LdapContext (javax.naming.ldap.LdapContext)1 StartTlsRequest (javax.naming.ldap.StartTlsRequest)1 StartTlsResponse (javax.naming.ldap.StartTlsResponse)1 JndiLdapContextFactory (org.apache.shiro.realm.ldap.JndiLdapContextFactory)1 Status (org.compiere.interfaces.Status)1