-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #968 from puzzle/feature/921_read_hibernate_config…
…_from_application_properties Feature/921 read hibernate config from application properties
- Loading branch information
Showing
17 changed files
with
155 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package ch.puzzle.okr; | ||
|
||
import ch.puzzle.okr.service.clientconfig.ClientCustomizationProperties; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@SpringBootApplication | ||
@EnableScheduling | ||
@EnableConfigurationProperties(ClientCustomizationProperties.class) | ||
public class OkrApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(OkrApplication.class, args); | ||
|
||
new SpringApplicationBuilder(OkrApplication.class) // | ||
.initializers(new OkrApplicationContextInitializer()) // | ||
.run(args); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/ch/puzzle/okr/OkrApplicationContextInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ch.puzzle.okr; | ||
|
||
import ch.puzzle.okr.multitenancy.listener.HibernateContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class OkrApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(OkrApplicationContextInitializer.class); | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
logger.info("Loading hibernate configuration from application properties"); | ||
HibernateContext.cacheHibernateProperties(applicationContext.getEnvironment()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
backend/src/main/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderH2.java
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
...nd/src/main/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderPGSQL.java
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/ch/puzzle/okr/multitenancy/listener/HibernateContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ch.puzzle.okr.multitenancy.listener; | ||
|
||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
import java.util.Properties; | ||
|
||
public class HibernateContext { | ||
public static final String HIBERNATE_CONNECTION_URL = "hibernate.connection.url"; | ||
public static final String HIBERNATE_CONNECTION_USERNAME = "hibernate.connection.username"; | ||
public static final String HIBERNATE_CONNECTION_PASSWORD = "hibernate.connection.password"; | ||
public static final String HIBERNATE_MULTITENANCY = "hibernate.multiTenancy"; | ||
|
||
public static String SPRING_DATASOURCE_URL = "spring.datasource.url"; | ||
public static String SPRING_DATASOURCE_USERNAME = "spring.datasource.username"; | ||
public static String SPRING_DATASOURCE_PASSWORD = "spring.datasource.password"; | ||
|
||
public record DbConfig(String url, String username, String password, String multiTenancy) { | ||
} | ||
|
||
private static DbConfig cachedHibernateConfig; | ||
|
||
public static void setHibernateConfig(DbConfig dbConfig) { | ||
cachedHibernateConfig = dbConfig; | ||
} | ||
|
||
public static Properties getHibernateConfig() { | ||
return getConfigAsProperties(cachedHibernateConfig); | ||
} | ||
|
||
public static Properties getConfigAsProperties(DbConfig dbConfig) { | ||
Properties properties = new Properties(); | ||
properties.put(HibernateContext.HIBERNATE_CONNECTION_URL, dbConfig.url()); | ||
properties.put(HibernateContext.HIBERNATE_CONNECTION_USERNAME, dbConfig.username()); | ||
properties.put(HibernateContext.HIBERNATE_CONNECTION_PASSWORD, dbConfig.password()); | ||
properties.put(HibernateContext.HIBERNATE_MULTITENANCY, dbConfig.multiTenancy()); | ||
properties.put(HibernateContext.SPRING_DATASOURCE_URL, dbConfig.url()); | ||
properties.put(HibernateContext.SPRING_DATASOURCE_USERNAME, dbConfig.username()); | ||
properties.put(HibernateContext.SPRING_DATASOURCE_PASSWORD, dbConfig.password()); | ||
return properties; | ||
} | ||
|
||
public static void cacheHibernateProperties(ConfigurableEnvironment environment) { | ||
String url = environment.getProperty(HibernateContext.HIBERNATE_CONNECTION_URL); | ||
String username = environment.getProperty(HibernateContext.HIBERNATE_CONNECTION_USERNAME); | ||
String password = environment.getProperty(HibernateContext.HIBERNATE_CONNECTION_PASSWORD); | ||
String multiTenancy = environment.getProperty(HibernateContext.HIBERNATE_MULTITENANCY); | ||
|
||
HibernateContext.DbConfig h2DbConfig = new HibernateContext.DbConfig(url, username, password, multiTenancy); | ||
HibernateContext.setHibernateConfig(h2DbConfig); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,12 @@ spring.flyway.locations=classpath:db/h2-db/database-h2-schema,classpath:db/h2-db | |
okr.tenant-ids=pitc,acme | ||
okr.datasource.driver-class-name=org.h2.Driver | ||
|
||
# hibernate | ||
hibernate.connection.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1; | ||
hibernate.connection.username=user | ||
hibernate.connection.password=sa | ||
hibernate.multiTenancy=SCHEMA | ||
|
||
# pitc | ||
okr.tenants.pitc.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS okr_pitc | ||
okr.tenants.pitc.datasource.username=user | ||
|
@@ -35,5 +41,3 @@ [email protected],[email protected] | |
okr.tenants.acme.security.oauth2.resourceserver.jwt.jwk-set-uri=http://localhost:8544/realms/pitc/protocol/openid-connect/certs | ||
okr.tenants.acme.security.oauth2.frontend.issuer-url=http://localhost:8544/realms/pitc | ||
okr.tenants.acme.security.oauth2.frontend.client-id=acme_okr_staging | ||
|
||
spring.jpa.properties.hibernate.multi_tenant_connection_provider=ch.puzzle.okr.multitenancy.SchemaMultiTenantConnectionProviderH2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
backend/src/main/resources/hibernate-multitenancy-h2.properties
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
backend/src/main/resources/hibernate-multitenancy-pgsql.properties
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...d/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderH2Test.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...rc/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderPGSQLTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.