001package com.nimbusds.common.ldap;
002
003
004import org.apache.logging.log4j.Logger;
005
006import com.codahale.metrics.health.HealthCheck;
007
008import com.unboundid.ldap.sdk.DN;
009import com.unboundid.ldap.sdk.Entry;
010import com.unboundid.ldap.sdk.LDAPException;
011import com.unboundid.ldap.sdk.LDAPInterface;
012
013
014/**
015 * Simple LDAP connection (individual or pool) health check. Retrieves a
016 * specified entry from the directory to determine the LDAP connection health.
017 */
018public class LDAPHealthCheck extends HealthCheck {
019
020
021        /**
022         * The LDAP interface.
023         */
024        private final LDAPInterface ldap;
025
026
027        /**
028         * LDAP entry to retrieve for the health check.
029         */
030        private final DN testEntry;
031
032
033        /**
034         * Optional logger for health check errors.
035         */
036        private final Logger log;
037
038
039        /**
040         * Creates a new LDAP connection pool health check.
041         *
042         * @param ldap      The LDAP store. Must not be {@code null}.
043         * @param testEntry LDAP entry to retrieve for the health check. Must
044         *                  not be {@code null}.
045         * @param log       Optional logger for health check errors,
046         *                  {@code null} if not required.
047         */
048        public LDAPHealthCheck(final LDAPInterface ldap,
049                               final DN testEntry,
050                               final Logger log) {
051
052                if (ldap == null) {
053                        throw new IllegalArgumentException("The LDAP interface must not be null");
054                }
055
056                this.ldap = ldap;
057
058                if (testEntry == null) {
059                        throw new IllegalArgumentException("The LDAP test entry must not be null");
060                }
061
062                this.testEntry = testEntry;
063
064                this.log = log;
065        }
066
067
068        @Override
069        public HealthCheck.Result check()
070                throws Exception {
071
072                final Entry entry;
073
074                try {
075                        entry = ldap.getEntry(testEntry.toString());
076
077                } catch (LDAPException e) {
078
079                        if (log != null) {
080                                log.warn("LDAP connector health check failure: {}", e.getMessage());
081                        }
082
083                        return HealthCheck.Result.unhealthy(e.getMessage());
084                }
085
086                if (entry == null) {
087
088                        final String msg = "LDAP connector health check failure: Missing test entry: " + testEntry;
089
090                        if (log != null) {
091                                log.warn(msg);
092                        }
093
094                        return HealthCheck.Result.unhealthy(msg);
095                }
096
097                if (log != null) {
098                        log.debug("LDAP connector health check success: healthy");
099                }
100
101                return HealthCheck.Result.healthy();
102        }
103}