001/*
002 * Copyright 2012-2018 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;
020import javax.sql.DataSource;
021
022import org.springframework.beans.BeansException;
023import org.springframework.beans.factory.BeanFactory;
024import org.springframework.beans.factory.BeanFactoryAware;
025import org.springframework.boot.autoconfigure.AutoConfigureAfter;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
029import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
030import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
031import org.springframework.boot.autoconfigure.condition.SearchStrategy;
032import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
033import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
034import org.springframework.boot.context.properties.EnableConfigurationProperties;
035import org.springframework.context.EnvironmentAware;
036import org.springframework.context.annotation.Bean;
037import org.springframework.context.annotation.Configuration;
038import org.springframework.context.annotation.Import;
039import org.springframework.core.env.Environment;
040import org.springframework.core.io.ResourceLoader;
041import org.springframework.integration.config.EnableIntegration;
042import org.springframework.integration.config.EnableIntegrationManagement;
043import org.springframework.integration.config.IntegrationManagementConfigurer;
044import org.springframework.integration.gateway.GatewayProxyFactoryBean;
045import org.springframework.integration.jdbc.store.JdbcMessageStore;
046import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
047import org.springframework.integration.monitor.IntegrationMBeanExporter;
048import org.springframework.util.StringUtils;
049
050/**
051 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
052 * Auto-configuration} for Spring Integration.
053 *
054 * @author Artem Bilan
055 * @author Dave Syer
056 * @author Stephane Nicoll
057 * @author Vedran Pavic
058 * @author Madhura Bhave
059 * @since 1.1.0
060 */
061@Configuration
062@ConditionalOnClass(EnableIntegration.class)
063@EnableConfigurationProperties(IntegrationProperties.class)
064@AutoConfigureAfter({ DataSourceAutoConfiguration.class, JmxAutoConfiguration.class })
065public class IntegrationAutoConfiguration {
066
067        /**
068         * Basic Spring Integration configuration.
069         */
070        @Configuration
071        @EnableIntegration
072        protected static class IntegrationConfiguration {
073
074        }
075
076        /**
077         * Spring Integration JMX configuration.
078         */
079        @Configuration
080        @ConditionalOnClass(EnableIntegrationMBeanExport.class)
081        @ConditionalOnMissingBean(value = IntegrationMBeanExporter.class, search = SearchStrategy.CURRENT)
082        @ConditionalOnBean(MBeanServer.class)
083        @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
084        protected static class IntegrationJmxConfiguration
085                        implements EnvironmentAware, BeanFactoryAware {
086
087                private BeanFactory beanFactory;
088
089                private Environment environment;
090
091                @Override
092                public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
093                        this.beanFactory = beanFactory;
094                }
095
096                @Override
097                public void setEnvironment(Environment environment) {
098                        this.environment = environment;
099                }
100
101                @Bean
102                public IntegrationMBeanExporter integrationMbeanExporter() {
103                        IntegrationMBeanExporter exporter = new IntegrationMBeanExporter();
104                        String defaultDomain = this.environment
105                                        .getProperty("spring.jmx.default-domain");
106                        if (StringUtils.hasLength(defaultDomain)) {
107                                exporter.setDefaultDomain(defaultDomain);
108                        }
109                        String serverBean = this.environment.getProperty("spring.jmx.server",
110                                        "mbeanServer");
111                        exporter.setServer(this.beanFactory.getBean(serverBean, MBeanServer.class));
112                        return exporter;
113                }
114
115        }
116
117        /**
118         * Integration management configuration.
119         */
120        @Configuration
121        @ConditionalOnClass(EnableIntegrationManagement.class)
122        @ConditionalOnMissingBean(value = IntegrationManagementConfigurer.class, name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, search = SearchStrategy.CURRENT)
123        protected static class IntegrationManagementConfiguration {
124
125                @Configuration
126                @EnableIntegrationManagement(defaultCountsEnabled = "true")
127                protected static class EnableIntegrationManagementConfiguration {
128
129                }
130
131        }
132
133        /**
134         * Integration component scan configuration.
135         */
136        @Configuration
137        @ConditionalOnMissingBean(GatewayProxyFactoryBean.class)
138        @Import(IntegrationAutoConfigurationScanRegistrar.class)
139        protected static class IntegrationComponentScanConfiguration {
140
141        }
142
143        /**
144         * Integration JDBC configuration.
145         */
146        @Configuration
147        @ConditionalOnClass(JdbcMessageStore.class)
148        @ConditionalOnSingleCandidate(DataSource.class)
149        protected static class IntegrationJdbcConfiguration {
150
151                @Bean
152                @ConditionalOnMissingBean
153                public IntegrationDataSourceInitializer integrationDataSourceInitializer(
154                                DataSource dataSource, ResourceLoader resourceLoader,
155                                IntegrationProperties properties) {
156                        return new IntegrationDataSourceInitializer(dataSource, resourceLoader,
157                                        properties);
158                }
159
160        }
161
162}