001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.jmx.export.naming;
018:
019: import java.util.Hashtable;
020:
021: import javax.management.MalformedObjectNameException;
022: import javax.management.ObjectName;
023:
024: import org.springframework.aop.support.AopUtils;
025: import org.springframework.beans.factory.InitializingBean;
026: import org.springframework.jmx.export.metadata.JmxAttributeSource;
027: import org.springframework.jmx.export.metadata.ManagedResource;
028: import org.springframework.jmx.support.ObjectNameManager;
029: import org.springframework.util.Assert;
030: import org.springframework.util.ClassUtils;
031: import org.springframework.util.StringUtils;
032:
033: /**
034: * An implementation of the {@link ObjectNamingStrategy} interface
035: * that reads the <code>ObjectName</code> from the source-level metadata.
036: * Falls back to the bean key (bean name) if no <code>ObjectName</code>
037: * can be found in source-level metadata.
038: *
039: * <p>Uses the {@link JmxAttributeSource} strategy interface, so that
040: * metadata can be read using any supported implementation. Out of the box,
041: * two strategies are included:
042: * <ul>
043: * <li><code>AttributesJmxAttributeSource</code>, for Commons Attributes
044: * <li><code>AnnotationJmxAttributeSource</code>, for JDK 1.5+ annotations
045: * </ul>
046: *
047: * @author Rob Harrop
048: * @author Juergen Hoeller
049: * @since 1.2
050: * @see ObjectNamingStrategy
051: */
052: public class MetadataNamingStrategy implements ObjectNamingStrategy,
053: InitializingBean {
054:
055: /**
056: * The <code>JmxAttributeSource</code> implementation to use for reading metadata.
057: */
058: private JmxAttributeSource attributeSource;
059:
060: private String defaultDomain;
061:
062: /**
063: * Create a new <code>MetadataNamingStrategy<code> which needs to be
064: * configured through the {@link #setAttributeSource} method.
065: */
066: public MetadataNamingStrategy() {
067: }
068:
069: /**
070: * Create a new <code>MetadataNamingStrategy<code> for the given
071: * <code>JmxAttributeSource</code>.
072: * @param attributeSource the JmxAttributeSource to use
073: */
074: public MetadataNamingStrategy(JmxAttributeSource attributeSource) {
075: Assert.notNull(attributeSource,
076: "JmxAttributeSource must not be null");
077: this .attributeSource = attributeSource;
078: }
079:
080: /**
081: * Set the implementation of the <code>JmxAttributeSource</code> interface to use
082: * when reading the source-level metadata.
083: */
084: public void setAttributeSource(JmxAttributeSource attributeSource) {
085: Assert.notNull(attributeSource,
086: "JmxAttributeSource must not be null");
087: this .attributeSource = attributeSource;
088: }
089:
090: /**
091: * Specify the default domain to be used for generating ObjectNames
092: * when no source-level metadata has been specified.
093: * <p>The default is to use the domain specified in the bean name
094: * (if the bean name follows the JMX ObjectName syntax); else,
095: * the package name of the managed bean class.
096: */
097: public void setDefaultDomain(String defaultDomain) {
098: this .defaultDomain = defaultDomain;
099: }
100:
101: public void afterPropertiesSet() {
102: if (this .attributeSource == null) {
103: throw new IllegalArgumentException(
104: "Property 'attributeSource' is required");
105: }
106: }
107:
108: /**
109: * Reads the <code>ObjectName</code> from the source-level metadata associated
110: * with the managed resource's <code>Class</code>.
111: */
112: public ObjectName getObjectName(Object managedBean, String beanKey)
113: throws MalformedObjectNameException {
114: Class managedClass = AopUtils.getTargetClass(managedBean);
115: ManagedResource mr = this .attributeSource
116: .getManagedResource(managedClass);
117:
118: // Check that an object name has been specified.
119: if (mr != null && StringUtils.hasText(mr.getObjectName())) {
120: return ObjectNameManager.getInstance(mr.getObjectName());
121: } else {
122: try {
123: return ObjectNameManager.getInstance(beanKey);
124: } catch (MalformedObjectNameException ex) {
125: String domain = this .defaultDomain;
126: if (domain == null) {
127: domain = ClassUtils.getPackageName(managedClass);
128: }
129: Hashtable properties = new Hashtable();
130: properties.put("type", ClassUtils
131: .getShortName(managedClass));
132: properties.put("name", beanKey);
133: return ObjectNameManager
134: .getInstance(domain, properties);
135: }
136: }
137: }
138:
139: }
|