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.autoconfigure.admin; 018 019import javax.management.MalformedObjectNameException; 020 021import org.springframework.beans.factory.ObjectProvider; 022import org.springframework.boot.admin.SpringApplicationAdminMXBean; 023import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar; 024import org.springframework.boot.autoconfigure.AutoConfigureAfter; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 027import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.core.env.Environment; 031import org.springframework.jmx.export.MBeanExporter; 032 033/** 034 * Register a JMX component that allows to administer the current application. Intended 035 * for internal use only. 036 * 037 * @author Stephane Nicoll 038 * @author Andy Wilkinson 039 * @since 1.3.0 040 * @see SpringApplicationAdminMXBean 041 */ 042@Configuration 043@AutoConfigureAfter(JmxAutoConfiguration.class) 044@ConditionalOnProperty(prefix = "spring.application.admin", value = "enabled", havingValue = "true", matchIfMissing = false) 045public class SpringApplicationAdminJmxAutoConfiguration { 046 047 /** 048 * The property to use to customize the {@code ObjectName} of the application admin 049 * mbean. 050 */ 051 private static final String JMX_NAME_PROPERTY = "spring.application.admin.jmx-name"; 052 053 /** 054 * The default {@code ObjectName} of the application admin mbean. 055 */ 056 private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication"; 057 058 private final Iterable<MBeanExporter> mbeanExporters; 059 060 private final Environment environment; 061 062 public SpringApplicationAdminJmxAutoConfiguration( 063 ObjectProvider<MBeanExporter> mbeanExporters, Environment environment) { 064 this.mbeanExporters = mbeanExporters; 065 this.environment = environment; 066 } 067 068 @Bean 069 @ConditionalOnMissingBean 070 public SpringApplicationAdminMXBeanRegistrar springApplicationAdminRegistrar() 071 throws MalformedObjectNameException { 072 String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY, 073 DEFAULT_JMX_NAME); 074 if (this.mbeanExporters != null) { // Make sure to not register that MBean twice 075 for (MBeanExporter mbeanExporter : this.mbeanExporters) { 076 mbeanExporter.addExcludedBean(jmxName); 077 } 078 } 079 return new SpringApplicationAdminMXBeanRegistrar(jmxName); 080 } 081 082}