001/* 002 * Copyright 2012-2017 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.autoconfigure; 018 019import java.lang.annotation.Annotation; 020 021import org.springframework.boot.autoconfigure.condition.ConditionMessage; 022import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 023import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 024import org.springframework.context.annotation.ConditionContext; 025import org.springframework.core.annotation.AnnotationAttributes; 026import org.springframework.core.env.Environment; 027import org.springframework.core.type.AnnotatedTypeMetadata; 028 029/** 030 * Base endpoint element condition. An element can be disabled globally via the 031 * {@code defaults} name or individually via the name of the element. 032 * 033 * @author Stephane Nicoll 034 * @author Madhura Bhave 035 * @since 2.0.0 036 */ 037public abstract class OnEndpointElementCondition extends SpringBootCondition { 038 039 private final String prefix; 040 041 private final Class<? extends Annotation> annotationType; 042 043 protected OnEndpointElementCondition(String prefix, 044 Class<? extends Annotation> annotationType) { 045 this.prefix = prefix; 046 this.annotationType = annotationType; 047 } 048 049 @Override 050 public ConditionOutcome getMatchOutcome(ConditionContext context, 051 AnnotatedTypeMetadata metadata) { 052 AnnotationAttributes annotationAttributes = AnnotationAttributes 053 .fromMap(metadata.getAnnotationAttributes(this.annotationType.getName())); 054 String endpointName = annotationAttributes.getString("value"); 055 ConditionOutcome outcome = getEndpointOutcome(context, endpointName); 056 if (outcome != null) { 057 return outcome; 058 } 059 return getDefaultEndpointsOutcome(context); 060 } 061 062 protected ConditionOutcome getEndpointOutcome(ConditionContext context, 063 String endpointName) { 064 Environment environment = context.getEnvironment(); 065 String enabledProperty = this.prefix + endpointName + ".enabled"; 066 if (environment.containsProperty(enabledProperty)) { 067 boolean match = environment.getProperty(enabledProperty, Boolean.class, true); 068 return new ConditionOutcome(match, 069 ConditionMessage.forCondition(this.annotationType).because( 070 this.prefix + endpointName + ".enabled is " + match)); 071 } 072 return null; 073 } 074 075 protected ConditionOutcome getDefaultEndpointsOutcome(ConditionContext context) { 076 boolean match = Boolean.valueOf(context.getEnvironment() 077 .getProperty(this.prefix + "defaults.enabled", "true")); 078 return new ConditionOutcome(match, 079 ConditionMessage.forCondition(this.annotationType).because( 080 this.prefix + "defaults.enabled is considered " + match)); 081 } 082 083}