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