001/*
002 * Copyright 2012-2016 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
017package org.springframework.boot.autoconfigure.integration;
018
019import javax.management.MBeanServer;
020
021import org.springframework.beans.BeansException;
022import org.springframework.beans.factory.BeanFactory;
023import org.springframework.beans.factory.BeanFactoryAware;
024import org.springframework.boot.autoconfigure.AutoConfigureAfter;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
028import org.springframework.boot.autoconfigure.condition.SearchStrategy;
029import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
030import org.springframework.boot.bind.RelaxedPropertyResolver;
031import org.springframework.context.EnvironmentAware;
032import org.springframework.context.annotation.Bean;
033import org.springframework.context.annotation.Configuration;
034import org.springframework.context.annotation.Import;
035import org.springframework.core.env.Environment;
036import org.springframework.integration.config.EnableIntegration;
037import org.springframework.integration.config.EnableIntegrationManagement;
038import org.springframework.integration.gateway.GatewayProxyFactoryBean;
039import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
040import org.springframework.integration.monitor.IntegrationMBeanExporter;
041import org.springframework.integration.support.management.IntegrationManagementConfigurer;
042import org.springframework.util.StringUtils;
043
044/**
045 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
046 * Auto-configuration} for Spring Integration.
047 *
048 * @author Artem Bilan
049 * @author Dave Syer
050 * @author Stephane Nicoll
051 * @since 1.1.0
052 */
053@Configuration
054@ConditionalOnClass(EnableIntegration.class)
055@AutoConfigureAfter(JmxAutoConfiguration.class)
056public class IntegrationAutoConfiguration {
057
058        /**
059         * Basic Spring Integration configuration.
060         */
061        @Configuration
062        @EnableIntegration
063        protected static class IntegrationConfiguration {
064
065        }
066
067        /**
068         * Spring Integration JMX configuration.
069         */
070        @Configuration
071        @ConditionalOnClass(EnableIntegrationMBeanExport.class)
072        @ConditionalOnMissingBean(value = IntegrationMBeanExporter.class, search = SearchStrategy.CURRENT)
073        @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
074        protected static class IntegrationJmxConfiguration
075                        implements EnvironmentAware, BeanFactoryAware {
076
077                private BeanFactory beanFactory;
078
079                private RelaxedPropertyResolver propertyResolver;
080
081                @Override
082                public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
083                        this.beanFactory = beanFactory;
084                }
085
086                @Override
087                public void setEnvironment(Environment environment) {
088                        this.propertyResolver = new RelaxedPropertyResolver(environment,
089                                        "spring.jmx.");
090                }
091
092                @Bean
093                public IntegrationMBeanExporter integrationMbeanExporter() {
094                        IntegrationMBeanExporter exporter = new IntegrationMBeanExporter();
095                        String defaultDomain = this.propertyResolver.getProperty("default-domain");
096                        if (StringUtils.hasLength(defaultDomain)) {
097                                exporter.setDefaultDomain(defaultDomain);
098                        }
099                        String server = this.propertyResolver.getProperty("server", "mbeanServer");
100                        if (StringUtils.hasLength(server)) {
101                                exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
102                        }
103                        return exporter;
104                }
105
106        }
107
108        /**
109         * Integration management configuration.
110         */
111        @Configuration
112        @ConditionalOnClass({ EnableIntegrationManagement.class,
113                        EnableIntegrationMBeanExport.class })
114        @ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class, name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, search = SearchStrategy.CURRENT)
115        @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
116        protected static class IntegrationManagementConfiguration {
117
118                @Configuration
119                @EnableIntegrationManagement(defaultCountsEnabled = "true", defaultStatsEnabled = "true")
120                protected static class EnableIntegrationManagementConfiguration {
121                }
122
123        }
124
125        /**
126         * Integration component scan configuration.
127         */
128        @ConditionalOnMissingBean(GatewayProxyFactoryBean.class)
129        @Import(IntegrationAutoConfigurationScanRegistrar.class)
130        protected static class IntegrationComponentScanAutoConfiguration {
131
132        }
133
134}