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 *      Copyright 2013-2015 ForgeRock AS.
024 */
025
026package org.forgerock.opendj.ldap;
027
028import static com.forgerock.opendj.ldap.CoreMessages.LOAD_BALANCER_EVENT_LISTENER_LOG_OFFLINE;
029import static com.forgerock.opendj.ldap.CoreMessages.LOAD_BALANCER_EVENT_LISTENER_LOG_ONLINE;
030
031import java.util.EventListener;
032
033import org.forgerock.i18n.slf4j.LocalizedLogger;
034
035/**
036 * An object that registers to be notified when a connection factory associated
037 * with a load-balancer changes state from offline to online or vice-versa.
038 * <p>
039 * <b>NOTE:</b> load-balancer implementations must ensure that only one event is
040 * sent at a time. Event listener implementations should not need to be thread
041 * safe.
042 *
043 * @see LoadBalancingAlgorithm#LOAD_BALANCER_EVENT_LISTENER
044 */
045public interface LoadBalancerEventListener extends EventListener {
046    /**
047     * An event listener implementation which logs events to the LoadBalancingAlgorithm logger. This event listener is
048     * the default implementation configured using the {@link LoadBalancingAlgorithm#LOAD_BALANCER_EVENT_LISTENER}
049     * option.
050     */
051    LoadBalancerEventListener LOG_EVENTS = new LoadBalancerEventListener() {
052        private final LocalizedLogger logger = LocalizedLogger.getLocalizedLogger(LoadBalancingAlgorithm.class);
053
054        @Override
055        public void handleConnectionFactoryOnline(final ConnectionFactory factory) {
056            logger.info(LOAD_BALANCER_EVENT_LISTENER_LOG_ONLINE.get(factory));
057        }
058
059        @Override
060        public void handleConnectionFactoryOffline(final ConnectionFactory factory, final LdapException error) {
061            logger.warn(LOAD_BALANCER_EVENT_LISTENER_LOG_OFFLINE.get(factory, error.getMessage()));
062        }
063    };
064
065    /** An event listener implementation which ignores all events. */
066    LoadBalancerEventListener NO_OP = new LoadBalancerEventListener() {
067        @Override
068        public void handleConnectionFactoryOnline(final ConnectionFactory factory) {
069            // Do nothing.
070        }
071
072        @Override
073        public void handleConnectionFactoryOffline(final ConnectionFactory factory, final LdapException error) {
074            // Do nothing.
075        }
076    };
077
078    /**
079     * Invoked when the load-balancer is unable to obtain a connection from the
080     * specified connection factory. The connection factory will be removed from
081     * the load-balancer and monitored periodically in order to determine when
082     * it is available again, at which point an online notification event will
083     * occur.
084     *
085     * @param factory
086     *            The connection factory which has failed.
087     * @param error
088     *            The last error that occurred.
089     */
090    void handleConnectionFactoryOffline(ConnectionFactory factory, LdapException error);
091
092    /**
093     * Invoked when the load-balancer detects that a previously offline
094     * connection factory is available for use again.
095     *
096     * @param factory
097     *            The connection factory which is now available for use.
098     */
099    void handleConnectionFactoryOnline(ConnectionFactory factory);
100}