package at.knowcenter.wag.egov.egiz.ldap.client;
import iaik.asn1.structures.Name;
import iaik.utils.RFC2253NameParser;
import iaik.utils.RFC2253NameParserException;
import java.util.Hashtable;
import java.util.Iterator;
import org.apache.log4j.Logger;
/**
* @author Thomas Knall
*/
public class LDAPMappingStore {
// private Hashtable storedMappings;
private Hashtable storedMappings;
private LDAPIssuerNameFilter issuerNameFilter;
private Logger log = Logger.getLogger(getClass());
public LDAPMappingStore(LDAPIssuerNameFilter issuerNameFilter) {
this.storedMappings = new Hashtable();
this.issuerNameFilter = issuerNameFilter;
}
public LDAPMappingStore() {
this(null);
}
public void clearStore() {
this.storedMappings = new Hashtable();
}
public boolean isEmpty() {
return this.storedMappings.isEmpty();
}
public boolean isLDAPIssuerNameFilter() {
return this.issuerNameFilter != null;
}
public void storeMapping(LDAPMapping mapping) {
Name name = mapping.getIssuerName();
if (issuerNameFilter != null) {
name = this.issuerNameFilter.applyFilter(name);
}
if (this.storedMappings.containsKey(name)) {
log.warn("Skipping mapping \"" + mapping + "\" because is has already been stored under \"" + name.getName() + "\".");
} else {
log.debug("Storing mapping \"" + mapping + "\" under \"" + name.getName() + "\".");
this.storedMappings.put(name, mapping);
}
}
// public void storeMappings(Iterable iterable) {
// Iterator it = iterable.iterator();
// while (it.hasNext()) {
// this.storeMapping((LDAPMapping) it.next());
// }
// }
public LDAPMapping getMapping(Name name) {
if (issuerNameFilter != null) {
name = this.issuerNameFilter.applyFilter(name);
}
return (LDAPMapping) this.storedMappings.get(name);
}
public LDAPMapping getMapping(String nameString) throws LDAPException {
RFC2253NameParser parser = new RFC2253NameParser(nameString);
Name name;
try {
name = parser.parse();
} catch (RFC2253NameParserException e) {
throw new LDAPException(e);
}
if (issuerNameFilter != null) {
name = this.issuerNameFilter.applyFilter(name);
}
return getMapping(name);
}
}