[seam-commits] Seam SVN: r13581 - in modules/faces/trunk: api/src/main/java/org/jboss/seam/faces/transaction and 4 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Aug 9 06:58:32 EDT 2010


Author: swd847
Date: 2010-08-09 06:58:31 -0400 (Mon, 09 Aug 2010)
New Revision: 13581

Added:
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransaction.java
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransactionType.java
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/transaction/
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/transaction/TransactionPhaseListener.java
Modified:
   modules/faces/trunk/impl/pom.xml
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/DelegatingPhaseListener.java
Log:
Seam managed transactions, no tests yet



Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransaction.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransaction.java	                        (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransaction.java	2010-08-09 10:58:31 UTC (rev 13581)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.transaction;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Configuration annotation for seam managed transactions
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.FIELD)
+public @interface SeamManagedTransaction
+{
+   SeamManagedTransactionType value();
+}

Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransactionType.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransactionType.java	                        (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/transaction/SeamManagedTransactionType.java	2010-08-09 10:58:31 UTC (rev 13581)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.transaction;
+
+/**
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public enum SeamManagedTransactionType
+{
+
+   ENABLED, DISABLED,
+   /**
+    * Transactions are only enabled during the RENDER_RESPONSE phase
+    */
+   RENDER_RESPONSE;
+}

Modified: modules/faces/trunk/impl/pom.xml
===================================================================
--- modules/faces/trunk/impl/pom.xml	2010-08-09 10:45:30 UTC (rev 13580)
+++ modules/faces/trunk/impl/pom.xml	2010-08-09 10:58:31 UTC (rev 13581)
@@ -1,59 +1,70 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-	<modelVersion>4.0.0</modelVersion>
+   <modelVersion>4.0.0</modelVersion>
 
-	<parent>
-		<artifactId>seam-faces-parent</artifactId>
-		<groupId>org.jboss.seam.faces</groupId>
-		<version>3.0.0-SNAPSHOT</version>
+   <parent>
+      <artifactId>seam-faces-parent</artifactId>
+      <groupId>org.jboss.seam.faces</groupId>
+      <version>3.0.0-SNAPSHOT</version>
       <relativePath>../pom.xml</relativePath>
-	</parent>
+   </parent>
 
-	<artifactId>seam-faces</artifactId>
-	<version>3.0.0-SNAPSHOT</version>
+   <artifactId>seam-faces</artifactId>
+   <version>3.0.0-SNAPSHOT</version>
 
-	<packaging>jar</packaging>
-	<name>Seam Faces Module Core Implementation</name>
+   <packaging>jar</packaging>
+   <name>Seam Faces Module Core Implementation</name>
 
-	<dependencies>
-		<dependency>
+   <dependencies>
+      <dependency>
          <!-- Required until the Servlet 3.0 API can be resolved in Central -->
          <groupId>org.jboss.spec.javax.servlet</groupId>
          <artifactId>jboss-servlet-api_3.0_spec</artifactId>
          <scope>provided</scope>
       </dependency>
-		<dependency>
-			<artifactId>seam-faces-api</artifactId>
-			<groupId>org.jboss.seam.faces</groupId>
-			<version>${project.version}</version>
-			<scope>compile</scope>
-		</dependency>
-		<dependency>
-			<groupId>org.jboss.seam.international</groupId>
-			<artifactId>seam-international</artifactId>
-			<version>${seam.i18n.version}</version>
-		</dependency>
-	</dependencies>
+      <dependency>
+         <artifactId>seam-faces-api</artifactId>
+         <groupId>org.jboss.seam.faces</groupId>
+         <version>${project.version}</version>
+         <scope>compile</scope>
+      </dependency>
+      <dependency>
+         <artifactId>seam-persistence-api</artifactId>
+         <groupId>org.jboss.seam.persistence</groupId>
+         <version>${project.version}</version>
+         <scope>compile</scope>
+      </dependency>
+      <dependency>
+         <groupId>org.jboss.seam.international</groupId>
+         <artifactId>seam-international</artifactId>
+         <version>${seam.i18n.version}</version>
+      </dependency>
+      <dependency>
+         <groupId>javax.transaction</groupId>
+         <artifactId>jta</artifactId>
+         <scope>provided</scope>
+      </dependency>
+   </dependencies>
 
-	<profiles>
-		<profile>
-			<id>code-coverage</id>
-			<build>
-				<plugins>
-					<plugin>
-						<groupId>org.codehaus.mojo</groupId>
-						<artifactId>emma-maven-plugin</artifactId>
-					</plugin>
-					<plugin>
-						<groupId>org.apache.maven.plugins</groupId>
-						<artifactId>maven-surefire-plugin</artifactId>
-					</plugin>
-					<plugin>
-						<groupId>org.sonatype.maven.plugin</groupId>
-						<artifactId>emma4it-maven-plugin</artifactId>
-					</plugin>
-				</plugins>
-			</build>
-		</profile>
-	</profiles>
+   <profiles>
+      <profile>
+         <id>code-coverage</id>
+         <build>
+            <plugins>
+               <plugin>
+                  <groupId>org.codehaus.mojo</groupId>
+                  <artifactId>emma-maven-plugin</artifactId>
+               </plugin>
+               <plugin>
+                  <groupId>org.apache.maven.plugins</groupId>
+                  <artifactId>maven-surefire-plugin</artifactId>
+               </plugin>
+               <plugin>
+                  <groupId>org.sonatype.maven.plugin</groupId>
+                  <artifactId>emma4it-maven-plugin</artifactId>
+               </plugin>
+            </plugins>
+         </build>
+      </profile>
+   </profiles>
 
 </project>

Modified: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/DelegatingPhaseListener.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/DelegatingPhaseListener.java	2010-08-09 10:45:30 UTC (rev 13580)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/event/DelegatingPhaseListener.java	2010-08-09 10:58:31 UTC (rev 13581)
@@ -28,6 +28,7 @@
 import javax.faces.event.PhaseListener;
 
 import org.jboss.seam.faces.context.FlashScopedContext;
+import org.jboss.seam.faces.transaction.TransactionPhaseListener;
 
 /**
  * Provide CDI injection to PhaseListener artifacts by delegating through this
@@ -79,7 +80,7 @@
    @SuppressWarnings("unchecked")
    private List<PhaseListener> getPhaseListeners()
    {
-      return getListeners(FlashScopedContext.class, PhaseEventBridge.class);
+      return getListeners(FlashScopedContext.class, PhaseEventBridge.class, TransactionPhaseListener.class);
    }
 
 }

Added: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/transaction/TransactionPhaseListener.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/transaction/TransactionPhaseListener.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/transaction/TransactionPhaseListener.java	2010-08-09 10:58:31 UTC (rev 13581)
@@ -0,0 +1,196 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.seam.faces.transaction;
+
+import static javax.faces.event.PhaseId.ANY_PHASE;
+import static javax.faces.event.PhaseId.RENDER_RESPONSE;
+
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+import javax.inject.Inject;
+
+import org.jboss.seam.faces.viewdata.ViewDataStore;
+import org.jboss.seam.persistence.transaction.PersistenceContexts;
+import org.jboss.seam.persistence.transaction.SeamTransaction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Phase listener that is resposible for seam managed transactions. It is also
+ * resposible for setting the correct flush mode on the persistence context
+ * during the render response phase
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class TransactionPhaseListener implements PhaseListener
+{
+   private static final long serialVersionUID = -9127555729455066493L;
+
+   private static final Logger log = LoggerFactory.getLogger(TransactionPhaseListener.class);
+
+   @Inject
+   SeamTransaction transaction;
+
+   @Inject
+   ViewDataStore dataStore;
+
+   @Inject
+   PersistenceContexts persistenceContexts;
+
+   public PhaseId getPhaseId()
+   {
+      return ANY_PHASE;
+   }
+
+   public void beforePhase(PhaseEvent event)
+   {
+      log.trace("before phase: " + event.getPhaseId());
+      handleTransactionsBeforePhase(event);
+   }
+
+   public void afterPhase(PhaseEvent event)
+   {
+      if (event.getPhaseId() == RENDER_RESPONSE)
+      {
+         persistenceContexts.afterRender();
+      }
+      handleTransactionsAfterPhase(event);
+   }
+
+
+   public void handleTransactionsBeforePhase(PhaseEvent event)
+   {
+      PhaseId phaseId = event.getPhaseId();
+      if (seamManagedTransactionStatus(phaseId))
+      {
+         if (phaseId == RENDER_RESPONSE)
+         {
+            persistenceContexts.beforeRender();
+         }
+         boolean beginTran = (phaseId == PhaseId.RENDER_RESPONSE || phaseId == PhaseId.RESTORE_VIEW);
+         if (beginTran)
+         {
+            begin(phaseId);
+         }
+      }
+   }
+
+   public void handleTransactionsAfterPhase(PhaseEvent event)
+   {
+      PhaseId phaseId = event.getPhaseId();
+      if (seamManagedTransactionStatus(phaseId))
+      {
+         boolean commitTran = phaseId == PhaseId.INVOKE_APPLICATION || event.getFacesContext().getRenderResponse() ||
+               event.getFacesContext().getResponseComplete() || phaseId == PhaseId.RENDER_RESPONSE;
+
+         if (commitTran)
+         {
+            commitOrRollback(phaseId); // we commit before destroying contexts,
+                                       // cos the contexts have the PC in them
+         }
+      }
+   }
+
+   void begin(PhaseId phaseId)
+   {
+      begin("prior to phase: " + phaseId);
+   }
+
+   void begin(String phaseString)
+   {
+      try
+      {
+         if (!transaction.isActiveOrMarkedRollback())
+         {
+            log.debug("beginning transaction " + phaseString);
+            transaction.begin();
+         }
+      }
+      catch (Exception e)
+      {
+         throw new IllegalStateException("Could not start transaction", e);
+      }
+   }
+
+   void commitOrRollback(PhaseId phaseId)
+   {
+      commitOrRollback("after phase: " + phaseId);
+   }
+
+   void commitOrRollback(String phaseString)
+   {
+      try
+      {
+         if (transaction.isActive())
+         {
+            try
+            {
+               log.debug("committing transaction " + phaseString);
+               transaction.commit();
+
+             }
+            catch (IllegalStateException e)
+            {
+               log.warn("TX commit failed with illegal state exception. This may be because the tx timed out and was rolled back in the background.", e);
+            }
+         }
+         else if (transaction.isRolledBackOrMarkedRollback())
+         {
+            log.debug("rolling back transaction " + phaseString);
+            transaction.rollback();
+         }
+
+      }
+      catch (Exception e)
+      {
+         throw new IllegalStateException("Could not commit transaction", e);
+      }
+   }
+
+   private boolean seamManagedTransactionStatus(PhaseId phase)
+   {
+      SeamManagedTransaction an = dataStore.getData(SeamManagedTransaction.class);
+      SeamManagedTransactionType config;
+      if (an == null)
+      {
+         // enable seam managed transactions by default
+         config = SeamManagedTransactionType.ENABLED;
+      }
+      else
+      {
+         config = an.value();
+      }
+      if (config == SeamManagedTransactionType.DISABLED)
+      {
+         return false;
+      }
+      else if (config == SeamManagedTransactionType.RENDER_RESPONSE && phase != PhaseId.RENDER_RESPONSE)
+      {
+         return false;
+      }
+      return true;
+   }
+}



More information about the seam-commits mailing list