01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.jmx.export.naming;
18:
19: import java.util.Hashtable;
20:
21: import javax.management.MalformedObjectNameException;
22: import javax.management.ObjectName;
23:
24: import org.springframework.jmx.support.ObjectNameManager;
25: import org.springframework.util.ClassUtils;
26: import org.springframework.util.ObjectUtils;
27:
28: /**
29: * An implementation of the <code>ObjectNamingStrategy</code> interface that
30: * creates a name based on the the identity of a given instance.
31: *
32: * <p>The resulting <code>ObjectName</code> will be in the form
33: * <i>package</i>:class=<i>class name</i>,hashCode=<i>identity hash (in hex)</i>
34: *
35: * @author Rob Harrop
36: * @author Juergen Hoeller
37: * @since 1.2
38: */
39: public class IdentityNamingStrategy implements ObjectNamingStrategy {
40:
41: public static final String TYPE_KEY = "type";
42:
43: public static final String HASH_CODE_KEY = "hashCode";
44:
45: /**
46: * Returns an instance of <code>ObjectName</code> based on the identity
47: * of the managed resource.
48: */
49: public ObjectName getObjectName(Object managedBean, String beanKey)
50: throws MalformedObjectNameException {
51: String domain = ClassUtils.getPackageName(managedBean
52: .getClass());
53: Hashtable keys = new Hashtable();
54: keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean
55: .getClass()));
56: keys.put(HASH_CODE_KEY, ObjectUtils
57: .getIdentityHexString(managedBean));
58: return ObjectNameManager.getInstance(domain, keys);
59: }
60:
61: }
|