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());
}
}
}
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);
}
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;
}
}
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;
}
}
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;
}
Aggregations