You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/04/20 16:00:51 UTC
olingo-odata2 git commit: [OLINGO-634] Changed EMF creation for JEE
default db
Repository: olingo-odata2
Updated Branches:
refs/heads/master e26395fd1 -> 5a73f0557
[OLINGO-634] Changed EMF creation for JEE default db
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/5a73f055
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/5a73f055
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/5a73f055
Branch: refs/heads/master
Commit: 5a73f0557d2a35b7e85acd12e1e2d55996cdb12c
Parents: e26395f
Author: Michael Bolz <mi...@sap.com>
Authored: Mon Apr 20 15:07:45 2015 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Mon Apr 20 15:57:42 2015 +0200
----------------------------------------------------------------------
.../main/resources/archetype-resources/pom.xml | 3 +-
.../java/service/ODataJPACarServiceFactory.java | 66 +++++++++++++++++---
.../src/main/resources/META-INF/persistence.xml | 21 ++++---
.../src/main/webapp/WEB-INF/web.xml | 6 ++
4 files changed, 80 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5a73f055/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/pom.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/pom.xml b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/pom.xml
index a0683d6..40edf1f 100644
--- a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/pom.xml
+++ b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/pom.xml
@@ -22,6 +22,7 @@
<version.cxf>2.7.6</version.cxf>
<version.eclipselink>2.5.1</version.eclipselink>
<version.javax.persistence>2.0.5</version.javax.persistence>
+ <version.hsqldb>2.3.2</version.hsqldb>
<version.olingo>2.0.4-SNAPSHOT</version.olingo>
</properties>
@@ -72,7 +73,7 @@
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
- <version>1.8.0.10</version>
+ <version>${version.hsqldb}</version>
</dependency>
<!-- Web Support -->
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5a73f055/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/java/service/ODataJPACarServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/java/service/ODataJPACarServiceFactory.java b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/java/service/ODataJPACarServiceFactory.java
index ecf628f..b057d5b 100644
--- a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/java/service/ODataJPACarServiceFactory.java
+++ b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/java/service/ODataJPACarServiceFactory.java
@@ -24,9 +24,16 @@ package ${package}.service;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
@@ -40,12 +47,10 @@ import ${package}.model.Manufacturer;
public class ODataJPACarServiceFactory extends ODataJPAServiceFactory {
- private static final String PUNIT_NAME = "MyFormula";
private static final int PAGE_SIZE = 5;
/** Load Sample Data **/
static {
-
List<Car> cars = new ArrayList<Car>();
Calendar mfDate = Calendar.getInstance();
mfDate.set(2013, 02, 01);
@@ -65,8 +70,8 @@ public class ODataJPACarServiceFactory extends ODataJPAServiceFactory {
cars.add(car);
driver.setCar(car);
- EntityManager em = Persistence.createEntityManagerFactory(PUNIT_NAME)
- .createEntityManager();
+
+ EntityManager em = EmfHolder.createInstance().createEntityManager();
em.getTransaction().begin();
em.persist(mf);
em.persist(driver);
@@ -79,13 +84,60 @@ public class ODataJPACarServiceFactory extends ODataJPAServiceFactory {
public ODataJPAContext initializeODataJPAContext()
throws ODataJPARuntimeException {
ODataJPAContext oDataJPAContext = getODataJPAContext();
- oDataJPAContext.setEntityManagerFactory(Persistence
- .createEntityManagerFactory(PUNIT_NAME));
- oDataJPAContext.setPersistenceUnitName(PUNIT_NAME);
+ EmfHolder emfHolder = EmfHolder.createInstance();
+ oDataJPAContext.setEntityManagerFactory(emfHolder.getEntityManagerFactory());
+ oDataJPAContext.setPersistenceUnitName(emfHolder.getPersistenceUnitName());
oDataJPAContext.setPageSize(PAGE_SIZE);
setDetailErrors(true);
return oDataJPAContext;
}
+
+ /**
+ *
+ */
+ private static class EmfHolder {
+ private static final String PUNIT_NAME = "MyFormulaHsqlDb";
+ private static final String PUNIT_NAME_DEFAULT = "MyFormulaJeeDb";
+ private static final String DATA_SOURCE_NAME = "java:comp/env/jdbc/DefaultDB";
+
+ final private EntityManagerFactory emf;
+ final private String persistenceUnitName;
+
+ private EmfHolder(EntityManagerFactory emf, String unitName) {
+ this.emf = emf;
+ this.persistenceUnitName = unitName;
+ }
+
+ public EntityManagerFactory getEntityManagerFactory() {
+ return emf;
+ }
+
+ public String getPersistenceUnitName() {
+ return persistenceUnitName;
+ }
+
+ public static EntityManager createEntityManager() {
+ return createInstance().getEntityManagerFactory().createEntityManager();
+ }
+
+
+ public static synchronized EmfHolder createInstance() {
+ try {
+ InitialContext ctx = new InitialContext();
+ DataSource ds = (DataSource) ctx.lookup(DATA_SOURCE_NAME);
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);
+ EntityManagerFactory emf =
+ Persistence.createEntityManagerFactory(PUNIT_NAME_DEFAULT, properties);
+ emf.createEntityManager();
+ return new EmfHolder(emf, PUNIT_NAME_DEFAULT);
+ } catch (javax.persistence.PersistenceException e) {
+ return new EmfHolder(Persistence.createEntityManagerFactory(PUNIT_NAME), PUNIT_NAME);
+ } catch (NamingException e) {
+ return new EmfHolder(Persistence.createEntityManagerFactory(PUNIT_NAME), PUNIT_NAME);
+ }
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5a73f055/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml
index 73321e4..b498074 100644
--- a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml
+++ b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/resources/META-INF/persistence.xml
@@ -15,25 +15,30 @@
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
- <persistence-unit name="MyFormula"
- transaction-type="RESOURCE_LOCAL">
+ <persistence-unit name="MyFormulaHsqlDb" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>${package}.model.Driver</class>
<class>${package}.model.Car</class>
<class>${package}.model.Manufacturer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
- <property name="javax.persistence.jdbc.url"
- value="jdbc:hsqldb:mem:org.apache.olingo.jpa.sample" />
+ <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:org.apache.olingo.jpa.sample" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
- <property name="eclipselink.target-database"
- value="org.eclipse.persistence.platform.database.HSQLPlatform" />
+ <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.HSQLPlatform" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.orm.throw.exceptions" value="true" />
<property name="eclipselink.ddl-generation" value="create-tables" />
- <property name="eclipselink.ddl-generation.output-mode"
- value="database" />
+ <property name="eclipselink.ddl-generation.output-mode" value="database" />
+ </properties>
+ </persistence-unit>
+ <persistence-unit name="MyFormulaJeeDb" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+ <class>${package}.model.Driver</class>
+ <class>${package}.model.Car</class>
+ <class>${package}.model.Manufacturer</class>
+ <properties>
+ <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5a73f055/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
index 908ecc0..b90c22b 100644
--- a/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
+++ b/odata2-sample/cars-jpa-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
@@ -22,6 +22,12 @@
<welcome-file>index.html</welcome-file>
</welcome-file-list>
+ <!-- Enable usage of default JEE persistence service -->
+ <resource-ref>
+ <res-ref-name>jdbc/DefaultDB</res-ref-name>
+ <res-type>javax.sql.DataSource</res-type>
+ </resource-ref>
+
<servlet>
<servlet-name>CarServiceServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>