View Javadoc
1   package io.github.jhipster.sample.config;
2   
3   /*-
4    * #%L
5    * Jhipster Sample Application
6    * %%
7    * Copyright (C) 2017 Osgiliath
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
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       * Open the TCP port for the H2 database, so it is available remotely.
63       *
64       * @return the H2 database TCP server
65       * @throws SQLException if the server failed to start
66       */
67      @Bean(initMethod = "start", destroyMethod = "stop")
68      @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)
69      public Object h2TCPServer() throws SQLException {
70          try {
71              // We don't want to include H2 when we are packaging for the "prod" profile and won't
72              // actually need it, so we have to load / invoke things at runtime through reflection.
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         // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start asynchronously
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 }