1 /*
2 * Copyright 2002-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.aop.support.AopUtils;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.jmx.export.metadata.JmxAttributeSource;
27 import org.springframework.jmx.export.metadata.ManagedResource;
28 import org.springframework.jmx.support.ObjectNameManager;
29 import org.springframework.util.Assert;
30 import org.springframework.util.ClassUtils;
31 import org.springframework.util.StringUtils;
32
33 /**
34 * An implementation of the {@link ObjectNamingStrategy} interface
35 * that reads the <code>ObjectName</code> from the source-level metadata.
36 * Falls back to the bean key (bean name) if no <code>ObjectName</code>
37 * can be found in source-level metadata.
38 *
39 * <p>Uses the {@link JmxAttributeSource} strategy interface, so that
40 * metadata can be read using any supported implementation. Out of the box,
41 * two strategies are included:
42 * <ul>
43 * <li><code>AttributesJmxAttributeSource</code>, for Commons Attributes
44 * <li><code>AnnotationJmxAttributeSource</code>, for JDK 1.5+ annotations
45 * </ul>
46 *
47 * @author Rob Harrop
48 * @author Juergen Hoeller
49 * @since 1.2
50 * @see ObjectNamingStrategy
51 */
52 public class MetadataNamingStrategy implements ObjectNamingStrategy, InitializingBean {
53
54 /**
55 * The <code>JmxAttributeSource</code> implementation to use for reading metadata.
56 */
57 private JmxAttributeSource attributeSource;
58
59 private String defaultDomain;
60
61
62 /**
63 * Create a new <code>MetadataNamingStrategy<code> which needs to be
64 * configured through the {@link #setAttributeSource} method.
65 */
66 public MetadataNamingStrategy() {
67 }
68
69 /**
70 * Create a new <code>MetadataNamingStrategy<code> for the given
71 * <code>JmxAttributeSource</code>.
72 * @param attributeSource the JmxAttributeSource to use
73 */
74 public MetadataNamingStrategy(JmxAttributeSource attributeSource) {
75 Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
76 this.attributeSource = attributeSource;
77 }
78
79
80 /**
81 * Set the implementation of the <code>JmxAttributeSource</code> interface to use
82 * when reading the source-level metadata.
83 */
84 public void setAttributeSource(JmxAttributeSource attributeSource) {
85 Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
86 this.attributeSource = attributeSource;
87 }
88
89 /**
90 * Specify the default domain to be used for generating ObjectNames
91 * when no source-level metadata has been specified.
92 * <p>The default is to use the domain specified in the bean name
93 * (if the bean name follows the JMX ObjectName syntax); else,
94 * the package name of the managed bean class.
95 */
96 public void setDefaultDomain(String defaultDomain) {
97 this.defaultDomain = defaultDomain;
98 }
99
100 public void afterPropertiesSet() {
101 if (this.attributeSource == null) {
102 throw new IllegalArgumentException("Property 'attributeSource' is required");
103 }
104 }
105
106
107 /**
108 * Reads the <code>ObjectName</code> from the source-level metadata associated
109 * with the managed resource's <code>Class</code>.
110 */
111 public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
112 Class managedClass = AopUtils.getTargetClass(managedBean);
113 ManagedResource mr = this.attributeSource.getManagedResource(managedClass);
114
115 // Check that an object name has been specified.
116 if (mr != null && StringUtils.hasText(mr.getObjectName())) {
117 return ObjectNameManager.getInstance(mr.getObjectName());
118 }
119 else {
120 try {
121 return ObjectNameManager.getInstance(beanKey);
122 }
123 catch (MalformedObjectNameException ex) {
124 String domain = this.defaultDomain;
125 if (domain == null) {
126 domain = ClassUtils.getPackageName(managedClass);
127 }
128 Hashtable properties = new Hashtable();
129 properties.put("type", ClassUtils.getShortName(managedClass));
130 properties.put("name", beanKey);
131 return ObjectNameManager.getInstance(domain, properties);
132 }
133 }
134 }
135
136 }