[jboss-cvs] jboss-seam/src/main/org/jboss/seam/interceptors ...
Gavin King
gavin.king at jboss.com
Tue Dec 12 23:54:19 EST 2006
User: gavin
Date: 06/12/12 23:54:19
Modified: src/main/org/jboss/seam/interceptors
ManagedEntityIdentityInterceptor.java
Log:
maintain referential integrity for entity beans in conversation context
Revision Changes Path
1.15 +9 -107 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.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- ManagedEntityIdentityInterceptor.java 2 Dec 2006 07:51:33 -0000 1.14
+++ ManagedEntityIdentityInterceptor.java 13 Dec 2006 04:54:19 -0000 1.15
@@ -1,23 +1,16 @@
package org.jboss.seam.interceptors;
-import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
-import java.util.Set;
-import javax.persistence.EntityManager;
-
-import org.hibernate.Session;
-import org.jboss.seam.Component;
-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.PassivatedEntity;
import org.jboss.seam.core.PersistenceContexts;
import org.jboss.seam.intercept.InvocationContext;
-import org.jboss.seam.persistence.PersistenceProvider;
import org.jboss.seam.util.Reflections;
/**
@@ -37,39 +30,6 @@
private List<PassivatedEntity> list = new ArrayList<PassivatedEntity>();
- static class PassivatedEntity implements Serializable
- {
- private Object id;
- private String persistenceContext;
- private String fieldName;
- private Class<?> entityClass;
-
- PassivatedEntity(Object id, Class<?> entityClass, String fieldName, String persistenceContext)
- {
- super();
- this.id = id;
- this.persistenceContext = persistenceContext;
- this.fieldName = fieldName;
- this.entityClass = entityClass;
- }
- String getPersistenceContext()
- {
- return persistenceContext;
- }
- Object getId()
- {
- return id;
- }
- String getFieldName()
- {
- return fieldName;
- }
- Class<?> getEntityClass()
- {
- return entityClass;
- }
- }
-
@AroundInvoke
public Object aroundInvoke(InvocationContext ctx) throws Exception
{
@@ -89,13 +49,12 @@
PersistenceContexts touchedContexts = PersistenceContexts.instance();
if ( touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0 )
{
- Set<String> pcs = touchedContexts.getTouchedContexts();
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: fields )
{
boolean ignoreField = Modifier.isTransient( field.getModifiers() ) ||
Modifier.isStatic( field.getModifiers() )
@@ -106,53 +65,11 @@
Object value = Reflections.get(field, bean);
if (value!=null)
{
- Class entityClass = Seam.getEntityClass( value.getClass() );
- if (entityClass!=null)
- {
- for (String persistenceContextName: pcs)
- {
- Object persistenceContext = Component.getInstance(persistenceContextName);
- boolean managed;
- Object id;
- if (persistenceContext instanceof EntityManager)
- {
- EntityManager em = (EntityManager) persistenceContext;
- try
- {
- managed = em.isOpen() && em.contains(value);
- }
- catch (RuntimeException re)
- {
- //workaround for bug in HEM! //TODO; deleteme
- managed = false;
- }
- id = managed ? PersistenceProvider.instance().getId(value, em) : null;
- }
- else
- {
- Session session = (Session) persistenceContext;
- try
- {
- managed = session.isOpen() && session.contains(value);
- }
- catch (RuntimeException re)
- {
- //just in case! //TODO; deleteme
- managed = false;
- }
- id = managed ? session.getIdentifier(value) : null;
- }
- if (managed)
- {
- if (id==null)
+ PassivatedEntity pi = PassivatedEntity.createPassivatedEntity( value, field.getName() );
+ if (pi!=null)
{
- throw new IllegalStateException("could not get id of: " + beanClass.getName() + '.' + field.getName());
- }
- list.add( new PassivatedEntity( id, entityClass, field.getName(), persistenceContextName ) );
+ list.add(pi);
Reflections.set(field, bean, null);
- break;
- }
- }
}
}
}
@@ -169,22 +86,8 @@
Class beanClass = bean.getClass();
for (PassivatedEntity pe: list)
{
- Object persistenceContext = Component.getInstance( pe.getPersistenceContext() );
- if ( persistenceContext!=null )
- {
- Object reference;
- if (persistenceContext instanceof EntityManager)
- {
- EntityManager em = (EntityManager) persistenceContext;
- if ( !em.isOpen() ) continue;
- reference = em.getReference( pe.getEntityClass(), pe.getId() );
- }
- else
- {
- Session session = (Session) persistenceContext;
- if ( !session.isOpen() ) continue;
- reference = session.load( pe.getEntityClass(), (Serializable) pe.getId() );
- }
+ Object reference = pe.toEntityReference();
+ if (reference!=null)
for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
{
try
@@ -200,6 +103,5 @@
}
list.clear();
}
- }
}
More information about the jboss-cvs-commits
mailing list