[jboss-cvs] jboss-seam/src/main/org/jboss/seam/interceptors ...
Gavin King
gavin.king at jboss.com
Sat Jun 16 14:34:28 EDT 2007
User: gavin
Date: 07/06/16 14:34:28
Modified: src/main/org/jboss/seam/interceptors
BijectionInterceptor.java
BusinessProcessInterceptor.java
ManagedEntityIdentityInterceptor.java
Log:
JBSEAM-586
Revision Changes Path
1.30 +3 -5 jboss-seam/src/main/org/jboss/seam/interceptors/BijectionInterceptor.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BijectionInterceptor.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/BijectionInterceptor.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- BijectionInterceptor.java 11 Feb 2007 19:48:12 -0000 1.29
+++ BijectionInterceptor.java 16 Jun 2007 18:34:28 -0000 1.30
@@ -1,14 +1,12 @@
-//$Id: BijectionInterceptor.java,v 1.29 2007/02/11 19:48:12 gavin Exp $
+//$Id: BijectionInterceptor.java,v 1.30 2007/06/16 18:34:28 gavin Exp $
package org.jboss.seam.interceptors;
-
-
import org.jboss.seam.Component;
-import org.jboss.seam.log.LogProvider;
-import org.jboss.seam.log.Logging;
import org.jboss.seam.annotations.AroundInvoke;
import org.jboss.seam.annotations.Interceptor;
import org.jboss.seam.intercept.InvocationContext;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
/**
* Before invoking the component, inject all dependencies. After
1.55 +1 -1 jboss-seam/src/main/org/jboss/seam/interceptors/BusinessProcessInterceptor.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BusinessProcessInterceptor.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/BusinessProcessInterceptor.java,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -b -r1.54 -r1.55
--- BusinessProcessInterceptor.java 15 May 2007 20:39:21 -0000 1.54
+++ BusinessProcessInterceptor.java 16 Jun 2007 18:34:28 -0000 1.55
@@ -28,7 +28,7 @@
* Interceptor which handles interpretation of jBPM-related annotations.
*
* @author <a href="mailto:steve at hibernate.org">Steve Ebersole </a>
- * @version $Revision: 1.54 $
+ * @author Gavin King
*/
@Interceptor(stateless=true,
around={ValidationInterceptor.class, BijectionInterceptor.class})
1.25 +64 -243 jboss-seam/src/main/org/jboss/seam/interceptors/ManagedEntityIdentityInterceptor.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedEntityIdentityInterceptor.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/ManagedEntityIdentityInterceptor.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- ManagedEntityIdentityInterceptor.java 16 Jun 2007 16:30:13 -0000 1.24
+++ ManagedEntityIdentityInterceptor.java 16 Jun 2007 18:34:28 -0000 1.25
@@ -2,19 +2,15 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import javax.faces.model.DataModel;
+import org.jboss.seam.Seam;
import org.jboss.seam.annotations.AroundInvoke;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Interceptor;
+import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.PassivatedEntity;
import org.jboss.seam.core.PersistenceContexts;
import org.jboss.seam.intercept.InvocationContext;
@@ -31,18 +27,11 @@
* @author Gavin King
*
*/
- at Interceptor(around=BijectionInterceptor.class)
+ at Interceptor(around=BijectionInterceptor.class, stateless=true)
public class ManagedEntityIdentityInterceptor extends AbstractInterceptor
{
- private static final long serialVersionUID = 3105217046803964083L;
- //TODO: cache much more - the list of fields, PassivatedEntity obects, etc
- //TODO: optimize serialization of these maps...
- private Map<String, PassivatedEntity> passivatedEntities = new HashMap<String, PassivatedEntity>();
- //TODO: keep the actual concrete class of the collection around, so that we can recreate it after nullifying
- private Map<String, List<PassivatedEntity>> passivatedEntityLists = new HashMap<String, List<PassivatedEntity>>();
- private Map<String, List<PassivatedEntity>> passivatedEntitySets = new HashMap<String, List<PassivatedEntity>>();
- private Map<String, Map<Object, PassivatedEntity>> passivatedEntityMaps = new HashMap<String, Map<Object, PassivatedEntity>>();
+ //TODO: cache the non-ignored fields, probably on Component
@AroundInvoke
public Object aroundInvoke(InvocationContext ctx) throws Exception
@@ -63,23 +52,17 @@
public void entityRefsToIds(InvocationContext ctx) throws Exception
{
- PersistenceContexts touchedContexts = PersistenceContexts.instance();
- if ( touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0 )
+ if ( touchedContextsExist() )
{
Object bean = ctx.getTarget();
Class beanClass = bean.getClass();
for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
{
- Field[] fields = beanClass.getDeclaredFields();
- for ( Field field: fields )
+ for ( Field field: beanClass.getDeclaredFields() )
{
- boolean ignoreField = Modifier.isTransient( field.getModifiers() ) ||
- Modifier.isStatic( field.getModifiers() )
- || field.isAnnotationPresent(In.class);
- if ( !ignoreField )
+ if ( !ignore(field) )
{
- if ( !field.isAccessible() ) field.setAccessible(true);
- Object value = Reflections.get(field, bean);
+ Object value = getFieldValue(bean, field);
if (value!=null)
{
DataModel dataModel = null;
@@ -87,264 +70,102 @@
{
dataModel = (DataModel) value;
value = dataModel.getWrappedData();
- if ( !(value instanceof List) && !(value instanceof Map) )
- {
- //we can't handle DataModels that wrap a single entity directly!
- continue;
- }
- }
- entityRefsToIds(bean, field, value, dataModel);
- }
- }
- }
- }
- }
}
-
- private void entityRefsToIds(Object bean, Field field, Object value, DataModel dataModel) throws Exception
- {
- if (value instanceof List)
+ if ( isRef(value) )
{
- List values = (List) value;
- entityListToIdList(bean, field, values, dataModel);
+ saveWrapper(bean, field, dataModel, value);
}
- else if (value instanceof Set)
- {
- Set values = (Set) value;
- entitySetToIdSet(bean, field, values, dataModel);
}
- else if (value instanceof Map)
- {
- Map values = (Map) value;
- entityMapToIdMap(bean, field, values, dataModel);
}
- else
- {
- entityRefToId(bean, field, value);
}
}
-
- private void entityListToIdList(Object bean, Field field, List values, DataModel dataModel) throws Exception
- {
- int count = 0;
- List<PassivatedEntity> list = new ArrayList<PassivatedEntity>();
- for ( int i=0; i<values.size(); i++ )
- {
- PassivatedEntity pi = PassivatedEntity.createPassivatedEntity( values.get(i) );
- if (pi==null)
- {
- list.add(null);
- }
- else
- {
- list.add(pi);
- //values.set(i, null);
- count++;
- }
- }
- if (count>0)
- {
- passivatedEntityLists.put( field.getName(), list );
- if ( count==values.size() )
- {
- nullify(bean, field, dataModel);
- }
}
}
- private void entityMapToIdMap(Object bean, Field field, Map values, DataModel dataModel) throws Exception
+ public void entityIdsToRefs(InvocationContext ctx) throws Exception
{
- int count = 0;
- Map<Object, PassivatedEntity> map = new HashMap<Object, PassivatedEntity>();
- for ( Map.Entry me: (Set<Map.Entry>) values.entrySet() )
+ if ( touchedContextsExist() )
{
- PassivatedEntity pe = PassivatedEntity.createPassivatedEntity( me.getValue() );
- if (pe!=null)
+ Object bean = ctx.getTarget();
+ Class beanClass = bean.getClass();
+ for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
{
- map.put( me.getKey(), pe );
- //values.remove( me.getKey() );
- count++;
- }
- }
- if (count>0)
+ for ( Field field: beanClass.getDeclaredFields() )
{
- passivatedEntityMaps.put( field.getName(), map );
- if ( count==values.size() )
+ if ( !ignore(field) )
{
- nullify(bean, field, dataModel);
- }
- }
- }
-
- private void entitySetToIdSet(Object bean, Field field, Set values, DataModel dataModel) throws Exception
- {
- int count = 0;
- List<PassivatedEntity> list = new ArrayList<PassivatedEntity>();
- Iterator iter = values.iterator();
- while ( iter.hasNext() )
- {
- PassivatedEntity pe = PassivatedEntity.createPassivatedEntity( iter.next() );
- if (pe!=null)
+ Object value = getFieldValue(bean, field);
+ DataModel dataModel = null;
+ if (value!=null && value instanceof DataModel)
{
- list.add(pe);
- //iter.remove();
- count++;
+ dataModel = (DataModel) value;
}
+ //TODO: be more selective
+ getFromWrapper(bean, field, dataModel);
}
- if (count>0)
- {
- passivatedEntitySets.put( field.getName(), list );
- if ( count==values.size() )
- {
- nullify(bean, field, dataModel);
}
}
}
-
- private void entityRefToId(Object bean, Field field, Object value) throws Exception
- {
- PassivatedEntity pe = PassivatedEntity.createPassivatedEntity(value);
- if (pe!=null)
- {
- passivatedEntities.put( field.getName(), pe );
- Reflections.set(field, bean, null);
- }
}
- private void nullify(Object bean, Field field, DataModel dataModel) throws Exception
- {
- if (dataModel==null)
+ private boolean isRef(Object value)
{
- Reflections.set(field, bean, null);
- }
- /*else
- {
- //TODO: put back in, once we figure
- // out how to reconstruct it
- dataModel.setWrappedData(null);
- }*/
+ //TODO: can do better than this for lists!
+ return value instanceof List && Seam.isEntityClass( value.getClass() );
}
- public void entityIdsToRefs(InvocationContext ctx) throws Exception
- {
- if ( passivatedEntities.size()>0 || passivatedEntityLists.size()>0 )
- {
- Object bean = ctx.getTarget();
- Class beanClass = bean.getClass();
-
- for ( Map.Entry<String, PassivatedEntity> entry: passivatedEntities.entrySet() )
+ private Object getFieldValue(Object bean, Field field) throws Exception
{
- entityIdToRef(bean, beanClass, entry);
- }
- passivatedEntities.clear();
-
- for ( Map.Entry<String, List<PassivatedEntity>> entry: passivatedEntityLists.entrySet() )
- {
- entityIdListToList(bean, beanClass, entry);
+ if ( !field.isAccessible() ) field.setAccessible(true);
+ Object value = Reflections.get(field, bean);
+ return value;
}
- passivatedEntityLists.clear();
- for ( Map.Entry<String, List<PassivatedEntity>> entry: passivatedEntitySets.entrySet() )
+ private boolean ignore(Field field)
{
- entityIdSetToSet(bean, beanClass, entry);
+ return Modifier.isTransient( field.getModifiers() ) ||
+ Modifier.isStatic( field.getModifiers() )
+ || field.isAnnotationPresent(In.class);
}
- passivatedEntitySets.clear();
- for ( Map.Entry<String, Map<Object, PassivatedEntity>> entry: passivatedEntityMaps.entrySet() )
+ private boolean touchedContextsExist()
{
- entityIdMapToMap(bean, beanClass, entry);
- }
- passivatedEntityMaps.clear();
- }
+ PersistenceContexts touchedContexts = PersistenceContexts.instance();
+ return touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0;
}
- private void entityIdListToList(Object bean, Class beanClass, Map.Entry<String, List<PassivatedEntity>> entry) throws Exception
- {
- Field field = getField( beanClass, entry.getKey() );
- List<PassivatedEntity> list = entry.getValue();
- List values = (List) getFieldValues(bean, field);
- boolean recreate = false;
- if (values==null)
- {
- recreate = true;
- values = new ArrayList( list.size() );
- //TODO: reconstruct a DataModel
- Reflections.set(field, bean, values);
- }
- for ( int i=0; i<list.size(); i++ )
- {
- if (recreate) values.add(null);
- PassivatedEntity pe = list.get(i);
- if ( pe!=null )
+ private String getFieldId(Field field)
{
- values.set( i, pe.toEntityReference() );
- }
- }
+ return getComponent().getName() + '.' + field.getName();
}
- private void entityIdSetToSet(Object bean, Class beanClass, Map.Entry<String, List<PassivatedEntity>> entry) throws Exception
+ private void saveWrapper(Object bean, Field field, DataModel dataModel, Object value) throws Exception
{
- Field field = getField( beanClass, entry.getKey() );
- List<PassivatedEntity> list = entry.getValue();
- Set values = (Set) getFieldValues(bean, field);
- if (values==null)
+ Contexts.getConversationContext().set( getFieldId(field), value );
+ if (dataModel==null)
{
- values = new HashSet( list.size() );
- //TODO: reconstruct a DataModel
- Reflections.set(field, bean, values);
+ Reflections.set(field, bean, null);
}
- for ( PassivatedEntity pe: list )
- {
- Object reference = pe.toEntityReference();
- if (reference!=null)
+ else
{
- values.add(reference);
- }
+ dataModel.setWrappedData(null);
}
}
- private void entityIdMapToMap(Object bean, Class beanClass, Map.Entry<String, Map<Object, PassivatedEntity>> entry) throws Exception
+ private void getFromWrapper(Object bean, Field field, DataModel dataModel) throws Exception
{
- Field field = getField( beanClass, entry.getKey() );
- Map<Object, PassivatedEntity> map = entry.getValue();
- Map values = (Map) getFieldValues(bean, field);
- if (values==null)
- {
- values = new HashMap( map.size() );
- //TODO: reconstruct a DataModel
- Reflections.set(field, bean, values);
- }
- for ( Map.Entry<Object, PassivatedEntity> me: map.entrySet() )
+ Object value = Contexts.getConversationContext().get( getFieldId(field) );
+ if (value!=null)
{
- Object reference = me.getValue().toEntityReference();
- if (reference!=null)
+ if (dataModel==null)
{
- values.put( me.getKey(), reference );
- }
- }
+ Reflections.set(field, bean, value);
}
-
- private void entityIdToRef(Object bean, Class beanClass, Map.Entry<String, PassivatedEntity> entry) throws IllegalAccessException
- {
- Object reference = entry.getValue().toEntityReference();
- if (reference!=null)
+ else
{
- getField( beanClass, entry.getKey() ).set(bean, reference);
+ dataModel.setWrappedData(value);
}
}
-
- private Field getField(Class beanClass, String fieldName)
- {
- Field field = Reflections.getField(beanClass, fieldName);
- if ( !field.isAccessible() ) field.setAccessible(true);
- return field;
- }
-
- private Object getFieldValues(Object bean, Field field) throws IllegalAccessException
- {
- Object value = field.get(bean);
- return value!=null && value instanceof DataModel ?
- ( (DataModel) value ).getWrappedData() : value;
}
}
More information about the jboss-cvs-commits
mailing list