Skip to content

Commit

Permalink
adding system property for databases as part of #36
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Aug 18, 2014
1 parent 3fc8501 commit 350dda3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
14 changes: 12 additions & 2 deletions pom.xml
Expand Up @@ -12,6 +12,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jetty.version>8.1.0.v20120127</jetty.version>
<jetty.jsp.version>8.1.4.v20120524</jetty.jsp.version>
<mamute.database>mysql</mamute.database>
</properties>

<scm>
Expand Down Expand Up @@ -106,7 +107,7 @@
<dependency>
<groupId>br.com.caelum.vraptor</groupId>
<artifactId>vraptor-jodatime</artifactId>
<version>4.0.0-RC3-SNAPSHOT</version>
<version>4.0.0-RC3-SNAPSHOT</version>
<!-- RC3 manual deployed to fix issue #2 -->
</dependency>
<dependency>
Expand Down Expand Up @@ -453,6 +454,12 @@
<version>1.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.180</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -586,6 +593,9 @@
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<mamute.database>${mamute.database}</mamute.database>
</systemPropertyVariables>
<excludes>
<exclude>**/scene/**</exclude>
</excludes>
Expand Down Expand Up @@ -665,7 +675,7 @@
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/mamute/providers/SessionFactoryCreator.java
Expand Up @@ -42,11 +42,12 @@
@Alternative
@Priority(Interceptor.Priority.APPLICATION)
public class SessionFactoryCreator {

public static final String JODA_TIME_TYPE= "org.jadira.usertype.dateandtime.joda.PersistentDateTime";

public static final String JODA_TIME_TYPE = "org.jadira.usertype.dateandtime.joda.PersistentDateTime";
public static final String DATABASE_PROPERTY = "mamute.database";
private static final Logger LOGGER = LoggerFactory
.getLogger(SessionFactoryCreator.class);

private Configuration cfg;
private SessionFactory factory;
private Environment env;
Expand All @@ -64,20 +65,24 @@ public SessionFactoryCreator(Environment env, ValidatorFactory vf) {

@PostConstruct
public void init() {
URL xml = env.getResource("/hibernate.cfg.xml");
//TODO: should probably be in a config file of some sort rather than a system prop
String db = System.getProperty(DATABASE_PROPERTY, "mysql");
String hibernateCfg = "/hibernate-" + db + ".cfg.xml";

URL xml = env.getResource(hibernateCfg);
LOGGER.info("Loading hibernate xml from " + xml);
this.cfg = new Configuration().configure(xml);

if (this.vf != null) {
Map<Object, Object> properties = cfg.getProperties();
properties.put("javax.persistence.validation.factory", this.vf);
}

String url = System.getenv("JDBC_URL");
if (url != null) {
String user = System.getenv("USER");
String password = System.getenv("PASSWORD");

LOGGER.info("reading database config from environment: " + url);
cfg.setProperty("hibernate.connection.url", url);
cfg.setProperty("hibernate.connection.username", user);
Expand All @@ -103,7 +108,7 @@ public void init() {
cfg.addAnnotatedClass(TagPage.class);

this.factory = cfg.buildSessionFactory();

}

@Produces
Expand All @@ -125,15 +130,16 @@ public void dropAndCreate() {
new SchemaExport(cfg).create(true, true);
init();
}

public void drop() {
factory.close();
factory = null;
new SchemaExport(cfg).drop(true, true);
init();
}

public Configuration getCfg() {
return cfg;
}

}
25 changes: 25 additions & 0 deletions src/test/resources/test/hibernate-h2.cfg.xml
@@ -0,0 +1,25 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:h2:./target/db/repository</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.hbm2ddl.import_files">import_test.sql</property>


<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>

<!-- Nao adicione as classes aqui! Elas ficariam replicadas em 3 xmls!
Deixe somente no SessionFactoryCreator! -->

</session-factory>
</hibernate-configuration>
File renamed without changes.

0 comments on commit 350dda3

Please sign in to comment.