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.autoconfigure.endpoint.condition;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
026import org.springframework.boot.actuate.endpoint.annotation.EndpointExtension;
027import org.springframework.context.annotation.Conditional;
028import org.springframework.core.env.Environment;
029
030/**
031 * {@link Conditional} that checks whether an endpoint is enabled or not. Matches
032 * according to the endpoints specific {@link Environment} property, falling back to
033 * {@code management.endpoints.enabled-by-default} or failing that
034 * {@link Endpoint#enableByDefault()}.
035 * <p>
036 * When placed on a {@code @Bean} method, the endpoint defaults to the return type of the
037 * factory method:
038 *
039 * <pre class="code">
040 * &#064;Configuration
041 * public class MyConfiguration {
042 *
043 *     &#064;ConditionalOnEnableEndpoint
044 *     &#064;Bean
045 *     public MyEndpoint myEndpoint() {
046 *         ...
047 *     }
048 *
049 * }</pre>
050 * <p>
051 * It is also possible to use the same mechanism for extensions:
052 *
053 * <pre class="code">
054 * &#064;Configuration
055 * public class MyConfiguration {
056 *
057 *     &#064;ConditionalOnEnableEndpoint
058 *     &#064;Bean
059 *     public MyEndpointWebExtension myEndpointWebExtension() {
060 *         ...
061 *     }
062 *
063 * }</pre>
064 * <p>
065 * In the sample above, {@code MyEndpointWebExtension} will be created if the endpoint is
066 * enabled as defined by the rules above. {@code MyEndpointWebExtension} must be a regular
067 * extension that refers to an endpoint, something like:
068 *
069 * <pre class="code">
070 * &#064;EndpointWebExtension(endpoint = MyEndpoint.class)
071 * public class MyEndpointWebExtension {
072 *
073 * }</pre>
074 * <p>
075 * Alternatively, the target endpoint can be manually specified for components that should
076 * only be created when a given endpoint is enabled:
077 *
078 * <pre class="code">
079 * &#064;Configuration
080 * public class MyConfiguration {
081 *
082 *     &#064;ConditionalOnEnableEndpoint(endpoint = MyEndpoint.class)
083 *     &#064;Bean
084 *     public MyComponent myComponent() {
085 *         ...
086 *     }
087 *
088 * }</pre>
089 *
090 * @author Stephane Nicoll
091 * @since 2.0.0
092 * @see Endpoint
093 */
094@Retention(RetentionPolicy.RUNTIME)
095@Target(ElementType.METHOD)
096@Documented
097@Conditional(OnEnabledEndpointCondition.class)
098public @interface ConditionalOnEnabledEndpoint {
099
100        /**
101         * The endpoint type that should be checked. Inferred when the return type of the
102         * {@code @Bean} method is either an {@link Endpoint} or an {@link EndpointExtension}.
103         * @return the endpoint type to check
104         * @since 2.0.6
105         */
106        Class<?> endpoint() default Void.class;
107
108}