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