001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.actuate.ldap; 018 019import javax.naming.NamingException; 020import javax.naming.directory.DirContext; 021 022import org.springframework.boot.actuate.health.AbstractHealthIndicator; 023import org.springframework.boot.actuate.health.Health; 024import org.springframework.boot.actuate.health.HealthIndicator; 025import org.springframework.ldap.core.ContextExecutor; 026import org.springframework.ldap.core.LdapOperations; 027import org.springframework.util.Assert; 028 029/** 030 * {@link HealthIndicator} for configured LDAP server(s). 031 * 032 * @author EddĂș MelĂ©ndez 033 * @author Stephane Nicoll 034 * @version 2.0.0 035 */ 036public class LdapHealthIndicator extends AbstractHealthIndicator { 037 038 private static final ContextExecutor<String> versionContextExecutor = new VersionContextExecutor(); 039 040 private final LdapOperations ldapOperations; 041 042 public LdapHealthIndicator(LdapOperations ldapOperations) { 043 super("LDAP health check failed"); 044 Assert.notNull(ldapOperations, "LdapOperations must not be null"); 045 this.ldapOperations = ldapOperations; 046 } 047 048 @Override 049 protected void doHealthCheck(Health.Builder builder) throws Exception { 050 String version = this.ldapOperations.executeReadOnly(versionContextExecutor); 051 builder.up().withDetail("version", version); 052 } 053 054 private static class VersionContextExecutor implements ContextExecutor<String> { 055 056 @Override 057 public String executeWithContext(DirContext ctx) throws NamingException { 058 Object version = ctx.getEnvironment().get("java.naming.ldap.version"); 059 if (version != null) { 060 return (String) version; 061 } 062 return null; 063 } 064 065 } 066 067}