Skip to content

Commit 48bc29c

Browse files
vpavicsnicoll
authored andcommitted
Add database initializer for Spring Integration
See gh-8881
1 parent 50b3b30 commit 48bc29c

File tree

7 files changed

+298
-10
lines changed

7 files changed

+298
-10
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@
329329
<artifactId>spring-integration-core</artifactId>
330330
<optional>true</optional>
331331
</dependency>
332+
<dependency>
333+
<groupId>org.springframework.integration</groupId>
334+
<artifactId>spring-integration-jdbc</artifactId>
335+
<optional>true</optional>
336+
</dependency>
332337
<dependency>
333338
<groupId>org.springframework.integration</groupId>
334339
<artifactId>spring-integration-jmx</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,25 +17,35 @@
1717
package org.springframework.boot.autoconfigure.integration;
1818

1919
import javax.management.MBeanServer;
20+
import javax.sql.DataSource;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.beans.factory.BeanFactory;
2324
import org.springframework.beans.factory.BeanFactoryAware;
2425
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
26+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2528
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2629
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2730
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2832
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
2933
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
3034
import org.springframework.boot.bind.RelaxedPropertyResolver;
35+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3136
import org.springframework.context.EnvironmentAware;
3237
import org.springframework.context.annotation.Bean;
38+
import org.springframework.context.annotation.Conditional;
3339
import org.springframework.context.annotation.Configuration;
3440
import org.springframework.context.annotation.Import;
3541
import org.springframework.core.env.Environment;
42+
import org.springframework.core.io.ResourceLoader;
3643
import org.springframework.integration.config.EnableIntegration;
3744
import org.springframework.integration.config.EnableIntegrationManagement;
3845
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
46+
import org.springframework.integration.jdbc.lock.DefaultLockRepository;
47+
import org.springframework.integration.jdbc.store.JdbcChannelMessageStore;
48+
import org.springframework.integration.jdbc.store.JdbcMessageStore;
3949
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
4050
import org.springframework.integration.monitor.IntegrationMBeanExporter;
4151
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
@@ -48,10 +58,12 @@
4858
* @author Artem Bilan
4959
* @author Dave Syer
5060
* @author Stephane Nicoll
61+
* @author Vedran Pavic
5162
* @since 1.1.0
5263
*/
5364
@Configuration
5465
@ConditionalOnClass(EnableIntegration.class)
66+
@EnableConfigurationProperties(IntegrationProperties.class)
5567
@AutoConfigureAfter(JmxAutoConfiguration.class)
5668
public class IntegrationAutoConfiguration {
5769

@@ -131,4 +143,47 @@ protected static class IntegrationComponentScanAutoConfiguration {
131143

132144
}
133145

146+
/**
147+
* Integration JDBC configuration.
148+
*/
149+
@Configuration
150+
@ConditionalOnClass(JdbcMessageStore.class)
151+
@ConditionalOnSingleCandidate(DataSource.class)
152+
protected static class IntegrationJdbcConfiguration {
153+
154+
@Bean
155+
@ConditionalOnMissingBean
156+
@Conditional(IntegrationSchemaCondition.class)
157+
public IntegrationDatabaseInitializer integrationDatabaseInitializer(
158+
DataSource dataSource, ResourceLoader resourceLoader,
159+
IntegrationProperties properties) {
160+
return new IntegrationDatabaseInitializer(dataSource, resourceLoader,
161+
properties);
162+
}
163+
164+
}
165+
166+
static class IntegrationSchemaCondition extends AnyNestedCondition {
167+
168+
IntegrationSchemaCondition() {
169+
super(ConfigurationPhase.REGISTER_BEAN);
170+
}
171+
172+
@ConditionalOnBean(JdbcMessageStore.class)
173+
static class JdbcMessageStoreUsed {
174+
175+
}
176+
177+
@ConditionalOnBean(JdbcChannelMessageStore.class)
178+
static class JdbcChannelMessageStoreUsed {
179+
180+
}
181+
182+
@ConditionalOnBean(DefaultLockRepository.class)
183+
static class DefaultLockRepositoryUsed {
184+
185+
}
186+
187+
}
188+
134189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.integration;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
22+
import org.springframework.core.io.ResourceLoader;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* Initializer for Spring Integration schema.
27+
*
28+
* @author Vedran Pavic
29+
* @since 2.0.0
30+
*/
31+
public class IntegrationDatabaseInitializer extends AbstractDatabaseInitializer {
32+
33+
private final IntegrationProperties.Jdbc properties;
34+
35+
public IntegrationDatabaseInitializer(DataSource dataSource,
36+
ResourceLoader resourceLoader, IntegrationProperties properties) {
37+
super(dataSource, resourceLoader);
38+
Assert.notNull(properties, "IntegrationProperties must not be null");
39+
this.properties = properties.getJdbc();
40+
}
41+
42+
@Override
43+
protected boolean isEnabled() {
44+
return this.properties.getInitializer().isEnabled();
45+
}
46+
47+
@Override
48+
protected String getSchemaLocation() {
49+
return this.properties.getSchema();
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.integration;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for Spring Integration.
23+
*
24+
* @author Vedran Pavic
25+
* @since 2.0.0
26+
*/
27+
@ConfigurationProperties(prefix = "spring.integration")
28+
public class IntegrationProperties {
29+
30+
private final Jdbc jdbc = new Jdbc();
31+
32+
public Jdbc getJdbc() {
33+
return this.jdbc;
34+
}
35+
36+
public static class Jdbc {
37+
38+
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
39+
+ "integration/jdbc/schema-@@platform@@.sql";
40+
41+
/**
42+
* Path to the SQL file to use to initialize the database schema.
43+
*/
44+
private String schema = DEFAULT_SCHEMA_LOCATION;
45+
46+
private final Initializer initializer = new Initializer();
47+
48+
public String getSchema() {
49+
return this.schema;
50+
}
51+
52+
public void setSchema(String schema) {
53+
this.schema = schema;
54+
}
55+
56+
public Initializer getInitializer() {
57+
return this.initializer;
58+
}
59+
60+
public class Initializer {
61+
62+
/**
63+
* Create the required integration tables on startup if necessary.
64+
*/
65+
private boolean enabled = true;
66+
67+
public boolean isEnabled() {
68+
return this.enabled;
69+
}
70+
71+
public void setEnabled(boolean enabled) {
72+
this.enabled = enabled;
73+
}
74+
75+
}
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)