package at.knowcenter.wag.egov.egiz.ldap.client; import java.util.Hashtable; import java.util.Iterator; import org.apache.log4j.Logger; import at.knowcenter.wag.egov.egiz.tools.Normalize; /** * @author Thomas Knall */ public abstract class LDAPClientFactory { private final Logger log = Logger.getLogger(getClass()); private static Hashtable ldapClientFactoryInstances = new Hashtable(); protected static final String DEFAULT_IDENTIFIER = "DEFAULT_IDENTIFIER"; private Hashtable registeredMappings; private boolean hasBeenConfigured; private Normalize normalizer; private boolean whiteSpaceRemoval; protected LDAPClientFactory() { this.registeredMappings = new Hashtable(); this.hasBeenConfigured = false; this.normalizer = null; this.whiteSpaceRemoval = false; } public void resetMappings() { this.registeredMappings = new Hashtable(); this.hasBeenConfigured = false; } public boolean hasBeenConfigured() { return this.hasBeenConfigured; } public LDAPMapping getMapping(String issuerName) { return this.registeredMappings.get(applyFilter(issuerName)); } protected Normalize getNormalizer() { return this.normalizer; } public synchronized void registerMappings(Iterable iterable) { Iterator it = iterable.iterator(); if (!it.hasNext()) { log.warn("There were no ldap mappings provided."); } else { this.hasBeenConfigured = true; } while (it.hasNext()) { this.registerMapping(it.next()); } } public synchronized void registerMapping(LDAPMapping... ldapMappings) { if (ldapMappings.length == 0) { log.warn("There were no ldap mappings provided."); } else { this.hasBeenConfigured = true; } for (LDAPMapping ldapMapping : ldapMappings) { log.debug("Registering Mapping for " + LDAPClientFactory.class.getSimpleName() + ": " + ldapMapping + "."); String issuerName = applyFilter(ldapMapping.getIssuerName().getName()); if (this.registeredMappings.containsKey(issuerName)) { log.warn("Skipping mapping for issuer name \"" + issuerName + "\" because it has already been registered."); } else { this.registeredMappings.put(issuerName, ldapMapping); } } } public void setNormalizer(Normalize normalizer) throws LDAPException { if (this.hasBeenConfigured) { throw new LDAPException("It is not allowed to set a normalizer after mappings have been defined."); } this.normalizer = normalizer; } public void setWhiteSpaceRemoval(boolean whiteSpaceRemoval) throws LDAPException { if (this.hasBeenConfigured) { throw new LDAPException("It is not allowed to set whitespace removal after mappings have been defined."); } this.whiteSpaceRemoval = whiteSpaceRemoval; } private String applyFilter(String text) { if (this.normalizer != null) { text = this.normalizer.normalize(text); } if (this.whiteSpaceRemoval) { text = text.replaceAll("\\s", ""); } return text; } public static synchronized LDAPClientFactory getInstance(String idenfifier) { LDAPClientFactory ldapClientFactoryInstance = ldapClientFactoryInstances.get(idenfifier); if (ldapClientFactoryInstance == null) { ldapClientFactoryInstance = new LDAPClientFactoryImpl(); ldapClientFactoryInstances.put(idenfifier, ldapClientFactoryInstance); } return ldapClientFactoryInstance; } public static synchronized LDAPClientFactory getInstance() { return getInstance(DEFAULT_IDENTIFIER); } public abstract LDAPClient createClient(String issuerName) throws LDAPException; }