001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2012-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.json.resource; 018 019import java.io.Closeable; 020 021import org.forgerock.util.promise.Promise; 022 023/** 024 * A connection factory provides an interface for obtaining a connection to a 025 * JSON resource provider. Connection factories can be used to wrap other 026 * connection factories in order to provide enhanced capabilities in a manner 027 * which is transparent to the application. For example: 028 * <ul> 029 * <li>Connection pooling 030 * <li>Load balancing 031 * <li>Keep alive 032 * <li>Logging connections 033 * <li>Read-only connections 034 * </ul> 035 * An application typically obtains a connection from a connection factory, 036 * performs one or more operations, and then closes the connection. Applications 037 * should aim to close connections as soon as possible in order to avoid 038 * resource contention. 039 */ 040public interface ConnectionFactory extends Closeable { 041 042 /** 043 * Releases any resources associated with this connection factory. Depending 044 * on the implementation a factory may: 045 * <ul> 046 * <li>do nothing 047 * <li>close underlying connection factories (e.g. load-balancers) 048 * <li>close pooled connections (e.g. connection pools) 049 * <li>shutdown IO event service and related thread pools (e.g. Grizzly). 050 * </ul> 051 * Calling {@code close} on a connection factory which is already closed has 052 * no effect. 053 * <p> 054 * Applications should avoid closing connection factories while there are 055 * remaining active connections in use or connection attempts in progress. 056 * 057 * @see Resources#uncloseable(ConnectionFactory) 058 */ 059 @Override 060 void close(); 061 062 /** 063 * Returns a connection to the JSON resource provider associated with this 064 * connection factory. The connection returned by this method can be used 065 * immediately. 066 * 067 * @return A connection to the JSON resource provider associated with this 068 * connection factory. 069 * @throws ResourceException 070 * If the connection request failed for some reason. 071 */ 072 Connection getConnection() throws ResourceException; 073 074 /** 075 * Asynchronously obtains a connection to the JSON resource provider 076 * associated with this connection factory. The returned 077 * {@code FutureResult} can be used to retrieve the completed connection. 078 * 079 * @return A future which can be used to retrieve the connection. 080 */ 081 Promise<Connection, ResourceException> getConnectionAsync(); 082}