001/* 002 * Copyright 2002-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 * https://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.jmx.export.naming; 018 019import java.util.Hashtable; 020 021import javax.management.MalformedObjectNameException; 022import javax.management.ObjectName; 023 024import org.springframework.jmx.support.ObjectNameManager; 025import org.springframework.lang.Nullable; 026import org.springframework.util.ClassUtils; 027import org.springframework.util.ObjectUtils; 028 029/** 030 * An implementation of the {@code ObjectNamingStrategy} interface that 031 * creates a name based on the identity of a given instance. 032 * 033 * <p>The resulting {@code ObjectName} will be in the form 034 * <i>package</i>:class=<i>class name</i>,hashCode=<i>identity hash (in hex)</i> 035 * 036 * @author Rob Harrop 037 * @author Juergen Hoeller 038 * @since 1.2 039 */ 040public class IdentityNamingStrategy implements ObjectNamingStrategy { 041 042 /** 043 * The type key. 044 */ 045 public static final String TYPE_KEY = "type"; 046 047 /** 048 * The hash code key. 049 */ 050 public static final String HASH_CODE_KEY = "hashCode"; 051 052 053 /** 054 * Returns an instance of {@code ObjectName} based on the identity 055 * of the managed resource. 056 */ 057 @Override 058 public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException { 059 String domain = ClassUtils.getPackageName(managedBean.getClass()); 060 Hashtable<String, String> keys = new Hashtable<>(); 061 keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass())); 062 keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean)); 063 return ObjectNameManager.getInstance(domain, keys); 064 } 065 066}