diff --git a/pom.xml b/pom.xml index 3dd45c096..258824227 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ UTF-8 8.1.0.v20120127 8.1.4.v20120524 + mysql @@ -106,7 +107,7 @@ br.com.caelum.vraptor vraptor-jodatime - 4.0.0-RC3-SNAPSHOT + 4.0.0-RC3-SNAPSHOT @@ -453,6 +454,12 @@ 1.7.3 test + + com.h2database + h2 + 1.4.180 + test + @@ -586,6 +593,9 @@ + + ${mamute.database} + **/scene/** @@ -665,7 +675,7 @@ - org.eclipse.m2e diff --git a/src/main/java/org/mamute/providers/SessionFactoryCreator.java b/src/main/java/org/mamute/providers/SessionFactoryCreator.java index e4cbb072b..4a37bef99 100644 --- a/src/main/java/org/mamute/providers/SessionFactoryCreator.java +++ b/src/main/java/org/mamute/providers/SessionFactoryCreator.java @@ -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; @@ -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 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); @@ -103,7 +108,7 @@ public void init() { cfg.addAnnotatedClass(TagPage.class); this.factory = cfg.buildSessionFactory(); - + } @Produces @@ -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; } - + } \ No newline at end of file diff --git a/src/main/resources/development/hibernate.cfg.xml b/src/main/resources/development/hibernate-mysql.cfg.xml similarity index 100% rename from src/main/resources/development/hibernate.cfg.xml rename to src/main/resources/development/hibernate-mysql.cfg.xml diff --git a/src/test/resources/test/hibernate-h2.cfg.xml b/src/test/resources/test/hibernate-h2.cfg.xml new file mode 100644 index 000000000..c3b96763d --- /dev/null +++ b/src/test/resources/test/hibernate-h2.cfg.xml @@ -0,0 +1,25 @@ + + + + + jdbc:h2:./target/db/repository + org.h2.Driver + org.hibernate.dialect.H2Dialect + root + + false + false + create-drop + import_test.sql + + + false + false + + + + + \ No newline at end of file diff --git a/src/test/resources/test/hibernate.cfg.xml b/src/test/resources/test/hibernate-mysql.cfg.xml similarity index 100% rename from src/test/resources/test/hibernate.cfg.xml rename to src/test/resources/test/hibernate-mysql.cfg.xml