Skip to content

Commit 8c90308

Browse files
author
Gabriel Schenker
committed
Added Configuration Management exercise
1 parent e462c42 commit 8c90308

37 files changed

+1704
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Environment Configuration
2+
3+
This repo contains all configurations for the various environments.
4+
5+
There is one branch per environment
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/node_modules/
2+
**/target
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="output" path="target/classes"/>
31+
</classpath>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

configuration-management/api/.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ddev</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM maven:latest AS appserver
2+
WORKDIR /usr/src/ddev
3+
COPY pom.xml .
4+
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
5+
COPY . .
6+
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests
7+
8+
FROM java:8-jdk-alpine
9+
RUN adduser -Dh /home/gordon gordon
10+
WORKDIR /app
11+
COPY --from=appserver /usr/src/ddev/target/ddev-0.0.1-SNAPSHOT.jar .
12+
ENTRYPOINT ["java", "-jar", "/app/ddev-0.0.1-SNAPSHOT.jar"]
13+
CMD ["--spring.profiles.active=postgres"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM maven:latest AS appserver
2+
WORKDIR /usr/src/ddev
3+
COPY pom.xml .
4+
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
5+
COPY . .
6+
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests
7+
8+
FROM java:8-jdk-alpine
9+
RUN adduser -Dh /home/gordon gordon
10+
WORKDIR /app
11+
COPY --from=appserver /usr/src/ddev/target/ddev-0.0.1-SNAPSHOT.jar .
12+
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005","-jar", "/app/ddev-0.0.1-SNAPSHOT.jar"]
13+
CMD ["--spring.profiles.active=postgres"]

configuration-management/api/pom.xml

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-starter-parent</artifactId>
7+
<version>1.5.3.RELEASE</version>
8+
</parent>
9+
10+
<groupId>com.docker.ddev</groupId>
11+
<artifactId>ddev</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<name>ddev</name>
14+
15+
<properties>
16+
<java.version>1.8</java.version>
17+
<h2.version>1.4.187</h2.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- Add typical dependencies for a web application -->
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-core</artifactId>
29+
</dependency>
30+
<!-- Add JPA support -->
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-jpa</artifactId>
34+
</dependency>
35+
<!-- Add Hikari Connection Pooling support -->
36+
<dependency>
37+
<groupId>com.zaxxer</groupId>
38+
<artifactId>HikariCP</artifactId>
39+
</dependency>
40+
<!-- Add H2 database support [for running with local profile] -->
41+
<dependency>
42+
<groupId>com.h2database</groupId>
43+
<artifactId>h2</artifactId>
44+
</dependency>
45+
<!-- Add PostgreSQL database support -->
46+
<dependency>
47+
<groupId>org.postgresql</groupId>
48+
<artifactId>postgresql</artifactId>
49+
</dependency>
50+
<!-- Postgres connection pooling -->
51+
<dependency>
52+
<groupId>org.hibernate</groupId>
53+
<artifactId>hibernate-c3p0</artifactId>
54+
<version>5.2.10.Final</version>
55+
</dependency>
56+
<!-- Add Jdbc Template support -->
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-jdbc</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework</groupId>
63+
<artifactId>spring-jdbc</artifactId>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.googlecode.json-simple</groupId>
67+
<artifactId>json-simple</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-security</artifactId>
72+
</dependency>
73+
<!-- devtools support -->
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-devtools</artifactId>
77+
</dependency>
78+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
79+
<dependency>
80+
<groupId>org.apache.commons</groupId>
81+
<artifactId>commons-lang3</artifactId>
82+
<version>3.0</version>
83+
</dependency>
84+
</dependencies>
85+
86+
<build>
87+
<plugins>
88+
<plugin><!-- Create a jar with all dependencies, run commandline using java -jar NAME -->
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-maven-plugin</artifactId>
91+
<configuration>
92+
<excludeDevtools>true</excludeDevtools>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.docker.ddev;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Import;
10+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
11+
12+
import com.docker.ddev.configuration.JpaConfiguration;
13+
14+
15+
@Import(JpaConfiguration.class)
16+
@SpringBootApplication(scanBasePackages={"com.docker.ddev"})
17+
@EntityScan("com.docker.ddev.model")
18+
@EnableJpaRepositories("com.docker.ddev.repository")
19+
public class DdevApp {
20+
public static void main(String[] args) {
21+
SpringApplication.run(DdevApp.class, args);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.docker.ddev.configuration;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.context.annotation.Bean;
5+
6+
import com.docker.ddev.service.ImageService;
7+
import com.docker.ddev.service.ImageServiceImpl;
8+
import com.mchange.v2.c3p0.ComboPooledDataSource;
9+
10+
public class BeanConfiguration {
11+
12+
@Bean
13+
public ImageService imageService() {
14+
return new ImageServiceImpl();
15+
}
16+
17+
// Implement C3P0 connection pooling
18+
@Bean
19+
@ConfigurationProperties("ddev.datasource")
20+
public ComboPooledDataSource dataSource() {
21+
return new ComboPooledDataSource();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)