1
+ package com .docker .ddev .configuration ;
2
+
3
+ import java .util .Properties ;
4
+ import java .io .BufferedReader ;
5
+ import java .io .FileReader ;
6
+ import java .io .IOException ;
7
+
8
+ import javax .naming .NamingException ;
9
+ import javax .persistence .EntityManagerFactory ;
10
+ import javax .sql .DataSource ;
11
+
12
+ import org .apache .commons .lang3 .StringUtils ;
13
+ import org .springframework .beans .factory .annotation .Autowired ;
14
+ import org .springframework .boot .autoconfigure .jdbc .DataSourceBuilder ;
15
+ import org .springframework .boot .autoconfigure .jdbc .DataSourceProperties ;
16
+ import org .springframework .boot .context .properties .ConfigurationProperties ;
17
+ import org .springframework .context .annotation .Bean ;
18
+ import org .springframework .context .annotation .Configuration ;
19
+ import org .springframework .context .annotation .Primary ;
20
+ import org .springframework .core .env .Environment ;
21
+ import org .springframework .data .jpa .repository .config .EnableJpaRepositories ;
22
+ import org .springframework .orm .jpa .JpaTransactionManager ;
23
+ import org .springframework .orm .jpa .JpaVendorAdapter ;
24
+ import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
25
+ import org .springframework .orm .jpa .vendor .HibernateJpaVendorAdapter ;
26
+ import org .springframework .transaction .PlatformTransactionManager ;
27
+ import org .springframework .transaction .annotation .EnableTransactionManagement ;
28
+
29
+ import com .zaxxer .hikari .HikariDataSource ;
30
+
31
+ @ Configuration
32
+ @ EnableJpaRepositories (basePackages = "com.docker.ddev.repositories" ,
33
+ entityManagerFactoryRef = "entityManagerFactory" ,
34
+ transactionManagerRef = "transactionManager" )
35
+ @ EnableTransactionManagement
36
+ public class JpaConfiguration {
37
+
38
+ @ Autowired
39
+ private Environment environment ;
40
+
41
+
42
+ /*
43
+ * Populate SpringBoot DataSourceProperties from application.yml
44
+ */
45
+ @ Bean
46
+ @ Primary
47
+ @ ConfigurationProperties (prefix = "datasource.ddev" )
48
+ public DataSourceProperties dataSourceProperties () {
49
+ DataSourceProperties dataSourceProperties = new DataSourceProperties ();
50
+
51
+ // Set password to connect to postgres using Docker secrets.
52
+ try (BufferedReader br = new BufferedReader (new FileReader ("/run/secrets/postgres_password" ))) {
53
+ StringBuilder sb = new StringBuilder ();
54
+ String line = br .readLine ();
55
+
56
+ while (line != null ) {
57
+ sb .append (line );
58
+ sb .append (System .lineSeparator ());
59
+ line = br .readLine ();
60
+ }
61
+ dataSourceProperties .setDataPassword (sb .toString ());
62
+ } catch (IOException e ) {
63
+ System .err .println ("Could not successfully load DB password file" );
64
+ }
65
+
66
+ return dataSourceProperties ;
67
+ }
68
+
69
+ /*
70
+ * Configure HikariCP pooled DataSource.
71
+ */
72
+ @ Bean
73
+ public DataSource dataSource () {
74
+ DataSourceProperties dataSourceProperties = dataSourceProperties ();
75
+ HikariDataSource dataSource = (HikariDataSource ) DataSourceBuilder
76
+ .create (dataSourceProperties .getClassLoader ())
77
+ .driverClassName (dataSourceProperties .getDriverClassName ())
78
+ .url (dataSourceProperties .getUrl ())
79
+ .username (dataSourceProperties .getUsername ())
80
+ .password (dataSourceProperties .getPassword ())
81
+ .type (HikariDataSource .class )
82
+ .build ();
83
+ return dataSource ;
84
+ }
85
+
86
+ /*
87
+ * Entity Manager Factory setup.
88
+ */
89
+ @ Bean
90
+ public LocalContainerEntityManagerFactoryBean entityManagerFactory () throws NamingException {
91
+ LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean ();
92
+ factoryBean .setDataSource (dataSource ());
93
+ factoryBean .setPackagesToScan (new String [] { "com.docker.ddev.model" });
94
+ factoryBean .setJpaVendorAdapter (jpaVendorAdapter ());
95
+ factoryBean .setJpaProperties (jpaProperties ());
96
+ return factoryBean ;
97
+ }
98
+
99
+ /*
100
+ * Provider specific adapter.
101
+ */
102
+ @ Bean
103
+ public JpaVendorAdapter jpaVendorAdapter () {
104
+ HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter ();
105
+ return hibernateJpaVendorAdapter ;
106
+ }
107
+
108
+ /*
109
+ * Provider specific properties.
110
+ */
111
+ private Properties jpaProperties () {
112
+ Properties properties = new Properties ();
113
+ properties .put ("hibernate.dialect" , environment .getRequiredProperty ("datasource.ddev.hibernate.dialect" ));
114
+ properties .put ("hibernate.hbm2ddl.auto" , environment .getRequiredProperty ("datasource.ddev.hibernate.hbm2ddl.method" ));
115
+ properties .put ("hibernate.show_sql" , environment .getRequiredProperty ("datasource.ddev.hibernate.show_sql" ));
116
+ properties .put ("hibernate.format_sql" , environment .getRequiredProperty ("datasource.ddev.hibernate.format_sql" ));
117
+ if (StringUtils .isNotEmpty (environment .getRequiredProperty ("datasource.ddev.defaultSchema" ))){
118
+ properties .put ("hibernate.default_schema" , environment .getRequiredProperty ("datasource.ddev.defaultSchema" ));
119
+ }
120
+ return properties ;
121
+ }
122
+
123
+ @ Bean
124
+ @ Autowired
125
+ public PlatformTransactionManager transactionManager (EntityManagerFactory emf ) {
126
+ JpaTransactionManager txManager = new JpaTransactionManager ();
127
+ txManager .setEntityManagerFactory (emf );
128
+ return txManager ;
129
+ }
130
+
131
+ }
0 commit comments