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