package at.knowcenter.wag.egov.egiz.ldap.client;
import iaik.asn1.ObjectID;
import iaik.asn1.structures.Name;
import iaik.utils.RFC2253NameParser;
import iaik.utils.RFC2253NameParserException;
import java.util.Hashtable;
import org.apache.log4j.Logger;
/**
* @author Thomas Knall
*/
public final class LDAPClientFactoryImpl extends LDAPClientFactory {
private final Logger log = Logger.getLogger(getClass());
// private Hashtable ldapClients;
private Hashtable ldapClients;
protected LDAPClientFactoryImpl() {
this.ldapClients = new Hashtable();
}
public static String rfc2253Name2Domain(Name name) {
Object[] values = name.getRDNValues(ObjectID.domainComponent);
if (values == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < values.length; i++) {
buffer.append(values[i]);
if (i+1 < values.length) {
buffer.append(".");
}
}
return buffer.toString();
}
public static String rfc2253Name2Domain(String nameString) throws RFC2253NameParserException {
RFC2253NameParser nameParser = new RFC2253NameParser(nameString);
return rfc2253Name2Domain(nameParser.parse());
}
private LDAPClient instantiateLDAPClient(String issuerName) throws LDAPException {
if (!super.hasBeenConfigured()) {
log.warn(super.getClass().getName() + " has not been configured yet.");
}
LDAPClient client = null;
LDAPMapping mapping = super.getMapping(issuerName);
if (mapping == null) {
try {
String alternativeURLString = rfc2253Name2Domain(issuerName);
if (alternativeURLString == null || alternativeURLString.length() == 0) {
throw new LDAPException("Neither issuer name \"" + issuerName + "\" has been registered nor domain components were provided.");
}
alternativeURLString = "ldap://" + alternativeURLString;
log.warn("Issuer name \"" + issuerName + "\" has not been registered; trying to instantiate client for url \"" + alternativeURLString + "\"...");
client = new LDAPClientImpl(alternativeURLString);
} catch (RFC2253NameParserException e) {
throw new LDAPException(e);
}
} else {
log.debug("Instantiating LDAP client for " + mapping + ".");
client = new LDAPClientImpl(mapping);
}
return client;
}
public synchronized LDAPClient createClient(String issuerName) throws LDAPException {
if (issuerName == null || issuerName.length() == 0) {
throw new NullPointerException("Issuer name must not be null or empty.");
}
LDAPClient ldapClient = (LDAPClient) ldapClients.get(issuerName);
if (ldapClient == null) {
ldapClient = instantiateLDAPClient(issuerName);
ldapClients.put(issuerName, ldapClient);
}
return ldapClient;
}
}