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.annotation;
018
019import org.springframework.beans.factory.BeanFactory;
020import org.springframework.jmx.export.MBeanExporter;
021import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler;
022import org.springframework.jmx.export.naming.MetadataNamingStrategy;
023
024/**
025 * Convenient subclass of Spring's standard {@link MBeanExporter},
026 * activating Java 5 annotation usage for JMX exposure of Spring beans:
027 * {@link ManagedResource}, {@link ManagedAttribute}, {@link ManagedOperation}, etc.
028 *
029 * <p>Sets a {@link MetadataNamingStrategy} and a {@link MetadataMBeanInfoAssembler}
030 * with an {@link AnnotationJmxAttributeSource}, and activates the
031 * {@link #AUTODETECT_ALL} mode by default.
032 *
033 * @author Juergen Hoeller
034 * @since 2.5
035 */
036public class AnnotationMBeanExporter extends MBeanExporter {
037
038        private final AnnotationJmxAttributeSource annotationSource =
039                        new AnnotationJmxAttributeSource();
040
041        private final MetadataNamingStrategy metadataNamingStrategy =
042                        new MetadataNamingStrategy(this.annotationSource);
043
044        private final MetadataMBeanInfoAssembler metadataAssembler =
045                        new MetadataMBeanInfoAssembler(this.annotationSource);
046
047
048        public AnnotationMBeanExporter() {
049                setNamingStrategy(this.metadataNamingStrategy);
050                setAssembler(this.metadataAssembler);
051                setAutodetectMode(AUTODETECT_ALL);
052        }
053
054
055        /**
056         * Specify the default domain to be used for generating ObjectNames
057         * when no source-level metadata has been specified.
058         * <p>The default is to use the domain specified in the bean name
059         * (if the bean name follows the JMX ObjectName syntax); else,
060         * the package name of the managed bean class.
061         * @see MetadataNamingStrategy#setDefaultDomain
062         */
063        public void setDefaultDomain(String defaultDomain) {
064                this.metadataNamingStrategy.setDefaultDomain(defaultDomain);
065        }
066
067        @Override
068        public void setBeanFactory(BeanFactory beanFactory) {
069                super.setBeanFactory(beanFactory);
070                this.annotationSource.setBeanFactory(beanFactory);
071        }
072
073}