openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r551885 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/event/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/ openjpa-persistence/src/main/java/...
Date Fri, 29 Jun 2007 13:18:27 GMT
Author: pcl
Date: Fri Jun 29 06:18:26 2007
New Revision: 551885

URL: http://svn.apache.org/viewvc?view=rev&rev=551885
Log:
OPENJPA-262. Added test case for lifecycle and tx registries in BrokerFactories. Also discovered
that BEFORE_PERSIST listeners (and possibly a couple of others) were not being invoked; fixed-by-inspection
and simplified the registration of types in the lifecycle event handler.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBrokerFactoryListenerRegistry.java
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/AbstractLifecycleListener.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/LifecycleEventManager.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/AbstractLifecycleListener.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/AbstractLifecycleListener.java?view=diff&rev=551885&r1=551884&r2=551885
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/AbstractLifecycleListener.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/AbstractLifecycleListener.java
Fri Jun 29 06:18:26 2007
@@ -34,6 +34,10 @@
     protected void eventOccurred(LifecycleEvent event) {
     }
 
+    public void beforePersist(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
     public void afterPersist(LifecycleEvent event) {
         eventOccurred(event);
     }
@@ -79,6 +83,26 @@
     }
 
     public void afterDirtyFlushed(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
+    public void afterRefresh(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
+    public void beforeDetach(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
+    public void afterDetach(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
+    public void beforeAttach(LifecycleEvent event) {
+        eventOccurred(event);
+    }
+
+    public void afterAttach(LifecycleEvent event) {
         eventOccurred(event);
     }
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/LifecycleEventManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/LifecycleEventManager.java?view=diff&rev=551885&r1=551884&r2=551885
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/LifecycleEventManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/event/LifecycleEventManager.java
Fri Jun 29 06:18:26 2007
@@ -131,75 +131,68 @@
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasPersistListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_PERSIST)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_PERSIST)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_PERSIST);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_PERSIST)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_PERSIST);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasDeleteListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_DELETE)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_DELETE)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_DELETE);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_DELETE)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_DELETE);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasClearListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_CLEAR)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_CLEAR)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_CLEAR);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_CLEAR)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_CLEAR);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasLoadListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.AFTER_LOAD)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_LOAD);
+        return hasHandlers(source, meta, LifecycleEvent.AFTER_LOAD);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasStoreListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_STORE)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_STORE)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_STORE);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_STORE)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_STORE);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasDirtyListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_DIRTY)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_DIRTY)
-            || hasCallbacks(source, meta, LifecycleEvent.BEFORE_DIRTY_FLUSHED)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_DIRTY_FLUSHED)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_DIRTY);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_DIRTY)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_DIRTY);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasDetachListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_DETACH)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_DETACH)
-            || hasListeners(source, meta, LifecycleEvent.BEFORE_DETACH)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_DETACH);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_DETACH)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_DETACH);
     }
 
     /**
      * Return whether there are listeners or callbacks for the given source.
      */
     public boolean hasAttachListeners(Object source, ClassMetaData meta) {
-        return hasCallbacks(source, meta, LifecycleEvent.BEFORE_ATTACH)
-            || hasCallbacks(source, meta, LifecycleEvent.AFTER_ATTACH)
-            || hasListeners(source, meta, LifecycleEvent.BEFORE_ATTACH)
-            || hasListeners(source, meta, LifecycleEvent.AFTER_ATTACH);
+        return hasHandlers(source, meta, LifecycleEvent.BEFORE_ATTACH)
+            || hasHandlers(source, meta, LifecycleEvent.AFTER_ATTACH);
+    }
+
+    private boolean hasHandlers(Object source, ClassMetaData meta, int type) {
+        return hasCallbacks(source, meta, type)
+            || hasListeners(source, meta, type);
     }
 
     /**
@@ -557,8 +550,10 @@
                 return types;
             }
 
-            if (listener instanceof PersistListener)
+            if (listener instanceof PersistListener) {
+                types |= 2 << LifecycleEvent.BEFORE_PERSIST;
                 types |= 2 << LifecycleEvent.AFTER_PERSIST;
+            }
             if (listener instanceof ClearListener) {
                 types |= 2 << LifecycleEvent.BEFORE_CLEAR;
                 types |= 2 << LifecycleEvent.AFTER_CLEAR;

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?view=diff&rev=551885&r1=551884&r2=551885
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
Fri Jun 29 06:18:26 2007
@@ -218,8 +218,7 @@
             Map.Entry entry;
             for (Iterator itr = _transactionListeners.iterator();
                 itr.hasNext(); ) {
-                entry = (Map.Entry) itr.next();
-                broker.addTransactionListener(entry.getKey());
+                broker.addTransactionListener(itr.next());
             }
         }
     }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBrokerFactoryListenerRegistry.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBrokerFactoryListenerRegistry.java?view=auto&rev=551885
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBrokerFactoryListenerRegistry.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBrokerFactoryListenerRegistry.java
Fri Jun 29 06:18:26 2007
@@ -0,0 +1,75 @@
+/*
+ * 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.openjpa.kernel;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+import org.apache.openjpa.persistence.simple.AllFieldTypes;
+import org.apache.openjpa.event.AbstractLifecycleListener;
+import org.apache.openjpa.event.AbstractTransactionListener;
+import org.apache.openjpa.event.LifecycleEvent;
+import org.apache.openjpa.event.TransactionEvent;
+
+public class TestBrokerFactoryListenerRegistry
+    extends SingleEMFTestCase {
+
+    private int persistCount = 0;
+    private int beginCount = 0;
+
+    @Override
+    protected void setUp() {
+        super.setUp(AllFieldTypes.class);
+    }
+
+    @Override
+    protected OpenJPAEntityManagerFactory createEMF(Object... props) {
+        OpenJPAEntityManagerFactory emf = super.createEMF(props);
+        emf.addLifecycleListener(new AbstractLifecycleListener() {
+            @Override
+            public void beforePersist(LifecycleEvent event) {
+                persistCount++;
+            }
+        }, null);
+        emf.addTransactionListener(new AbstractTransactionListener() {
+            @Override
+            public void afterBegin(TransactionEvent event) {
+                beginCount++;
+            }
+        });
+        return emf;
+    }
+
+    public void testLifecycleListenerRegistry() {
+        beginCount = 0;
+        persistCount = 0;
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        try {
+            em.persist(new AllFieldTypes());
+            em.flush();
+            assertEquals(1, beginCount);
+            assertEquals(1, persistCount);
+        } finally {
+            em.getTransaction().rollback();
+            em.close();
+        }
+    }
+}

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java?view=diff&rev=551885&r1=551884&r2=551885
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
Fri Jun 29 06:18:26 2007
@@ -272,6 +272,14 @@
         _factory.removeLifecycleListener(listener);
     }
 
+    public void addTransactionListener(Object listener) {
+        _factory.addTransactionListener(listener);
+    }
+
+    public void removeTransactionListener(Object listener) {
+        _factory.removeTransactionListener(listener);
+    }
+
     public void close() {
         _factory.close();
     }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.java?view=diff&rev=551885&r1=551884&r2=551885
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.java
Fri Jun 29 06:18:26 2007
@@ -93,7 +93,7 @@
      * Register a listener for lifecycle-related events on the specified
      * classes. If the classes are null, all events will be propagated to
      * the listener. The listener will be passed on to all new entity
-     * managers. See the <code>org.apache.openjpa.event</code> package for 
+     * managers. See the <code>org.apache.openjpa.event</code> package for
      * listener types.
      *
      * @since 0.3.3
@@ -106,4 +106,21 @@
      * @since 0.3.3
      */
     public void removeLifecycleListener (Object listener);
+
+    /**
+     * Register a listener for transaction-related events on the specified
+     * classes. The listener will be passed on to all new entity
+     * managers. See the <code>org.apache.openjpa.event</code> package for
+     * listener types.
+     *
+     * @since 1.0.0
+     */
+    public void addTransactionListener(Object listener);
+
+    /**
+     * Remove a listener for transaction-related events.
+     *
+     * @since 1.0.0
+     */
+    public void removeTransactionListener (Object listener);
 }



Mime
View raw message