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