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 java.io.Closeable;
031
032import org.forgerock.util.promise.Promise;
033
034/**
035 * A connection factory provides an interface for obtaining a connection to a
036 * Directory Server. Connection factories can be used to wrap other connection
037 * factories in order to provide enhanced capabilities in a manner which is
038 * transparent to the application. For example:
039 * <ul>
040 * <li>Connection pooling
041 * <li>Load balancing
042 * <li>Keep alive
043 * <li>Transactional connections
044 * <li>Connections to LDIF files
045 * <li>Data transformations
046 * <li>Logging connections
047 * <li>Read-only connections
048 * <li>Pre-authenticated connections
049 * <li>Recording connections, with primitive roll-back functionality
050 * </ul>
051 * An application typically obtains a connection from a connection factory,
052 * performs one or more operations, and then closes the connection. Applications
053 * should aim to close connections as soon as possible in order to avoid
054 * resource contention.
055 */
056public interface ConnectionFactory extends Closeable {
057
058    /**
059     * Releases any resources associated with this connection factory. Depending
060     * on the implementation a factory may:
061     * <ul>
062     * <li>do nothing
063     * <li>close underlying connection factories (e.g. load-balancers)
064     * <li>close pooled connections (e.g. connection pools)
065     * <li>shutdown IO event service and related thread pools (e.g. Grizzly).
066     * </ul>
067     * Calling {@code close} on a connection factory which is already closed has
068     * no effect.
069     * <p>
070     * Applications should avoid closing connection factories while there are
071     * remaining active connections in use or connection attempts in progress.
072     *
073     * @see Connections#uncloseable(ConnectionFactory)
074     */
075    @Override
076    void close();
077
078    /**
079     * Asynchronously obtains a connection to the Directory Server associated
080     * with this connection factory. The returned {@code Promise} can be used to
081     * retrieve the completed connection.
082     *
083     * @return A promise which can be used to retrieve the connection.
084     */
085    Promise<Connection, LdapException> getConnectionAsync();
086
087    /**
088     * Returns a connection to the Directory Server associated with this
089     * connection factory. The connection returned by this method can be used
090     * immediately.
091     * <p>
092     * If the calling thread is interrupted while waiting for the connection
093     * attempt to complete then the calling thread unblock and throw a
094     * {@link CancelledResultException} whose cause is the underlying
095     * {@link InterruptedException}.
096     *
097     * @return A connection to the Directory Server associated with this
098     *         connection factory.
099     * @throws LdapException
100     *             If the connection request failed for some reason.
101     */
102    Connection getConnection() throws LdapException;
103}