EntityBean

 EntityBean 主要用来保存一个Entity的对象,同时操作PassivateEntity对保存的实体进行钝化或者激活.
 也可以说是对EntityBean的包装,当对一个EntityBean包装时,便可以对此Entity进行精确的version控制
 package org.jboss.seam.contexts;

import static org.jboss.seam.contexts.PassivatedEntity.passivateEntity;

/**
 * Swizzles entities held in the conversation context at
 * the end of each request.
 * 
 * @see PassivatedEntity
 * 
 * @author Gavin King
 *
 */
class EntityBean implements Wrapper
{
   private static final long serialVersionUID = -2884601453783925804L;
   
   private Object instance;
   private PassivatedEntity passivatedEntity;
   private transient boolean initialized;
   
   public EntityBean(Object instance)
   {
      this.instance = instance;
      initialized = true;
   }
   
   //TODO: use @Unwrap
   public Object getInstance()
   {
      if ( !initialized && passivatedEntity!=null ) //没初实化但是已经钝化实体
      {
         //non-versioned entities can be lazily unpassivated 
         instance = passivatedEntity.toEntityReference(true); //version check
      }
      initialized = true;
      return instance;
   }
   
   public boolean passivate()
   {    //钝化实体为空,或者钝化实体的版本改变,才能创建
      if ( passivatedEntity==null || passivatedEntity.isVersioned() ) //and the version number changed!
      {
         if ( PassivatedEntity.isTransactionRolledBackOrMarkedRollback() ) //事物是否标记为回滚
         {
            passivatedEntity = null;
         }
         else
         {
            passivatedEntity = passivateEntity(instance); //创建钝化实体
            //if (passivatedEntity!=null) instance = null; //for performance of serialization
         }
         return true;
      }
      else
      {
         return false;
      }
   }
   
   public void activate()
   {
      //versioned entities must be unpassivated at the beginning 
      //of the request 
      if ( passivatedEntity!=null && passivatedEntity.isVersioned() )
      {
         instance = passivatedEntity.toEntityReference(true);
         initialized = true;
      }
      else
      {
         initialized = false;
      }
   }
   
}


你可能感兴趣的:(jboss,performance,seam)