001/*
002 * Copyright 2015-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-2024 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-2024 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.NotNull;
047import com.unboundid.util.StaticUtils;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050
051
052
053/**
054 * This class provides an {@link LDAPConnectionPoolHealthCheck} implementation
055 * that may be used to invoke a series of subordinate health checks and ensure
056 * that all of them consider a connection valid before indicating that the
057 * connection is valid.  If any of the subordinate health checks indicates that
058 * the connection is invalid, then the connection will be considered invalid.
059 */
060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
061public final class AggregateLDAPConnectionPoolHealthCheck
062       extends LDAPConnectionPoolHealthCheck
063{
064  // The list of subordinate health checks that will be invoked.
065  @NotNull private final List<LDAPConnectionPoolHealthCheck> healthChecks;
066
067
068
069  /**
070   * Creates a new instance of this LDAP connection pool health check.
071   *
072   * @param  healthChecks  The set of health checks that must all be satisfied
073   *                       in order to consider a connection valid.
074   */
075  public AggregateLDAPConnectionPoolHealthCheck(
076              @NotNull final LDAPConnectionPoolHealthCheck... healthChecks)
077  {
078    this(StaticUtils.toList(healthChecks));
079  }
080
081
082
083  /**
084   * Creates a new instance of this LDAP connection pool health check.
085   *
086   * @param  healthChecks  The set of health checks that must all be satisfied
087   *                       in order to consider a connection valid.
088   */
089  public AggregateLDAPConnectionPoolHealthCheck(
090              @NotNull final Collection<? extends LDAPConnectionPoolHealthCheck>
091                   healthChecks)
092  {
093    if (healthChecks == null)
094    {
095      this.healthChecks = Collections.emptyList();
096    }
097    else
098    {
099      this.healthChecks =
100           Collections.unmodifiableList(new ArrayList<>(healthChecks));
101    }
102  }
103
104
105
106  /**
107   * {@inheritDoc}
108   */
109  @Override()
110  public void ensureNewConnectionValid(@NotNull final LDAPConnection connection)
111         throws LDAPException
112  {
113    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
114    {
115      hc.ensureNewConnectionValid(connection);
116    }
117  }
118
119
120
121  /**
122   * {@inheritDoc}
123   */
124  @Override()
125  public void ensureConnectionValidAfterAuthentication(
126                   @NotNull final LDAPConnection connection,
127                   @NotNull final BindResult bindResult)
128         throws LDAPException
129  {
130    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
131    {
132      hc.ensureConnectionValidAfterAuthentication(connection, bindResult);
133    }
134  }
135
136
137
138  /**
139   * {@inheritDoc}
140   */
141  @Override()
142  public void ensureConnectionValidForCheckout(
143                   @NotNull final LDAPConnection connection)
144         throws LDAPException
145  {
146    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
147    {
148      hc.ensureConnectionValidForCheckout(connection);
149    }
150  }
151
152
153
154  /**
155   * {@inheritDoc}
156   */
157  @Override()
158  public void ensureConnectionValidForRelease(
159                   @NotNull final LDAPConnection connection)
160         throws LDAPException
161  {
162    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
163    {
164      hc.ensureConnectionValidForRelease(connection);
165    }
166  }
167
168
169
170  /**
171   * {@inheritDoc}
172   */
173  @Override()
174  public void ensureConnectionValidForContinuedUse(
175                   @NotNull final LDAPConnection connection)
176         throws LDAPException
177  {
178    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
179    {
180      hc.ensureConnectionValidForContinuedUse(connection);
181    }
182  }
183
184
185
186  /**
187   * {@inheritDoc}
188   */
189  @Override()
190  public void performPoolMaintenance(@NotNull final AbstractConnectionPool pool)
191  {
192    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
193    {
194      hc.performPoolMaintenance(pool);
195    }
196  }
197
198
199
200  /**
201   * {@inheritDoc}
202   */
203  @Override()
204  public void ensureConnectionValidAfterException(
205                   @NotNull final LDAPConnection connection,
206                   @NotNull final LDAPException exception)
207         throws LDAPException
208  {
209    for (final LDAPConnectionPoolHealthCheck hc : healthChecks)
210    {
211      hc.ensureConnectionValidAfterException(connection, exception);
212    }
213  }
214
215
216
217  /**
218   * {@inheritDoc}
219   */
220  @Override()
221  public void toString(@NotNull final StringBuilder buffer)
222  {
223    buffer.append("AggregateLDAPConnectionPoolHealthCheck(healthChecks={");
224
225    final Iterator<LDAPConnectionPoolHealthCheck> iterator =
226         healthChecks.iterator();
227    while (iterator.hasNext())
228    {
229      iterator.next().toString(buffer);
230      if (iterator.hasNext())
231      {
232        buffer.append(", ");
233      }
234    }
235
236    buffer.append("})");
237  }
238}