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.jmx; 018 019import java.util.Hashtable; 020 021import javax.management.MalformedObjectNameException; 022import javax.management.ObjectName; 023 024import org.springframework.beans.BeansException; 025import org.springframework.context.ApplicationContext; 026import org.springframework.context.ApplicationContextAware; 027import org.springframework.jmx.export.metadata.JmxAttributeSource; 028import org.springframework.jmx.export.naming.MetadataNamingStrategy; 029import org.springframework.jmx.support.ObjectNameManager; 030import org.springframework.util.ObjectUtils; 031 032/** 033 * Extension of {@link MetadataNamingStrategy} that supports a parent 034 * {@link ApplicationContext}. 035 * 036 * @author Dave Syer 037 * @since 1.1.1 038 */ 039public class ParentAwareNamingStrategy extends MetadataNamingStrategy 040 implements ApplicationContextAware { 041 042 private ApplicationContext applicationContext; 043 044 private boolean ensureUniqueRuntimeObjectNames; 045 046 public ParentAwareNamingStrategy(JmxAttributeSource attributeSource) { 047 super(attributeSource); 048 } 049 050 /** 051 * Set if unique runtime object names should be ensured. 052 * @param ensureUniqueRuntimeObjectNames {@code true} if unique names should ensured. 053 */ 054 public void setEnsureUniqueRuntimeObjectNames( 055 boolean ensureUniqueRuntimeObjectNames) { 056 this.ensureUniqueRuntimeObjectNames = ensureUniqueRuntimeObjectNames; 057 } 058 059 @Override 060 public ObjectName getObjectName(Object managedBean, String beanKey) 061 throws MalformedObjectNameException { 062 ObjectName name = super.getObjectName(managedBean, beanKey); 063 Hashtable<String, String> properties = new Hashtable<>(); 064 properties.putAll(name.getKeyPropertyList()); 065 if (this.ensureUniqueRuntimeObjectNames) { 066 properties.put("identity", ObjectUtils.getIdentityHexString(managedBean)); 067 } 068 else if (parentContextContainsSameBean(this.applicationContext, beanKey)) { 069 properties.put("context", 070 ObjectUtils.getIdentityHexString(this.applicationContext)); 071 } 072 return ObjectNameManager.getInstance(name.getDomain(), properties); 073 } 074 075 @Override 076 public void setApplicationContext(ApplicationContext applicationContext) 077 throws BeansException { 078 this.applicationContext = applicationContext; 079 } 080 081 private boolean parentContextContainsSameBean(ApplicationContext context, 082 String beanKey) { 083 if (context.getParent() == null) { 084 return false; 085 } 086 try { 087 this.applicationContext.getParent().getBean(beanKey); 088 return true; 089 } 090 catch (BeansException ex) { 091 return parentContextContainsSameBean(context.getParent(), beanKey); 092 } 093 } 094 095}