[seam-commits] Seam SVN: r8919 - in trunk/src/main/org/jboss/seam: persistence and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Sep 5 06:49:53 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-09-05 06:49:51 -0400 (Fri, 05 Sep 2008)
New Revision: 8919

Added:
   trunk/src/main/org/jboss/seam/persistence/ManagedEntityInterceptor.java
   trunk/src/main/org/jboss/seam/persistence/ManagedEntityWrapper.java
Removed:
   trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java
   trunk/src/main/org/jboss/seam/persistence/ManagedEntityStateManager.java
Modified:
   trunk/src/main/org/jboss/seam/contexts/PassivatedEntity.java
Log:
Slight refactor of MEII, add logging

Modified: trunk/src/main/org/jboss/seam/contexts/PassivatedEntity.java
===================================================================
--- trunk/src/main/org/jboss/seam/contexts/PassivatedEntity.java	2008-09-05 10:43:40 UTC (rev 8918)
+++ trunk/src/main/org/jboss/seam/contexts/PassivatedEntity.java	2008-09-05 10:49:51 UTC (rev 8919)
@@ -17,7 +17,7 @@
  * id and persistence context name.
  * 
  * @see EntityBean
- * @see org.jboss.seam.persistence.ManagedEntityIdentityInterceptor
+ * @see org.jboss.seam.persistence.ManagedEntityInterceptor
  * 
  * @author Gavin King
  *

Deleted: trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java	2008-09-05 10:43:40 UTC (rev 8918)
+++ trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java	2008-09-05 10:49:51 UTC (rev 8919)
@@ -1,73 +0,0 @@
-package org.jboss.seam.persistence;
-
-import static org.jboss.seam.ScopeType.CONVERSATION;
-
-import org.jboss.seam.annotations.intercept.AroundInvoke;
-import org.jboss.seam.annotations.intercept.Interceptor;
-import org.jboss.seam.core.BijectionInterceptor;
-import org.jboss.seam.intercept.AbstractInterceptor;
-import org.jboss.seam.intercept.InvocationContext;
-import org.jboss.seam.transaction.Transaction;
-
-/**
- * Swizzles entity references around each invocation, maintaining referential
- * integrity even across passivation of the stateful bean or Seam-managed
- * extended persistence context, and allowing for more efficient replication.
- * 
- * @author Gavin King
- * 
- */
- at Interceptor(around = BijectionInterceptor.class)
-public class ManagedEntityIdentityInterceptor extends AbstractInterceptor
-{
-
-   private static ManagedEntityStateManager managedEntityStateManager = new ManagedEntityStateManager();
-
-   private boolean reentrant;
-
-   // TODO: cache the non-ignored fields, probably on Component
-
-   public boolean isInterceptorEnabled()
-   {
-      return getComponent().getScope() == CONVERSATION;
-   }
-
-   @AroundInvoke
-   public Object aroundInvoke(InvocationContext ctx) throws Exception
-   {
-      if (reentrant)
-      {
-         return ctx.proceed();
-      }
-      else
-      {
-         reentrant = true;
-         managedEntityStateManager.entityIdsToRefs(ctx.getTarget(), getComponent());
-         try
-         {
-            return ctx.proceed();
-         }
-         finally
-         {
-            if (!isTransactionRolledBackOrMarkedRollback())
-            {
-               managedEntityStateManager.entityRefsToIds(ctx.getTarget(), getComponent());
-               reentrant = false;
-            }
-         }
-      }
-   }
-
-   private static boolean isTransactionRolledBackOrMarkedRollback()
-   {
-      try
-      {
-         return Transaction.instance().isRolledBackOrMarkedRollback();
-      }
-      catch (Exception e)
-      {
-         return false;
-      }
-   }
-
-}

Copied: trunk/src/main/org/jboss/seam/persistence/ManagedEntityInterceptor.java (from rev 8912, trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java)
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/ManagedEntityInterceptor.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/persistence/ManagedEntityInterceptor.java	2008-09-05 10:49:51 UTC (rev 8919)
@@ -0,0 +1,91 @@
+package org.jboss.seam.persistence;
+
+import static org.jboss.seam.ScopeType.CONVERSATION;
+
+import java.io.Serializable;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.ComponentType;
+import org.jboss.seam.annotations.intercept.AroundInvoke;
+import org.jboss.seam.annotations.intercept.Interceptor;
+import org.jboss.seam.annotations.intercept.PostActivate;
+import org.jboss.seam.annotations.intercept.PrePassivate;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.BijectionInterceptor;
+import org.jboss.seam.core.Conversation;
+import org.jboss.seam.intercept.AbstractInterceptor;
+import org.jboss.seam.intercept.InvocationContext;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.transaction.Transaction;
+import org.jboss.seam.web.Session;
+
+/**
+ * Swizzles entity references around each invocation, maintaining referential
+ * integrity even across passivation of the stateful bean or Seam-managed
+ * extended persistence context, and allowing for more efficient replication.
+ * 
+ * @author Gavin King
+ * @author Pete Muir
+ * 
+ */
+ at Interceptor(around = BijectionInterceptor.class)
+public class ManagedEntityInterceptor extends AbstractInterceptor
+{
+
+   private static LogProvider log = Logging.getLogProvider(ManagedEntityInterceptor.class);
+   
+   private static ManagedEntityWrapper managedEntityWrapper = new ManagedEntityWrapper();
+
+   private boolean reentrant;
+   
+   @AroundInvoke
+   public Object aroundInvoke(InvocationContext ctx) throws Exception
+   {
+      if (reentrant)
+      {
+         return ctx.proceed();
+      }
+      else
+      {
+         reentrant = true;
+         log.trace("Attempting to activate " + getComponent().getName() + " component");
+         managedEntityWrapper.deserialize(ctx.getTarget(), getComponent());
+         log.debug("Activated " + getComponent().getName() + " component");
+         try
+         {
+            return ctx.proceed();
+         }
+         finally
+         {
+            if (!isTransactionRolledBackOrMarkedRollback())
+            {
+               log.trace("Attempting to passivate " + getComponent().getName() + " component");
+               managedEntityWrapper.wrap(ctx.getTarget(), getComponent());
+               reentrant = false;
+               log.debug("Passivated " + getComponent().getName() + " component");
+            }
+         }
+      }
+   }
+
+
+
+   public boolean isInterceptorEnabled()
+   {
+      return getComponent().getScope() == CONVERSATION;
+   }
+
+   private static boolean isTransactionRolledBackOrMarkedRollback()
+   {
+      try
+      {
+         return Transaction.instance().isRolledBackOrMarkedRollback();
+      }
+      catch (Exception e)
+      {
+         return false;
+      }
+   }
+
+}


Property changes on: trunk/src/main/org/jboss/seam/persistence/ManagedEntityInterceptor.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Deleted: trunk/src/main/org/jboss/seam/persistence/ManagedEntityStateManager.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/ManagedEntityStateManager.java	2008-09-05 10:43:40 UTC (rev 8918)
+++ trunk/src/main/org/jboss/seam/persistence/ManagedEntityStateManager.java	2008-09-05 10:49:51 UTC (rev 8919)
@@ -1,162 +0,0 @@
-package org.jboss.seam.persistence;
-
-import static org.jboss.seam.util.JSF.DATA_MODEL;
-import static org.jboss.seam.util.JSF.getWrappedData;
-import static org.jboss.seam.util.JSF.setWrappedData;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.jboss.seam.Component;
-import org.jboss.seam.Seam;
-import org.jboss.seam.annotations.In;
-import org.jboss.seam.contexts.Contexts;
-import org.jboss.seam.util.Reflections;
-
-/**
- * @author Gavin King
- * @author Pete Muir
- * @author Norman Richards
- *
- */
-public class ManagedEntityStateManager
-{
-
-   public void entityRefsToIds(Object controllerBean, Component component) throws Exception
-   {
-      if ( touchedContextsExist() )
-      {
-         Class beanClass = controllerBean.getClass();
-         for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
-         {
-            for ( Field field: beanClass.getDeclaredFields() )
-            {
-               if ( !ignore(field) )
-               {
-                  Object value = getFieldValue(controllerBean, field);
-                  if (value!=null)
-                  {
-                     Object dataModel = null;
-                     if ( DATA_MODEL.isInstance(value) )
-                     {
-                        dataModel = value;
-                        value = getWrappedData(dataModel);
-                     }
-                     if ( isRef(value) )
-                     {
-                        saveWrapper(controllerBean, component, field, dataModel, value);
-                     }
-                     else
-                     {
-                        clearWrapper(component, field);
-                     }
-                  }
-                  else
-                  {
-                     clearWrapper(component, field);
-                  }
-               }
-            }
-         }
-      }
-   }
-
-   public void entityIdsToRefs(Object controllerBean, Component component) throws Exception
-   {      
-      if ( touchedContextsExist() )
-      {
-         Class beanClass = controllerBean.getClass();
-         for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
-         {
-            for ( Field field: beanClass.getDeclaredFields() )
-            {
-               if ( !ignore(field) )
-               {
-                  Object value = getFieldValue(controllerBean, field);
-                  Object dataModel = null;
-                  if (value!=null && DATA_MODEL.isInstance(value) )
-                  {
-                     dataModel = value;
-                  }
-                  //TODO: be more selective
-                  getFromWrapper(controllerBean, component, field, dataModel);
-               }
-            }
-         }
-      }
-   }
-
-   private boolean isRef(Object value)
-   {
-      //TODO: could do better by checking if the
-      //      collection really contains an entity
-      return value instanceof List || 
-            value instanceof Map || 
-            value instanceof Set || 
-            Seam.getEntityClass(value.getClass()) != null;
-   }
-
-   private Object getFieldValue(Object bean, Field field) throws Exception
-   {
-      if ( !field.isAccessible() ) field.setAccessible(true);
-      Object value = Reflections.get(field, bean);
-      return value;
-   }
-
-   private boolean ignore(Field field)
-   {
-      return Modifier.isTransient( field.getModifiers() ) || 
-            Modifier.isStatic( field.getModifiers() )
-            || field.isAnnotationPresent(In.class);
-   }
-
-   private boolean touchedContextsExist()
-   {
-       PersistenceContexts touchedContexts = PersistenceContexts.instance();
-       return touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0;
-   }
-
-   private String getFieldId(Component component, Field field)
-   {
-      return component.getName() + '.' + field.getName();
-   }
-
-   private void saveWrapper(Object bean, Component component, Field field, Object dataModel, Object value) throws Exception
-   {
-      Contexts.getConversationContext().set( getFieldId(component, field), value );
-      if (dataModel==null)
-      {
-         Reflections.set(field, bean, null);
-      }
-      else
-      {
-         setWrappedData(dataModel, null);
-      }
-   }
-
-   private void clearWrapper(Component component, Field field) throws Exception
-   {
-      Contexts.getConversationContext().remove( getFieldId(component, field) );
-   }
-
-   private void getFromWrapper(Object bean, Component component, Field field, Object dataModel) throws Exception
-   {
-      Object value =Contexts.getConversationContext().get( getFieldId(component, field) );
-      if (value!=null)
-      {
-         if (dataModel==null)
-         {
-            Reflections.set(field, bean, value);
-         }
-         else
-         {
-            setWrappedData(dataModel, value);
-         }
-      }
-   }
-
-   
-}

Copied: trunk/src/main/org/jboss/seam/persistence/ManagedEntityWrapper.java (from rev 8910, trunk/src/main/org/jboss/seam/persistence/ManagedEntityStateManager.java)
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/ManagedEntityWrapper.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/persistence/ManagedEntityWrapper.java	2008-09-05 10:49:51 UTC (rev 8919)
@@ -0,0 +1,188 @@
+package org.jboss.seam.persistence;
+
+import static org.jboss.seam.util.JSF.DATA_MODEL;
+import static org.jboss.seam.util.JSF.getWrappedData;
+import static org.jboss.seam.util.JSF.setWrappedData;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.Seam;
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.util.Reflections;
+
+/**
+ * @author Gavin King
+ * @author Pete Muir
+ * @author Norman Richards
+ *
+ */
+public class ManagedEntityWrapper
+{
+
+   private static LogProvider log = Logging.getLogProvider(ManagedEntityWrapper.class);
+   
+   public void wrap(Object target, Component component) throws Exception
+   {
+      if ( touchedContextsExist() )
+      {
+         Class beanClass = target.getClass();
+         for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
+         {
+            log.trace("Examining fields on " + beanClass);
+            for ( Field field: beanClass.getDeclaredFields() )
+            {
+               if ( !ignore(field) )
+               {
+                  Object value = getFieldValue(target, field);
+                  if (value!=null)
+                  {
+                     Object dataModel = null;
+                     if ( DATA_MODEL.isInstance(value) )
+                     {
+                        dataModel = value;
+                        value = getWrappedData(dataModel);
+                     }
+                     if ( isRef(value) )
+                     {
+                        log.trace("Attempting to save wrapper for " + field + " (" + value + ")");
+                        saveWrapper(target, component, field, dataModel, value);
+                     }
+                     else
+                     {
+                        log.trace("Clearing wrapper for " + field + " (" + value + ") as it isn't a entity reference");
+                        clearWrapper(component, field);
+                     }
+                  }
+                  else
+                  {
+                     log.trace("Clearing wrapper for " + field + " as it is null");
+                     clearWrapper(component, field);
+                  }
+               }
+               else
+               {
+                  log.trace("Ignoring field " + field + " as it is static, transient or annotated with @In");
+               }
+            }
+         }
+      }
+      else
+      {
+         log.trace("No touched persistence contexts");
+      }
+   }
+
+   public void deserialize(Object controllerBean, Component component) throws Exception
+   {      
+      if ( touchedContextsExist() )
+      {
+         Class beanClass = controllerBean.getClass();
+         for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
+         {
+            log.trace("Examining fields on " + beanClass);
+            for ( Field field: beanClass.getDeclaredFields() )
+            {
+               if ( !ignore(field) )
+               {
+                  Object value = getFieldValue(controllerBean, field);
+                  Object dataModel = null;
+                  if (value!=null && DATA_MODEL.isInstance(value) )
+                  {
+                     dataModel = value;
+                  }
+                  log.trace("Attempting to restore wrapper for " + field + " (" + value + ")");
+                  //TODO: be more selective
+                  getFromWrapper(controllerBean, component, field, dataModel);
+               }
+               else
+               {
+                  log.trace("Ignoring field " + field + " as it is static, transient or annotated with @In");
+               }
+            }
+         }
+      }
+      else
+      {
+         log.trace("No touched persistence contexts");
+      }
+   }
+
+   private boolean isRef(Object value)
+   {
+      //TODO: could do better by checking if the
+      //      collection really contains an entity
+      return value instanceof List || 
+            value instanceof Map || 
+            value instanceof Set || 
+            Seam.getEntityClass(value.getClass()) != null;
+   }
+
+   private Object getFieldValue(Object bean, Field field) throws Exception
+   {
+      if ( !field.isAccessible() ) field.setAccessible(true);
+      Object value = Reflections.get(field, bean);
+      return value;
+   }
+
+   private boolean ignore(Field field)
+   {
+      return Modifier.isTransient( field.getModifiers() ) || 
+            Modifier.isStatic( field.getModifiers() )
+            || field.isAnnotationPresent(In.class);
+   }
+
+   private boolean touchedContextsExist()
+   {
+       PersistenceContexts touchedContexts = PersistenceContexts.instance();
+       return touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0;
+   }
+
+   private String getFieldId(Component component, Field field)
+   {
+      return component.getName() + '.' + field.getName();
+   }
+
+   private void saveWrapper(Object bean, Component component, Field field, Object dataModel, Object value) throws Exception
+   {
+      Contexts.getConversationContext().set( getFieldId(component, field), value );
+      if (dataModel==null)
+      {
+         Reflections.set(field, bean, null);
+      }
+      else
+      {
+         setWrappedData(dataModel, null);
+      }
+   }
+
+   private void clearWrapper(Component component, Field field) throws Exception
+   {
+      Contexts.getConversationContext().remove( getFieldId(component, field) );
+   }
+
+   private void getFromWrapper(Object bean, Component component, Field field, Object dataModel) throws Exception
+   {
+      Object value =Contexts.getConversationContext().get( getFieldId(component, field) );
+      if (value!=null)
+      {
+         if (dataModel==null)
+         {
+            Reflections.set(field, bean, value);
+         }
+         else
+         {
+            setWrappedData(dataModel, value);
+         }
+      }
+   }
+
+   
+}


Property changes on: trunk/src/main/org/jboss/seam/persistence/ManagedEntityWrapper.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the seam-commits mailing list