/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ 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.ArrayList; import java.util.Collections; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger; /** * @author Thomas Knall */ public final class LDAPClientFactoryImpl extends LDAPClientFactory { private final Logger log = Logger.getLogger(getClass()); 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 List instantiateLDAPClients(String issuerName) throws LDAPException { if (!super.hasBeenConfigured()) { log.warn(super.getClass().getName() + " has not been configured yet."); } List ldapClients = new ArrayList(); List mappings = super.getMappings(issuerName); if (mappings == null || mappings.isEmpty()) { 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 + "\"..."); LDAPClient client = new LDAPClientImpl(alternativeURLString); ldapClients.add(client); } catch (RFC2253NameParserException e) { throw new LDAPException(e); } } else { log.debug("Instantiating LDAP clients for " + ArrayUtils.toString(mappings.toArray()) + "."); Iterator mappingIt = mappings.iterator(); while (mappingIt.hasNext()) { LDAPMapping mapping = (LDAPMapping) mappingIt.next(); ldapClients.add(new LDAPClientImpl(mapping)); } } return ldapClients; } public synchronized List createClients(String issuerName) throws LDAPException { if (issuerName == null) { throw new NullPointerException("Issuer name must not be null."); } if (issuerName.length() == 0) { throw new IllegalArgumentException("Issuer name must not be empty."); } List ldapClientList = (List) ldapClients.get(issuerName); if (ldapClientList == null) { ldapClientList = instantiateLDAPClients(issuerName); ldapClients.put(issuerName, ldapClientList); } return Collections.unmodifiableList(ldapClientList); } }