01: /*
02: * Copyright 2002-2005 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: * @since 1.2
37: */
38: public class IdentityNamingStrategy implements ObjectNamingStrategy {
39:
40: public static final String TYPE_KEY = "type";
41:
42: public static final String HASH_CODE_KEY = "hashCode";
43:
44: /**
45: * Returns an instance of <code>ObjectName</code> based on the identity
46: * of the managed resource.
47: */
48: public ObjectName getObjectName(Object managedBean, String beanKey)
49: throws MalformedObjectNameException {
50: String domain = managedBean.getClass().getPackage().getName();
51:
52: Hashtable keys = new Hashtable();
53: keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean
54: .getClass()));
55: keys.put(HASH_CODE_KEY, ObjectUtils
56: .getIdentityHexString(managedBean));
57:
58: return ObjectNameManager.getInstance(domain, keys);
59: }
60:
61: }
|