001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2009-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2011-2014 ForgeRock AS. 026 */ 027 028package org.forgerock.opendj.ldap; 029 030import static com.forgerock.opendj.util.StaticUtils.getProvider; 031 032import org.forgerock.opendj.ldap.spi.LDAPConnectionFactoryImpl; 033import org.forgerock.opendj.ldap.spi.TransportProvider; 034import org.forgerock.util.Reject; 035import org.forgerock.util.promise.Promise; 036 037/** 038 * A factory class which can be used to obtain connections to an LDAP Directory 039 * Server. 040 */ 041public final class LDAPConnectionFactory implements ConnectionFactory { 042 /** 043 * We implement the factory using the pimpl idiom in order to avoid making 044 * too many implementation classes public. 045 */ 046 private final LDAPConnectionFactoryImpl impl; 047 048 /** 049 * Transport provider that provides the implementation of this factory. 050 */ 051 private final TransportProvider provider; 052 053 /** 054 * Creates a new LDAP connection factory which can be used to create LDAP 055 * connections to the Directory Server at the provided host and port 056 * number. 057 * 058 * @param host 059 * The host name. 060 * @param port 061 * The port number. 062 * @throws NullPointerException 063 * If {@code host} was {@code null}. 064 * @throws ProviderNotFoundException if no provider is available or if the 065 * provider requested using options is not found. 066 */ 067 public LDAPConnectionFactory(final String host, final int port) { 068 this(host, port, new LDAPOptions()); 069 } 070 071 /** 072 * Creates a new LDAP connection factory which can be used to create LDAP 073 * connections to the Directory Server at the provided host and port 074 * number. 075 * 076 * @param host 077 * The host name. 078 * @param port 079 * The port number. 080 * @param options 081 * The LDAP options to use when creating connections. 082 * @throws NullPointerException 083 * If {@code host} or {@code options} was {@code null}. 084 * @throws ProviderNotFoundException if no provider is available or if the 085 * provider requested using options is not found. 086 */ 087 public LDAPConnectionFactory(final String host, final int port, final LDAPOptions options) { 088 Reject.ifNull(host, options); 089 this.provider = getProvider(TransportProvider.class, options.getTransportProvider(), 090 options.getProviderClassLoader()); 091 this.impl = provider.getLDAPConnectionFactory(host, port, options); 092 } 093 094 @Override 095 public void close() { 096 impl.close(); 097 } 098 099 @Override 100 public Promise<Connection, LdapException> getConnectionAsync() { 101 return impl.getConnectionAsync(); 102 } 103 104 @Override 105 public Connection getConnection() throws LdapException { 106 return impl.getConnection(); 107 } 108 109 /** 110 * Returns the host name of the Directory Server. The returned host name is 111 * the same host name that was provided during construction and may be an IP 112 * address. More specifically, this method will not perform a reverse DNS 113 * lookup. 114 * 115 * @return The host name of the Directory Server. 116 */ 117 public String getHostName() { 118 return impl.getHostName(); 119 } 120 121 /** 122 * Returns the port of the Directory Server. 123 * 124 * @return The port of the Directory Server. 125 */ 126 public int getPort() { 127 return impl.getPort(); 128 } 129 130 /** 131 * Returns the name of the transport provider, which provides the implementation 132 * of this factory. 133 * 134 * @return The name of actual transport provider. 135 */ 136 public String getProviderName() { 137 return provider.getName(); 138 } 139 140 @Override 141 public String toString() { 142 return impl.toString(); 143 } 144}