Package org.ldaptive
Class DnsSrvConnectionStrategy
- java.lang.Object
-
- org.ldaptive.DnsSrvConnectionStrategy
-
- All Implemented Interfaces:
ConnectionStrategy
public class DnsSrvConnectionStrategy extends Object implements ConnectionStrategy
DNS SRV connection strategy. Queries a DNS server for SRV records and uses those records to construct a list of URLs. A time to live can be set to control how often the DNS server is consulted. See http://www.ietf.org/rfc/rfc2782.txt.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
DnsSrvConnectionStrategy.SrvRecord
SRV record.
-
Field Summary
Fields Modifier and Type Field Description private static long
DEFAULT_TTL
Default time to live for DNS results.private static String
DNS_CONTEXT_FACTORY
JNDI context factory for DNS.private static String
DNS_PROVIDER_URL
JNDI context factory for DNS.private Map<String,Object>
jndiEnv
JNDI environment.protected Logger
logger
Logger for this class.private List<DnsSrvConnectionStrategy.SrvRecord>
srvRecords
SRV records from the last DNS lookup.private long
srvTtl
Time to live for SRV records in milliseconds.
-
Constructor Summary
Constructors Constructor Description DnsSrvConnectionStrategy()
Creates a new DNS SRV connection strategy.DnsSrvConnectionStrategy(long ttl)
Creates a new DNS SRV connection strategy.DnsSrvConnectionStrategy(Map<String,Object> env, long ttl)
Creates a new DNS SRV connection strategy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,Object>
getJndiEnvironment()
Returns the JNDI environment used for DNS lookup.String[]
getLdapUrls(ConnectionFactoryMetadata metadata)
Returns a list of URLs retrieved from DNS SRV records.long
getTimeToLive()
Returns the time that DNS lookups will be cached.protected List<DnsSrvConnectionStrategy.SrvRecord>
retrieveDNSRecords(String name, Map<String,Object> props, long ttl)
Uses JNDI to retrieve the DNS SRV record from the supplied url.void
setJndiEnvironment(Map<String,Object> env)
Sets the JNDI environment used for DNS lookups.void
setTimeToLive(long ttl)
Sets the time that DNS lookups will be cached.protected List<DnsSrvConnectionStrategy.SrvRecord>
sortSrvRecords(List<DnsSrvConnectionStrategy.SrvRecord> records)
Sorts the supplied SRV records according to RFC 2782.String
toString()
-
-
-
Field Detail
-
DNS_CONTEXT_FACTORY
private static final String DNS_CONTEXT_FACTORY
JNDI context factory for DNS.- See Also:
- Constant Field Values
-
DNS_PROVIDER_URL
private static final String DNS_PROVIDER_URL
JNDI context factory for DNS.- See Also:
- Constant Field Values
-
DEFAULT_TTL
private static final long DEFAULT_TTL
Default time to live for DNS results. Value is 3600000L.- See Also:
- Constant Field Values
-
logger
protected final Logger logger
Logger for this class.
-
srvTtl
private long srvTtl
Time to live for SRV records in milliseconds.
-
srvRecords
private List<DnsSrvConnectionStrategy.SrvRecord> srvRecords
SRV records from the last DNS lookup.
-
-
Method Detail
-
getJndiEnvironment
public Map<String,Object> getJndiEnvironment()
Returns the JNDI environment used for DNS lookup.- Returns:
- jndi environment
-
getTimeToLive
public long getTimeToLive()
Returns the time that DNS lookups will be cached.- Returns:
- time to live in milliseconds
-
setJndiEnvironment
public void setJndiEnvironment(Map<String,Object> env)
Sets the JNDI environment used for DNS lookups. If noContext.INITIAL_CONTEXT_FACTORY
is set, it is defaulted toDNS_CONTEXT_FACTORY
. If noContext.PROVIDER_URL
is set, it is defaulted toDNS_PROVIDER_URL
.- Parameters:
env
- jndi environment or null
-
setTimeToLive
public void setTimeToLive(long ttl)
Sets the time that DNS lookups will be cached.- Parameters:
ttl
- time to live in milliseconds
-
getLdapUrls
public String[] getLdapUrls(ConnectionFactoryMetadata metadata)
Returns a list of URLs retrieved from DNS SRV records. The LDAP URL in the supplied metadata can be a space delimited list of DNS servers, each will be tried in order.- Specified by:
getLdapUrls
in interfaceConnectionStrategy
- Parameters:
metadata
- which can be used to produce the URL list- Returns:
- list of URLs to attempt connections to
-
retrieveDNSRecords
protected List<DnsSrvConnectionStrategy.SrvRecord> retrieveDNSRecords(String name, Map<String,Object> props, long ttl) throws NamingException
Uses JNDI to retrieve the DNS SRV record from the supplied url. The supplied properties are passed into the JNDI context.- Parameters:
name
- of the SRV recordsprops
- for the JNDI contextttl
- time to live for each SRV record- Returns:
- list of LDAP URLs
- Throws:
NamingException
- if the DNS record cannot be retrieved
-
sortSrvRecords
protected List<DnsSrvConnectionStrategy.SrvRecord> sortSrvRecords(List<DnsSrvConnectionStrategy.SrvRecord> records)
Sorts the supplied SRV records according to RFC 2782. Records with the lowest priority are first. Records with the same priority are arranged by weight with higher weights having a greater chance to be ordered first.- Parameters:
records
- to sort- Returns:
- sorted records
-
-