openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1584153 - in /openjpa/branches/2.2.1.x: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/ openjpa-persistence-jdbc/src/test/resources/META-INF/
Date Wed, 02 Apr 2014 20:15:48 GMT
Author: hthomann
Date: Wed Apr  2 20:15:48 2014
New Revision: 1584153

URL: http://svn.apache.org/r1584153
Log:
OPENJPA-2437: transactional listeners added too late to observe begin event - back ported
Mark Struber's changes to 2.2.1.x.

Added:
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/AuditedEntry.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
Modified:
    openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
    openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml

Modified: openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?rev=1584153&r1=1584152&r2=1584153&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
Wed Apr  2 20:15:48 2014
@@ -226,8 +226,6 @@ public abstract class AbstractBrokerFact
         DelegatingStoreManager dsm = createDelegatingStoreManager();
 
         ((BrokerImpl) broker).initialize(this, dsm, managed, connRetainMode, fromDeserialization);
-        if (!fromDeserialization)
-            addListeners(broker);
 
         // if we're using remote events, register the event manager so
         // that it can broadcast commit notifications from the broker

Modified: openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1584153&r1=1584152&r2=1584153&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Wed Apr  2 20:15:48 2014
@@ -388,6 +388,11 @@ public class BrokerImpl
         _printParameters =
             Boolean.parseBoolean(Configurations.parseProperties(_conf.getConnectionFactoryProperties()).getProperty(
                 PRINT_PARAMETERS_CONFIG_STR, "false"));
+
+        // do it before begin event otherwise transactional listeners can't use it, see @Auditable
+        if (!fromDeserialization)
+            _factory.addListeners(this);
+
         // synch with the global transaction in progress, if any
         if (_factory.syncWithManagedTransaction(this, false))
             beginInternal();

Added: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/AuditedEntry.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/AuditedEntry.java?rev=1584153&view=auto
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/AuditedEntry.java
(added)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/AuditedEntry.java
Wed Apr  2 20:15:48 2014
@@ -0,0 +1,119 @@
+/*
+ * 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.audit;
+
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.ElementCollection;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.kernel.Audited;
+import org.apache.openjpa.persistence.Type;
+
+/**
+ * An example of an immutable persistent entity that holds a reference to the entity being
audited.
+ * <br>
+ * This entity holds the reference to the entity being audited in a <em>generic</em>
+ * sense i.e. it does not know the exact type of the audited entity, but merely that
+ * it is a {@link PersistenceCapable} instance.
+ * <br>
+ * OpenJPA supports such reference by annotating with the {@link #audited reference field}
as 
+ * <tt>@Type(PersistenceCapable.class)</tt>.
+ * <br>
+ * The audit operation is also represented as a {@link #operation enumerated field}.
+ *    
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+public class AuditedEntry {
+	@Id
+	@GeneratedValue
+	private long id;
+	
+	@ManyToOne(cascade=CascadeType.MERGE)
+	@Type(PersistenceCapable.class)
+	private Object audited;
+	
+	@Enumerated(EnumType.STRING)
+	private AuditableOperation operation;
+	
+	@Temporal(TemporalType.TIMESTAMP)
+	private Timestamp ts;
+	
+	@ElementCollection
+	private List<String> updatedFields;
+	
+	/**
+	 * Constructs using an {@link Audited audited} instance.
+	 * <br>
+	 * An audited instances are supplied to the {@link Auditor#audit(Broker, Collection, Collection,
Collection)
+	 * auditor} by OpenJPA runtime within the transaction just before it is going to commit.
+	 * <br>
+	 * An audited instance carries the managed instance being audited in two <em>separate</em>
references.
+	 * The {link {@link Audited#getManagedObject()} first reference} is to the actual persistence
instance
+	 * being audited. The {link {@link Audited#getOriginalObject() second reference} is a <em>transient</em>
+	 * copy of the actual persistence instance but in a state as it were when it entered the
managed context
+	 * i.e. when it was persisted or loaded from the database.
+	 * <br>
+	 * The {@link Audited} instance also knows the fields that were updated.    
+	 * @param a an audited instance.
+	 */
+	public AuditedEntry(Audited a) {
+		audited = a.getManagedObject();
+		ts = new Timestamp(new Date().getTime());
+		operation = a.getType();
+		if (operation == AuditableOperation.UPDATE) {
+			updatedFields = Arrays.asList(a.getUpdatedFields());
+		}
+		
+	}
+
+	public Object getAudited() {
+		return audited;
+	}
+
+	public AuditableOperation getOperation() {
+		return operation;
+	}
+
+	public Timestamp getTimestamp() {
+		return ts;
+	}
+
+	public long getId() {
+		return id;
+	}
+	
+	public List<String> getUpdatedFields() {
+		return updatedFields;
+	}
+}

Added: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java?rev=1584153&view=auto
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
(added)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
Wed Apr  2 20:15:48 2014
@@ -0,0 +1,106 @@
+/*
+ * 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.audit;
+
+import junit.framework.TestCase;
+import org.apache.openjpa.ee.ManagedRuntime;
+import org.apache.openjpa.kernel.Audited;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.lib.conf.Configuration;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import java.util.Collection;
+
+public class TestBeginEventOnTransactionListener extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(X.class, AuditedEntry.class, CLEAR_TABLES);
+    }
+
+    @Override
+    protected String getPersistenceUnitName() {
+        return "auditjta";
+    }
+
+
+    public void test() throws Exception {
+        doTest(emf);
+        assertTrue(MockAuditor.called);
+    }
+
+    private void doTest(final EntityManagerFactory emf) throws Exception {
+        final ManagedRuntime runtime = OpenJPAEntityManagerFactorySPI.class.cast(emf)
+                .getConfiguration().getManagedRuntimeInstance();
+
+        runtime.getTransactionManager().begin();
+        try {
+            final EntityManager em = emf.createEntityManager();
+            em.joinTransaction();
+
+            final X x = new X();
+            em.persist(x);
+            runtime.getTransactionManager().commit();
+        }
+        finally {
+            emf.close();
+        }
+    }
+
+
+    public static class MockAuditor implements Auditor
+    {
+        public static boolean called = false;
+
+        @Override
+        public void audit(Broker broker, Collection<Audited> newObjects,
+                          Collection<Audited> updates, Collection<Audited> deletes)
{
+            called = true;
+        }
+
+        @Override
+        public boolean isRollbackOnError() {
+            return false;
+        }
+
+        @Override
+        public void close() throws Exception {
+
+        }
+
+        @Override
+        public void setConfiguration(Configuration conf) {
+
+        }
+
+        @Override
+        public void startConfiguration() {
+
+        }
+
+        @Override
+        public void endConfiguration() {
+
+        }
+    }
+
+}

Added: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java?rev=1584153&view=auto
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
(added)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/X.java
Wed Apr  2 20:15:48 2014
@@ -0,0 +1,90 @@
+/*
+ * 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.audit;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+
+/**
+ * A simple persistent entity used to test audit facility.
+ * An entity is annotated with {@link Auditable} annotation to qualify for audit.
+ *  
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@Auditable
+public class X {
+	@Id
+	private long id;
+	
+	private String name;
+	private int price;
+	
+	private static AtomicLong ID_GENERATOR = new AtomicLong(System.currentTimeMillis());
+	
+	public X() {
+		id = ID_GENERATOR.getAndIncrement();
+	}
+	
+	public String getName() {
+		return name;
+	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public int getPrice() {
+		return price;
+	}
+	public void setPrice(int price) {
+		this.price = price;
+	}
+	public long getId() {
+		return id;
+	}
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + (int) (id ^ (id >>> 32));
+		return result;
+	}
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		X other = (X) obj;
+		if (id != other.id)
+			return false;
+		return true;
+	}
+	
+	public String toString() {
+		return "X[" + id + "]";
+	}
+}

Modified: openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml?rev=1584153&r1=1584152&r2=1584153&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
(original)
+++ openjpa/branches/2.2.1.x/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
Wed Apr  2 20:15:48 2014
@@ -450,4 +450,16 @@
         </properties>
     </persistence-unit>
 
+  <persistence-unit name="auditjta" transaction-type="JTA">
+    <class>org.apache.openjpa.audit.X</class>
+    <class>org.apache.openjpa.audit.AuditedEntry</class>
+
+    <properties>
+      <property name="openjpa.Auditor" value="org.apache.openjpa.audit.TestBeginEventOnTransactionListener$MockAuditor"
/>
+      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"
/>
+      <property name="openjpa.DynamicEnhancementAgent" value="false" />
+      <property name="openjpa.ManagedRuntime" value="org.apache.openjpa.jta.JTAManagedRuntime"/>
+    </properties>
+  </persistence-unit> 
+
 </persistence>



Mime
View raw message