tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kao...@apache.org
Subject [2/2] git commit: FIXED - TAP5-2102: Allow supplying EntityManager properties via TapestryPersistenceUnitInfo - add new EntityManagerProperties to TapestryPersistenceUnitInfo - if set, use the supplied entityManagerProperties in EntityManagerSourceImpl.c
Date Sun, 07 Apr 2013 02:51:40 GMT
FIXED - TAP5-2102: Allow supplying EntityManager properties via
TapestryPersistenceUnitInfo
- add new EntityManagerProperties to TapestryPersistenceUnitInfo
- if set, use the supplied entityManagerProperties in
EntityManagerSourceImpl.createEntityManagerFactory


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/1578ea0d
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/1578ea0d
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/1578ea0d

Branch: refs/heads/master
Commit: 1578ea0d229b536622258b4bd17176fb9ced9c85
Parents: cf0f4e3
Author: Kalle Korhonen <kaosko@apache.org>
Authored: Sat Apr 6 19:49:12 2013 -0700
Committer: Kalle Korhonen <kaosko@apache.org>
Committed: Sat Apr 6 19:50:10 2013 -0700

----------------------------------------------------------------------
 .../internal/jpa/EntityManagerSourceImpl.java      |    5 +-
 .../internal/jpa/PersistenceUnitInfoImpl.java      |   12 ++++
 .../tapestry5/jpa/TapestryPersistenceUnitInfo.java |   19 ++++++-
 .../internal/jpa/EntityManagerSourceImplTest.java  |   41 +++++++++++++++
 4 files changed, 74 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1578ea0d/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
b/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
index 6f02b32..39da3c4 100644
--- a/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
+++ b/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
@@ -190,14 +190,15 @@ public class EntityManagerSourceImpl implements EntityManagerSource
         return emf;
     }
 
-    private EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName)
+    @SuppressWarnings("unchecked")
+    EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName)
     {
 
         for (final TapestryPersistenceUnitInfo info : persistenceUnitInfos)
         {
             if (info.getPersistenceUnitName().equals(persistenceUnitName))
             {
-                final Map<String, String> properties = CollectionFactory.newCaseInsensitiveMap();
+                final Map properties = info.getEntityManagerProperties() == null ? CollectionFactory.newCaseInsensitiveMap()
: info.getEntityManagerProperties();
                 properties.put(JpaConstants.PERSISTENCE_UNIT_NAME, persistenceUnitName);
 
                 String providerClassName = info.getPersistenceProviderClassName();

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1578ea0d/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java
b/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java
index 82c0e4a..463a4dd 100644
--- a/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java
+++ b/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/PersistenceUnitInfoImpl.java
@@ -29,6 +29,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
@@ -60,6 +61,7 @@ public class PersistenceUnitInfoImpl implements TapestryPersistenceUnitInfo
 
     private final Properties properties = new Properties();
 
+		private Map entityManagerProperties;
 
     public PersistenceUnitInfoImpl(String persistenceUnitName)
     {
@@ -364,4 +366,14 @@ public class PersistenceUnitInfoImpl implements TapestryPersistenceUnitInfo
 
     }
 
+    public TapestryPersistenceUnitInfo setEntityManagerProperties(Map properties) {
+    	entityManagerProperties = properties;
+    	return this;
+    }
+
+    public Map getEntityManagerProperties() {
+    	return entityManagerProperties;
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1578ea0d/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java
----------------------------------------------------------------------
diff --git a/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java
b/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java
index 128f78d..4ff5b41 100644
--- a/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java
+++ b/tapestry-jpa/src/main/java/org/apache/tapestry5/jpa/TapestryPersistenceUnitInfo.java
@@ -14,11 +14,13 @@
 
 package org.apache.tapestry5.jpa;
 
+import java.net.URL;
+import java.util.Map;
+
 import javax.persistence.SharedCacheMode;
 import javax.persistence.ValidationMode;
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.PersistenceUnitTransactionType;
-import java.net.URL;
 
 /**
  * Tapestry's mutable extension of {@link PersistenceUnitInfo} interface used for XML-less
configuration
@@ -152,4 +154,19 @@ public interface TapestryPersistenceUnitInfo extends PersistenceUnitInfo
      *         defines whether to exclude or not
      */
     TapestryPersistenceUnitInfo excludeUnlistedClasses(boolean exclude);
+
+    /**
+     * {@link javax.persistence.spi.PersistenceProvider} allows creating an {@alink javax.persistence.EntityManagerFactory}
+     * with a default EntityManager properties map. This operation allows contributing default
properties for
+     * EntityManager.
+     *
+     * @param properties
+     *         properties to initialize EntityManagerFactory with
+     */
+    TapestryPersistenceUnitInfo setEntityManagerProperties(Map properties);
+
+    /**
+     * @return Returns the supplied EntityManagerFactory properties. Returns null if not
set
+     */
+    Map getEntityManagerProperties();
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1578ea0d/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
----------------------------------------------------------------------
diff --git a/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
b/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
index 75da36f..9ccbafc 100644
--- a/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
+++ b/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
@@ -14,9 +14,16 @@
 
 package org.apache.tapestry5.internal.jpa;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+
 import org.apache.tapestry5.ioc.internal.util.ClasspathResource;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.jpa.PersistenceUnitConfigurer;
+import org.apache.tapestry5.jpa.TapestryPersistenceUnitInfo;
 import org.apache.tapestry5.test.TapestryTestCase;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.Test;
@@ -48,4 +55,38 @@ public class EntityManagerSourceImplTest extends TapestryTestCase
         assertEquals(exception.getMessage(), "Persistence units 'TestUnit, TestUnit2' are
configured to include managed classes that have not been explicitly listed. This is forbidden
when multiple persistence units are used in the same application. Please configure persistence
units to exclude unlisted managed classes (e.g. by removing <exclude-unlisted-classes>
element) and include them explicitly.");
 
     }
+
+    @Test
+    public void createEntityManagerFactory_with_supplied_entitymanagerproperties()
+    {
+        PersistenceUnitConfigurer configurer = new PersistenceUnitConfigurer()
+        {
+            @SuppressWarnings(
+            { "unchecked", "rawtypes" })
+            public void configure(TapestryPersistenceUnitInfo unitInfo)
+            {
+                Map properties = new HashMap();
+                properties.put("MYKEY", "MYVALUE");
+                unitInfo.transactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL)
+                        .persistenceProviderClassName(
+                                "org.eclipse.persistence.jpa.PersistenceProvider")
+                        .excludeUnlistedClasses(true)
+                        .addProperty("javax.persistence.jdbc.user", "sa")
+                        .addProperty("javax.persistence.jdbc.driver", "org.h2.Driver")
+                        .addProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:test")
+                        .setEntityManagerProperties(properties);
+            }
+        };
+
+        Map<String, PersistenceUnitConfigurer> configurerMap = CollectionFactory
+                .<String, PersistenceUnitConfigurer> newMap();
+        configurerMap.put("defaultpropertytest", configurer);
+        EntityManagerSourceImpl emSource = new EntityManagerSourceImpl(
+                LoggerFactory.getLogger(EntityManagerSourceImplTest.class), new ClasspathResource(
+                        "single-persistence-unit.xml"), null, configurerMap);
+        EntityManager em = emSource.createEntityManagerFactory("defaultpropertytest")
+                .createEntityManager();
+        assertEquals(em.getProperties().get("MYKEY"), "MYVALUE");
+    }
+
 }


Mime
View raw message