1 package io.github.jhipster.sample.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import io.github.jhipster.config.JHipsterConstants;
24 import io.github.jhipster.config.liquibase.AsyncSpringLiquibase;
25
26 import liquibase.integration.spring.SpringLiquibase;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Qualifier;
30 import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.annotation.Profile;
34 import org.springframework.core.env.Environment;
35 import org.springframework.core.task.TaskExecutor;
36 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
37 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
38 import org.springframework.transaction.annotation.EnableTransactionManagement;
39
40 import javax.sql.DataSource;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.sql.SQLException;
44 import org.springframework.context.annotation.ComponentScan.Filter;
45 import org.springframework.context.annotation.FilterType;
46
47 @Configuration
48 @EnableJpaRepositories("io.github.jhipster.sample.repository")
49 @EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
50 @EnableTransactionManagement
51 public class DatabaseConfiguration {
52
53 private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
54
55 private final Environment env;
56
57 public DatabaseConfiguration(Environment env) {
58 this.env = env;
59 }
60
61
62
63
64
65
66
67 @Bean(initMethod = "start", destroyMethod = "stop")
68 @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)
69 public Object h2TCPServer() throws SQLException {
70 try {
71
72
73 ClassLoader loader = Thread.currentThread().getContextClassLoader();
74 Class<?> serverClass = Class.forName("org.h2.tools.Server", true, loader);
75 Method createServer = serverClass.getMethod("createTcpServer", String[].class);
76 return createServer.invoke(null, new Object[] { new String[] { "-tcp", "-tcpAllowOthers" } });
77
78 } catch (ClassNotFoundException | LinkageError e) {
79 throw new RuntimeException("Failed to load and initialize org.h2.tools.Server", e);
80
81 } catch (SecurityException | NoSuchMethodException e) {
82 throw new RuntimeException("Failed to get method org.h2.tools.Server.createTcpServer()", e);
83
84 } catch (IllegalAccessException | IllegalArgumentException e) {
85 throw new RuntimeException("Failed to invoke org.h2.tools.Server.createTcpServer()", e);
86
87 } catch (InvocationTargetException e) {
88 Throwable t = e.getTargetException();
89 if (t instanceof SQLException) {
90 throw (SQLException) t;
91 }
92 throw new RuntimeException("Unchecked exception in org.h2.tools.Server.createTcpServer()", t);
93 }
94 }
95
96 @Bean
97 public SpringLiquibase liquibase(@Qualifier("taskExecutor") TaskExecutor taskExecutor,
98 DataSource dataSource, LiquibaseProperties liquibaseProperties) {
99
100
101 SpringLiquibase liquibase = new AsyncSpringLiquibase(taskExecutor, env);
102 liquibase.setDataSource(dataSource);
103 liquibase.setChangeLog("classpath:config/liquibase/master.xml");
104 liquibase.setContexts(liquibaseProperties.getContexts());
105 liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
106 liquibase.setDropFirst(liquibaseProperties.isDropFirst());
107 if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) {
108 liquibase.setShouldRun(false);
109 } else {
110 liquibase.setShouldRun(liquibaseProperties.isEnabled());
111 log.debug("Configuring Liquibase");
112 }
113 return liquibase;
114 }
115 }