Added profiles for MySQL and PostgreSQL. Added support for overriding hibernate config with System properties

This commit is contained in:
mposolda 2014-02-14 18:31:37 +01:00
parent cbb6ec8b2c
commit 5578e3b6f3
2 changed files with 62 additions and 1 deletions

View file

@ -1,5 +1,9 @@
package org.keycloak.models.jpa;
import java.util.Map;
import java.util.Properties;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.ModelProvider;
@ -12,6 +16,8 @@ import javax.persistence.Persistence;
*/
public class JpaModelProvider implements ModelProvider {
private static final Logger logger = Logger.getLogger(JpaModelProvider.class);
@Override
public String getId() {
return "jpa";
@ -19,8 +25,22 @@ public class JpaModelProvider implements ModelProvider {
@Override
public KeycloakSessionFactory createFactory() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-keycloak-identity-store");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-keycloak-identity-store", getHibernateProperties());
logger.info("RDBMS connection url: " + emf.getProperties().get("hibernate.connection.url"));
return new JpaKeycloakSessionFactory(emf);
}
// Allows to override some properties in persistence.xml by system properties
protected Properties getHibernateProperties() {
Properties result = new Properties();
for (Object property : System.getProperties().keySet()) {
if (property.toString().startsWith("hibernate.")) {
String propValue = System.getProperty(property.toString());
result.put(property, propValue);
}
}
return result;
}
}

41
pom.xml
View file

@ -22,6 +22,8 @@
<hibernate.javax.persistence.version>1.0.1.Final</hibernate.javax.persistence.version>
<hibernate.entitymanager.version>4.0.1.Final</hibernate.entitymanager.version>
<h2.version>1.3.161</h2.version>
<mysql.version>5.1.29</mysql.version>
<postgresql.version>9.3-1100-jdbc41</postgresql.version>
<dom4j.version>1.6.1</dom4j.version>
<mysql.version>5.1.25</mysql.version>
<slf4j.version>1.6.1</slf4j.version>
@ -544,5 +546,44 @@
<module>distribution</module>
</modules>
</profile>
<!-- MySQL -->
<profile>
<activation>
<property>
<name>hibernate.connection.driver_class</name>
<value>com.mysql.jdbc.Driver</value>
</property>
</activation>
<id>mysql</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<!-- PostgreSQL -->
<profile>
<activation>
<property>
<name>hibernate.connection.driver_class</name>
<value>org.postgresql.Driver</value>
</property>
</activation>
<id>postgresql</id>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>postgresql.version</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>