001    /*
002     * Copyright 2015-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2015-2016 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk;
022    
023    
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Collections;
028    import java.util.Iterator;
029    import java.util.List;
030    
031    import com.unboundid.util.StaticUtils;
032    import com.unboundid.util.ThreadSafety;
033    import com.unboundid.util.ThreadSafetyLevel;
034    
035    
036    
037    /**
038     * This class provides an {@link LDAPConnectionPoolHealthCheck} implementation
039     * that may be used to invoke a series of subordinate health checks and ensure
040     * that all of them consider a connection valid before indicating that the
041     * connection is valid.  If any of the subordinate health checks indicates that
042     * the connection is invalid, then the connection will be considered invalid.
043     */
044    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
045    public final class AggregateLDAPConnectionPoolHealthCheck
046           extends LDAPConnectionPoolHealthCheck
047    {
048      // The list of subordinate health checks that will be invoked.
049      private final List<LDAPConnectionPoolHealthCheck> healthChecks;
050    
051    
052    
053      /**
054       * Creates a new instance of this LDAP connection pool health check.
055       *
056       * @param  healthChecks  The set of health checks that must all be satisfied
057       *                       in order to consider a connection valid.
058       */
059      public AggregateLDAPConnectionPoolHealthCheck(
060                  final LDAPConnectionPoolHealthCheck... healthChecks)
061      {
062        this(StaticUtils.toList(healthChecks));
063      }
064    
065    
066    
067      /**
068       * Creates a new instance of this LDAP connection pool health check.
069       *
070       * @param  healthChecks  The set of health checks that must all be satisfied
071       *                       in order to consider a connection valid.
072       */
073      public AggregateLDAPConnectionPoolHealthCheck(
074           final Collection<? extends LDAPConnectionPoolHealthCheck> healthChecks)
075      {
076        if (healthChecks == null)
077        {
078          this.healthChecks = Collections.emptyList();
079        }
080        else
081        {
082          this.healthChecks = Collections.unmodifiableList(
083               new ArrayList<LDAPConnectionPoolHealthCheck>(healthChecks));
084        }
085      }
086    
087    
088    
089      /**
090       * {@inheritDoc}
091       */
092      @Override()
093      public void ensureNewConnectionValid(final LDAPConnection connection)
094             throws LDAPException
095      {
096        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
097        {
098          hc.ensureNewConnectionValid(connection);
099        }
100      }
101    
102    
103    
104      /**
105       * {@inheritDoc}
106       */
107      @Override()
108      public void ensureConnectionValidAfterAuthentication(
109                       final LDAPConnection connection,
110                       final BindResult bindResult)
111             throws LDAPException
112      {
113        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
114        {
115          hc.ensureConnectionValidAfterAuthentication(connection, bindResult);
116        }
117      }
118    
119    
120    
121      /**
122       * {@inheritDoc}
123       */
124      @Override()
125      public void ensureConnectionValidForCheckout(final LDAPConnection connection)
126             throws LDAPException
127      {
128        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
129        {
130          hc.ensureConnectionValidForCheckout(connection);
131        }
132      }
133    
134    
135    
136      /**
137       * {@inheritDoc}
138       */
139      @Override()
140      public void ensureConnectionValidForRelease(final LDAPConnection connection)
141             throws LDAPException
142      {
143        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
144        {
145          hc.ensureConnectionValidForRelease(connection);
146        }
147      }
148    
149    
150    
151      /**
152       * {@inheritDoc}
153       */
154      @Override()
155      public void ensureConnectionValidForContinuedUse(
156                       final LDAPConnection connection)
157             throws LDAPException
158      {
159        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
160        {
161          hc.ensureConnectionValidForContinuedUse(connection);
162        }
163      }
164    
165    
166    
167      /**
168       * {@inheritDoc}
169       */
170      @Override()
171      public void ensureConnectionValidAfterException(
172                       final LDAPConnection connection,
173                       final LDAPException exception)
174             throws LDAPException
175      {
176        for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
177        {
178          hc.ensureConnectionValidAfterException(connection, exception);
179        }
180      }
181    
182    
183    
184      /**
185       * {@inheritDoc}
186       */
187      @Override()
188      public void toString(final StringBuilder buffer)
189      {
190        buffer.append("AggregateLDAPConnectionPoolHealthCheck(healthChecks={");
191    
192        final Iterator<LDAPConnectionPoolHealthCheck> iterator =
193             healthChecks.iterator();
194        while (iterator.hasNext())
195        {
196          iterator.next().toString(buffer);
197          if (iterator.hasNext())
198          {
199            buffer.append(", ");
200          }
201        }
202    
203        buffer.append("})");
204      }
205    }