JR Utily

manage datasource via maven profile

in production, tomee needs now to have its own datasource configuration
......@@ -31,15 +31,21 @@
<!-- paths -->
<custom.web.dir>src/main/java/org/legrog/web</custom.web.dir>
<!-- misc -->
<!-- misc tomee conf overriden by maven profiles -->
<debug.jvm.args/>
<tomee.autoreload/>
<deltaspike.project.stage/>
<grogdatabase.jdbc.driver/>
<grogdatabase.jdbc.url/>
<grogdatabase.username/>
<grogdatabase.password/>
<driver.lib/>
</properties>
<profiles>
<profile>
<id>debug</id>
<id>Tomee-debug</id>
<!--
activate this one to be able to attach a remote debbuger on tomee
-->
......@@ -49,7 +55,7 @@
</profile>
<profile>
<id>autoreload</id>
<id>Tomee-autoreload</id>
<!--
activate this one for tomee to reload (takes times) every times it detect a synchro
reminder : you can always force a reload by typing reload in the console while tomee:run is active
......@@ -58,8 +64,44 @@
<tomee.autoreload>true</tomee.autoreload>
</properties>
</profile>
</profiles>
<profile>
<id>DS-h2-embeded</id>
<!--
Set the datasource to a user-defined H2 database and activate the H2 console servlet
-->
<properties>
<deltaspike.project.stage>-Dorg.apache.deltaspike.ProjectStage=IntegrationTest
</deltaspike.project.stage>
<driver.lib>com.h2database:h2:1.4.193</driver.lib>
<grogdatabase.jdbc.driver>org.h2.Driver</grogdatabase.jdbc.driver>
<grogdatabase.jdbc.url>jdbc:h2:mem:grogdev</grogdatabase.jdbc.url>
<grogdatabase.username>sa</grogdatabase.username>
</properties>
</profile>
<profile>
<id>DS-mysql-local</id>
<!--
Set the datasource to the local mysql server
-->
<properties>
<deltaspike.project.stage>-Dorg.apache.deltaspike.ProjectStage=Staging</deltaspike.project.stage>
<driver.lib>mysql:mysql-connector-java:5.1.32</driver.lib>
<grogdatabase.jdbc.driver>com.mysql.jdbc.Driver</grogdatabase.jdbc.driver>
<!-- EDIT THE FOLLOWING PARAMETERS TO MATCH YOUR SERVER -->
<grogdatabase.username>grogdev</grogdatabase.username>
<grogdatabase.password>grogdev</grogdatabase.password>
<!-- format is jdbc:mysql://HOST:PORT/SCHEMA?useSSL=true/false -->
<grogdatabase.jdbc.url>jdbc:mysql://localhost/grogdev?useSSL=false</grogdatabase.jdbc.url>
</properties>
</profile>
</profiles>
<dependencies>
......@@ -83,6 +125,22 @@
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.core</groupId>
<artifactId>deltaspike-core-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.deltaspike.core</groupId>
<artifactId>deltaspike-core-impl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.omnifaces/omnifaces -->
<dependency>
<groupId>org.omnifaces</groupId>
......@@ -154,9 +212,6 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--
<version>5.1.6</version>
-->
</dependency>
</dependencies>
......@@ -197,11 +252,20 @@
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.1</version>
<executions>
<execution>
<phase>pre-site</phase>
</execution>
</executions>
<configuration>
<context>ROOT</context>
<!-- debug agent to attach a remote debbuger, activate profile for that -->
<args>${debug.jvm.args}</args>
<args>${debug.jvm.args} ${deltaspike.project.stage}</args>
<libs>
<lib>${driver.lib}</lib>
</libs>
<systemVariables>
<!--
......@@ -209,6 +273,16 @@
-->
<openejb.system.apps>true</openejb.system.apps>
<tomee.serialization.class.blacklist>-</tomee.serialization.class.blacklist>
<GrogDatabase>new://Resource?type=DataSource</GrogDatabase>
<GrogDatabase.JtaManaged>true</GrogDatabase.JtaManaged>
<GrogDatabase.JdbcDriver>${grogdatabase.jdbc.driver}</GrogDatabase.JdbcDriver>
<GrogDatabase.JdbcUrl>${grogdatabase.jdbc.url}</GrogDatabase.JdbcUrl>
<GrogDatabase.UserName>${grogdatabase.username}</GrogDatabase.UserName>
<GrogDatabase.Password>${grogdatabase.password}</GrogDatabase.Password>
</systemVariables>
......
package org.legrog.configuration;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
* JPA configuration class, used by all classes extending JpaRepository
*
* No difference made between Integration and Production yet,
* as there is only one PU in the environnement at a given moment
*/
public class JpaConfiguration {
//@Exclude(ifProjectStage = ProjectStage.IntegrationTest.class)
public class EntityManagerProducer {
/**
* Injectable interface for persistence handling
*/
@Produces
@RequestScoped
@PersistenceContext(unitName = "development-pu")
@PersistenceContext(unitName = "Grog-DB")
public EntityManager entityManager;
public void closeEntityManager(@Disposes EntityManager entityManager) {
if (entityManager.isOpen()) {
entityManager.close();
}
}
}
......
<?xml version="1.0"?>
<tomee>
<Resource id="H2Database" type="javax.sql.DataSource">
JdbcDriver = org.h2.Driver
JdbcUrl = jdbc:h2:~/grog-dev.db
UserName = sa
JtaManaged= true
</Resource>
<!--<Resource id="migrationDatabase" type="javax.sql.DataSource">-->
<!--JdbcDriver = com.mysql.jdbc.Driver-->
<!--JdbcUrl = jdbc:mysql://localhost/migration?useSSL=false-->
<!--UserName = grogdev-->
<!--password = grogdev-->
<!--JtaManaged= true-->
<!--</Resource>-->
<!-- Placeholder file if some resources are needed some day -->
</tomee>
\ No newline at end of file
......