cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r508801 - in /cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa: JtaEntityManagerFactory.java ResourceLocalEntityManager.java ResourceLocalEntityManagerFactory.java TypeCheckingEntityManager.java
Date Sat, 17 Feb 2007 19:52:00 GMT
Author: aadamchik
Date: Sat Feb 17 11:52:00 2007
New Revision: 508801

URL: http://svn.apache.org/viewvc?view=rev&rev=508801
Log:
refactoring entity manager, extracting entity type checking in a separate wrapper class

Added:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/TypeCheckingEntityManager.java
Modified:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JtaEntityManagerFactory.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManagerFactory.java

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JtaEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JtaEntityManagerFactory.java?view=diff&rev=508801&r1=508800&r2=508801
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JtaEntityManagerFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JtaEntityManagerFactory.java
Sat Feb 17 11:52:00 2007
@@ -93,7 +93,8 @@
     @Override
     public EntityManager createEntityManager(Map map) {
         checkClosed();
-        EntityManager em = new JtaEntityManager(createObjectContext(), this);
+        CayenneEntityManager em = new JtaEntityManager(createObjectContext(), this);
+        em = new TypeCheckingEntityManager(em);
 
         if (isActiveTransaction()) {
             em.joinTransaction();

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java?view=diff&rev=508801&r1=508800&r2=508801
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
Sat Feb 17 11:52:00 2007
@@ -104,7 +104,6 @@
      */
     public void persist(Object entity) {
         checkClosed();
-        checkEntityType(entity);
         context.registerNewObject(entity);
     }
 
@@ -119,7 +118,6 @@
      */
     public <T> T merge(T entity) {
         checkClosed();
-        checkEntityType(entity);
         checkNotRemoved(entity);
         Persistent persistent = (Persistent) entity;
         return (T) context.localObject(persistent.getObjectId(), persistent);
@@ -133,7 +131,6 @@
      */
     public void remove(Object entity) {
         checkClosed();
-        checkEntityType(entity);
         checkAttached(entity);
         context.deleteObject((Persistent) entity);
     }
@@ -149,7 +146,6 @@
      */
     public <T> T find(Class<T> entityClass, Object primaryKey) {
         checkClosed();
-        checkEntityType(entityClass);
         return (T) DataObjectUtils.objectForPK(context, entityClass, primaryKey);
     }
 
@@ -257,8 +253,6 @@
      */
     public boolean contains(Object entity) {
         checkClosed();
-        checkEntityType(entity);
-
         Persistent p = (Persistent) entity;
         return p.getObjectContext() == context;
     }
@@ -287,14 +281,11 @@
      */
     public Query createNamedQuery(String name) {
         checkClosed();
-
         return new JpaQuery(context, name);
     }
 
     public Query createNativeQuery(String sqlString, Class resultClass) {
         checkClosed();
-        checkEntityType(resultClass);
-
         return new JpaNativeQuery(context, sqlString, resultClass);
     }
 
@@ -368,15 +359,10 @@
      * IllegalArgumentException if not.
      */
     protected void checkAttached(Object entity) throws IllegalArgumentException {
-        if (entity instanceof Persistent) {
-            Persistent p = (Persistent) entity;
-            if (p.getPersistenceState() == PersistenceState.TRANSIENT
-                    || p.getObjectContext() == null) {
-                throw new IllegalArgumentException("entity is detached: " + entity);
-            }
-        }
-        else {
-            throw new IllegalArgumentException("entity must be Persistent: " + entity);
+        Persistent p = (Persistent) entity;
+        if (p.getPersistenceState() == PersistenceState.TRANSIENT
+                || p.getObjectContext() == null) {
+            throw new IllegalArgumentException("entity is detached: " + entity);
         }
     }
 
@@ -385,33 +371,9 @@
      * IllegalArgumentException if it is.
      */
     protected void checkNotRemoved(Object entity) throws IllegalArgumentException {
-        if (entity instanceof Persistent) {
-            Persistent p = (Persistent) entity;
-            if (p.getPersistenceState() == PersistenceState.DELETED) {
-                throw new IllegalArgumentException("entity is removed: " + entity);
-            }
-        }
-    }
-
-    protected void checkEntityType(Class entityClass) throws IllegalArgumentException {
-        if (entityClass == null) {
-            throw new IllegalArgumentException("Null entity class");
-        }
-
-        if (!Persistent.class.isAssignableFrom(entityClass)) {
-            throw new IllegalArgumentException("Entity class must be Persistent, got: "
-                    + entityClass.getName());
-        }
-    }
-
-    protected void checkEntityType(Object entity) throws IllegalArgumentException {
-        if (entity == null) {
-            throw new IllegalArgumentException("Null entity");
-        }
-
-        if (!(entity instanceof Persistent)) {
-            String className = (entity != null) ? entity.getClass().getName() : "<null>";
-            throw new IllegalArgumentException("entity must be Persistent: " + className);
+        Persistent p = (Persistent) entity;
+        if (p.getPersistenceState() == PersistenceState.DELETED) {
+            throw new IllegalArgumentException("entity is removed: " + entity);
         }
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManagerFactory.java?view=diff&rev=508801&r1=508800&r2=508801
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManagerFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManagerFactory.java
Sat Feb 17 11:52:00 2007
@@ -110,7 +110,8 @@
      */
     public EntityManager createEntityManager(Map map) {
         checkClosed();
-        return new ResourceLocalEntityManager(createObjectContext(), this);
+        CayenneEntityManager em = new ResourceLocalEntityManager(createObjectContext(), this);
+        return new TypeCheckingEntityManager(em);
     }
 
     /**

Added: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/TypeCheckingEntityManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/TypeCheckingEntityManager.java?view=auto&rev=508801
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/TypeCheckingEntityManager.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/TypeCheckingEntityManager.java
Sat Feb 17 11:52:00 2007
@@ -0,0 +1,169 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.jpa;
+
+import javax.persistence.EntityTransaction;
+import javax.persistence.FlushModeType;
+import javax.persistence.LockModeType;
+import javax.persistence.Query;
+
+import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.Persistent;
+
+/**
+ * An EntityManager decorator that checks that only properly enhanced entities are passwed
+ * to the underlying EntityManager.
+ * 
+ * @author Andrus Adamchik
+ */
+// TODO: andrus, 2/18/2007 - in the future this wrapper can also enhance entities on the
+// fly, for now it simply does the type checks before passing entities to the underlying
+// EM.
+public class TypeCheckingEntityManager implements CayenneEntityManager {
+
+    protected CayenneEntityManager entityManager;
+
+    public TypeCheckingEntityManager(CayenneEntityManager entityManager) {
+        this.entityManager = entityManager;
+    }
+
+    protected void checkEntityType(Class entityClass) throws IllegalArgumentException {
+        if (entityClass == null) {
+            throw new IllegalArgumentException("Null entity class");
+        }
+
+        if (!Persistent.class.isAssignableFrom(entityClass)) {
+            throw new IllegalArgumentException("Entity class must be Persistent, got: "
+                    + entityClass.getName());
+        }
+    }
+
+    protected void checkEntityType(Object entity) throws IllegalArgumentException {
+        if (entity == null) {
+            throw new IllegalArgumentException("Null entity");
+        }
+
+        if (!(entity instanceof Persistent)) {
+            String className = (entity != null) ? entity.getClass().getName() : "<null>";
+            throw new IllegalArgumentException("entity must be Persistent: " + className);
+        }
+    }
+
+    protected void enhance(Class entityClass) {
+
+    }
+
+    public void clear() {
+        entityManager.clear();
+    }
+
+    public void close() {
+        entityManager.close();
+    }
+
+    public boolean contains(Object entity) {
+        checkEntityType(entity);
+        return entityManager.contains(entity);
+    }
+
+    public Query createNamedQuery(String name) {
+        return entityManager.createNamedQuery(name);
+    }
+
+    public Query createNativeQuery(String sqlString, Class resultClass) {
+        checkEntityType(resultClass);
+        return entityManager.createNativeQuery(sqlString, resultClass);
+    }
+
+    public Query createNativeQuery(String sqlString, String resultSetMapping) {
+        return entityManager.createNativeQuery(sqlString, resultSetMapping);
+    }
+
+    public Query createNativeQuery(String sqlString) {
+        return entityManager.createNativeQuery(sqlString);
+    }
+
+    public Query createQuery(String ejbqlString) {
+        return entityManager.createQuery(ejbqlString);
+    }
+
+    public <T> T find(Class<T> entityClass, Object primaryKey) {
+        checkEntityType(entityClass);
+        return entityManager.find(entityClass, primaryKey);
+    }
+
+    public void flush() {
+        entityManager.flush();
+    }
+
+    public DataChannel getChannel() {
+        return entityManager.getChannel();
+    }
+
+    public Object getDelegate() {
+        return entityManager.getDelegate();
+    }
+
+    public FlushModeType getFlushMode() {
+        return entityManager.getFlushMode();
+    }
+
+    public <T> T getReference(Class<T> entityClass, Object primaryKey) {
+        return entityManager.getReference(entityClass, primaryKey);
+    }
+
+    public EntityTransaction getTransaction() {
+        return entityManager.getTransaction();
+    }
+
+    public boolean isOpen() {
+        return entityManager.isOpen();
+    }
+
+    public void joinTransaction() {
+        entityManager.joinTransaction();
+    }
+
+    public void lock(Object entity, LockModeType lockMode) {
+        entityManager.lock(entity, lockMode);
+    }
+
+    public <T> T merge(T entity) {
+        checkEntityType(entity);
+        return entityManager.merge(entity);
+    }
+
+    public void persist(Object entity) {
+        checkEntityType(entity);
+        entityManager.persist(entity);
+    }
+
+    public void refresh(Object entity) {
+        entityManager.refresh(entity);
+    }
+
+    public void remove(Object entity) {
+        checkEntityType(entity);
+        entityManager.remove(entity);
+    }
+
+    public void setFlushMode(FlushModeType flushMode) {
+        entityManager.setFlushMode(flushMode);
+    }
+}



Mime
View raw message