[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic ...

Jason Thomas Greene jgreene at jboss.com
Wed Jun 20 21:33:03 EDT 2007


  User: jgreene 
  Date: 07/06/20 21:33:03

  Modified:    src/org/jboss/cache/pojo/interceptors/dynamic     
                        AbstractCollectionInterceptor.java
                        CacheFieldInterceptor.java
                        CachedListInterceptor.java
                        CachedMapInterceptor.java CachedSetInterceptor.java
  Log:
  Fix JBCACHE-1057
  
  Revision  Changes    Path
  1.3       +28 -4     JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AbstractCollectionInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- AbstractCollectionInterceptor.java	30 May 2007 06:08:02 -0000	1.2
  +++ AbstractCollectionInterceptor.java	21 Jun 2007 01:33:03 -0000	1.3
  @@ -7,32 +7,44 @@
   
   package org.jboss.cache.pojo.interceptors.dynamic;
   
  +import org.jboss.aop.joinpoint.Invocation;
   import org.jboss.cache.Fqn;
  +import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
  +import org.jboss.cache.pojo.impl.PojoCacheImpl;
   import org.jboss.cache.pojo.impl.PojoInstance;
  +import org.jboss.cache.pojo.util.ObjectUtil;
   
   /**
    * Abstract base class for collection interceptor.
    *
    * @author Ben Wang
  - * @version $Id: AbstractCollectionInterceptor.java,v 1.2 2007/05/30 06:08:02 jgreene Exp $
  + * @version $Id: AbstractCollectionInterceptor.java,v 1.3 2007/06/21 01:33:03 jgreene Exp $
    */
   @SuppressWarnings({"CanBeFinal"})
   public abstract class AbstractCollectionInterceptor implements BaseInterceptor
   {
  -   Fqn fqn_;
  +   Fqn fqn;
  +   PojoCacheImpl cache;
  +
      private boolean attached_ = true;
      private PojoInstance pojoInstance_;
   
  +   AbstractCollectionInterceptor(PojoCacheImpl cache, Fqn fqn)
  +   {
  +      this.fqn = fqn;
  +      this.cache = cache;
  +   }
  +
      @SuppressWarnings({"CanBeFinal"})
      public Fqn getFqn()
      {
  -      return fqn_;
  +      return fqn;
      }
   
      @SuppressWarnings({"CanBeFinal"})
      public void setFqn(Fqn fqn)
      {
  -      this.fqn_ = fqn;
  +      this.fqn = fqn;
      }
   
      @SuppressWarnings({"CanBeFinal"})
  @@ -71,6 +83,18 @@
         return attached_;
      }
   
  +   /**
  +    * Check if the pojo is detached already.
  +    */
  +   public void verifyAttached(Object target)
  +   {
  +      if (cache.getCache().get(fqn, PojoInstance.KEY) != null)
  +         return;
  +
  +      String identity = ObjectUtil.identityString(target);
  +      throw new PojoCacheAlreadyDetachedException(identity + " has possibly been detached remotely. Internal id: " + fqn);
  +   }
  +
      abstract void setInMemoryCopy(Object obj);
      abstract Object getInMemoryCopy();
      abstract void setCacheCopy(Object obj);
  
  
  
  1.5       +21 -104   JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CacheFieldInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheFieldInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CacheFieldInterceptor.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- CacheFieldInterceptor.java	30 May 2007 06:08:02 -0000	1.4
  +++ CacheFieldInterceptor.java	21 Jun 2007 01:33:03 -0000	1.5
  @@ -6,31 +6,25 @@
    */
   package org.jboss.cache.pojo.interceptors.dynamic;
   
  +import java.lang.reflect.Field;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.jboss.aop.Advised;
   import org.jboss.aop.Advisor;
  -import org.jboss.aop.InstanceAdvisor;
   import org.jboss.aop.advice.Interceptor;
   import org.jboss.aop.joinpoint.FieldInvocation;
   import org.jboss.aop.joinpoint.FieldReadInvocation;
   import org.jboss.aop.joinpoint.FieldWriteInvocation;
   import org.jboss.aop.joinpoint.Invocation;
   import org.jboss.aop.joinpoint.MethodInvocation;
  -import org.jboss.cache.CacheSPI;
  +import org.jboss.cache.Cache;
   import org.jboss.cache.Fqn;
   import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
  -import org.jboss.cache.pojo.PojoCacheException;
   import org.jboss.cache.pojo.impl.CachedType;
   import org.jboss.cache.pojo.impl.PojoCacheImpl;
   import org.jboss.cache.pojo.impl.PojoInstance;
   import org.jboss.cache.pojo.impl.PojoUtil;
  -import org.jboss.cache.pojo.memory.FieldPersistentReference;
  -import org.jboss.cache.pojo.util.AopUtil;
  -import org.jboss.cache.pojo.util.CacheApiUtil;
  -
  -import java.lang.reflect.Field;
  -import java.util.Iterator;
  +import org.jboss.cache.pojo.util.ObjectUtil;
   
   /**
    * Main dynamic interceptor to intercept for field replication.
  @@ -41,10 +35,9 @@
   public class CacheFieldInterceptor implements BaseInterceptor
   {
      private final Log log_ = LogFactory.getLog(CacheFieldInterceptor.class);
  -   private CacheSPI cache_;
  +   Cache<Object, Object> cache_;
      private PojoCacheImpl pCache_;
  -   private CachedType type_;
  -   private Fqn fqn_;
  +   Fqn fqn_;
      private String name_;
      private PojoInstance pojoInstance_;
      private PojoUtil util_;
  @@ -52,9 +45,8 @@
      public CacheFieldInterceptor(PojoCacheImpl pCache, Fqn fqn, CachedType type)
      {
         this.pCache_ = pCache;
  -      cache_ = (CacheSPI) this.pCache_.getCache();
  +      this.cache_ = pCache_.getCache();
         this.fqn_ = fqn;
  -      this.type_ = type;
         util_ = new PojoUtil();
      }
   
  @@ -69,7 +61,7 @@
   
      public Object clone()
      {
  -      CacheFieldInterceptor interceptor = new CacheFieldInterceptor();
  +      BaseInterceptor interceptor = new CacheFieldInterceptor();
         interceptor.setFqn(getFqn());
         interceptor.setAopInstance(getAopInstance());
         return interceptor;
  @@ -77,7 +69,7 @@
   
      public void setInterceptor(Interceptor intcptr)
      {
  -      CacheFieldInterceptor interceptor = (CacheFieldInterceptor) intcptr;
  +      BaseInterceptor interceptor = (BaseInterceptor) intcptr;
         setFqn(interceptor.getFqn());
         setAopInstance(interceptor.getAopInstance());
      }
  @@ -102,13 +94,6 @@
         if (invocation instanceof MethodInvocation)
            return invocation.invokeNext();
   
  -      // Check if CLASS_INTERNAL exists. If not, that means we are done. We need to remove ourself.
  -      // Note that if speed is important, we will need to perform the detach step pro-actively,
  -      // that is, use a listener to listen for the removeObject event.
  -//      if (isPojoDetached(invocation))
  -//      {
  -//         return invocation.invokeNext();  // invoke the in-memory pojo directly
  -//      }
   
         if (invocation instanceof FieldWriteInvocation)
         {
  @@ -122,6 +107,8 @@
               log_.trace("invoke(): field write interception for fqn: " + fqn_ + " and field: " + field);
            }
   
  +         verifyAttached(invocation.getTargetObject());
  +
            // Only if this field is replicatable. static, transient and final are not.
            CachedType fieldType = pCache_.getCachedType(field.getType());
            CachedType parentType = pCache_.getCachedType(field.getDeclaringClass());
  @@ -134,7 +121,6 @@
               }
               else
               {
  -               //cache_.putObject(((Fqn)fqn_.clone()).add(field.getLastElementAsString()), value);
                  pCache_.attach(fqn_, value, field.getName());
               }
            }
  @@ -161,23 +147,14 @@
               }
               else
               {
  -               //result = cache_.getObject(((Fqn)fqn_.clone()).add(field.getLastElementAsString()));
                  result = pCache_.getObject(fqn_, field.getName());
               }
   
  -            // if result is null, we need to make sure the in-memory reference is null
  -            // as well. If it is not, then we know this one is null because it has
  -            // been evicted. Will need to reconstruct it
  -            if (result != null)
  -               return result;
  -            else
  -            {
  -               Object value = invocation.getTargetObject();
  -               if (value == null || field.get(value) == null)   // if both are null, we know this is null as well.
  -                  return null;
  +            // If the result is null, the object might have been detached
  +            if (result == null)
  +               verifyAttached(invocation.getTargetObject());
   
  -               isPojoDetached(invocation);
  -            }
  +            return result;
            }
         }
   
  @@ -211,76 +188,16 @@
      }
   
      /**
  -    * Check if the pojo is detached already. If it is and we still have the cache_ interceptor on
  -    * this pojo, we will go ahead and remove it since it should not be there in the first place.
  -    *
  -    * @param invocation
  +    * Check if the pojo is detached already.
       */
  -   private boolean isPojoDetached(Invocation invocation)
  -   {
  -      boolean detached = false;
  -
  -      if (!CacheApiUtil.exists(cache_, fqn_, PojoInstance.KEY))
  -      {
  -         detached = true;
  -         Object obj = invocation.getTargetObject();
  -         if (!(obj instanceof Advised))
  -            throw new PojoCacheException("Interception on non-advised pojo " + obj.toString());
  -
  -         InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
  -         CacheFieldInterceptor interceptor = (CacheFieldInterceptor) AopUtil.findCacheInterceptor(advisor);
  -         if (interceptor != null)
  -         {
  -            advisor.removeInterceptor(interceptor.getName());
  -            throw new PojoCacheAlreadyDetachedException("pojo: " + obj.getClass() +
  -                                                        " has possibly been detached remotely. Internal id: " + interceptor.getFqn());
  -         }
  -      }
  -
  -      return detached;
  -   }
  -
  -   protected void checkCacheConsistency() throws Exception
  -   {
  -      if (this != cache_.get(fqn_, PojoInstance.KEY))
  -      {
  -         throw new PojoCacheException("Cache inconsistency: Outdated PojoInstance");
  -      }
  -   }
  -
  -   public void beforeSerialization(Object target) throws Exception
  +   private void verifyAttached(Object target)
      {
  +      if (cache_.get(fqn_, PojoInstance.KEY) != null)
  +         return;
   
  -      // fill objects
  -      for (Iterator i = type_.getFields().iterator(); i.hasNext();)
  -      {
  -         Field field = (Field) (((FieldPersistentReference) i.next())).get();
  -         CachedType fieldType = pCache_.getCachedType(field.getType());
  -         Object value = null;
  -         if (fieldType.isImmediate())
  -         {
  -            value = cache_.get(fqn_, field.getName());
  +      String identity = ObjectUtil.identityString(target);
  +      throw new PojoCacheAlreadyDetachedException(identity + " has possibly been detached remotely. Internal id: " + fqn_);
            }
  -         else
  -         {
  -            //		value = removeObject(fqn_+CacheImpl.SEPARATOR+field.getLastElementAsString());
  -            //value = cache_.getObject(((Fqn)fqn_.clone()).add(field.getLastElementAsString()));
  -            value = pCache_.getObject(new Fqn(fqn_, field.getName()));
  -         }
  -         //	    System.out.println("Setting field " + field.getLastElementAsString() + "[" + field.getDeclaringClass() + "] of "+ target.getClass() + " to " + value);
  -         field.set(target, value);
  -      }
  -   }
  -
  -   boolean isChildOf(Fqn parentFqn)
  -   {
  -      return fqn_.isChildOf(parentFqn);
  -   }
  -
  -//   void setFqn(Fqn fqn_)
  -//   {
  -//      this.fqn_ = fqn_;
  -//   }
   
      public Fqn getFqn()
      {
  
  
  
  1.3       +6 -5      JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CachedListInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- CachedListInterceptor.java	23 May 2007 10:28:55 -0000	1.2
  +++ CachedListInterceptor.java	21 Jun 2007 01:33:03 -0000	1.3
  @@ -40,20 +40,21 @@
   
      public CachedListInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, List obj)
      {
  -      this.fqn_ = fqn;
  +      super(cache, fqn);
         methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
         cacheImpl_ = new CachedListImpl(cache, this);
         inMemImpl_ = obj;   // lazy initialization here.
         current_ = cacheImpl_;
      }
   
  -   public CachedListInterceptor()
  +   private CachedListInterceptor(PojoCacheImpl cache, Fqn fqn)
      {
  +      super(cache, fqn);
      }
   
      public Object clone()
      {
  -      CachedListInterceptor interceptor = new CachedListInterceptor();
  +      CachedListInterceptor interceptor = new CachedListInterceptor(cache, fqn);
         interceptor.setFqn(getFqn());
         interceptor.setAopInstance(getAopInstance());
         interceptor.setCurrentCopy(getCurrentCopy());
  @@ -192,8 +193,8 @@
            throw new IllegalStateException("CachedListInterceptor.invoke(). current_ is null.");
   
         return CollectionInterceptorUtil.invoke(invocation,
  +                                              this,
                                                 current_,
  -                                              methodMap_,
  -                                              managedMethods_);
  +                                              methodMap_, managedMethods_);
      }
   }
  
  
  
  1.3       +6 -5      JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CachedMapInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- CachedMapInterceptor.java	23 May 2007 10:28:55 -0000	1.2
  +++ CachedMapInterceptor.java	21 Jun 2007 01:33:03 -0000	1.3
  @@ -35,20 +35,21 @@
   
      public CachedMapInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, Map obj)
      {
  -      this.fqn_ = fqn;
  +      super(cache, fqn);
         methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
         cacheImpl_ = new CachedMapImpl(cache, this);
         inMemImpl_ = obj;
         current_ = cacheImpl_;
      }
   
  -   CachedMapInterceptor()
  +   private CachedMapInterceptor(PojoCacheImpl cache, Fqn fqn)
      {
  +      super(cache, fqn);
      }
   
      public Object clone()
      {
  -      CachedMapInterceptor interceptor = new CachedMapInterceptor();
  +      CachedMapInterceptor interceptor = new CachedMapInterceptor(cache, fqn);
         interceptor.setFqn(getFqn());
         interceptor.setAopInstance(getAopInstance());
         interceptor.setCurrentCopy(getCurrentCopy());
  @@ -175,9 +176,9 @@
            throw new IllegalStateException("CachedMapInterceptor.invoke(). current_ is null.");
   
         return CollectionInterceptorUtil.invoke(invocation,
  +                                              this,
                                                 current_,
  -                                              methodMap_,
  -                                              managedMethods_);
  +                                              methodMap_, managedMethods_);
      }
   
   }
  
  
  
  1.3       +6 -5      JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CachedSetInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- CachedSetInterceptor.java	23 May 2007 10:28:55 -0000	1.2
  +++ CachedSetInterceptor.java	21 Jun 2007 01:33:03 -0000	1.3
  @@ -36,20 +36,21 @@
   
      public CachedSetInterceptor(PojoCacheImpl cache, Fqn fqn, Class clazz, Set obj)
      {
  -      this.fqn_ = fqn;
  +      super(cache, fqn);
         methodMap_ = CollectionInterceptorUtil.getMethodMap(clazz);
         cacheImpl_ = new CachedSetImpl(cache, this);
         inMemImpl_ = obj;
         current_ = cacheImpl_;
      }
   
  -   public CachedSetInterceptor()
  +   private CachedSetInterceptor(PojoCacheImpl cache, Fqn fqn)
      {
  +      super(cache, fqn);
      }
   
      public Object clone()
      {
  -      CachedSetInterceptor interceptor = new CachedSetInterceptor();
  +      CachedSetInterceptor interceptor = new CachedSetInterceptor(cache, fqn);
         interceptor.setFqn(getFqn());
         interceptor.setAopInstance(getAopInstance());
         interceptor.setCurrentCopy(getCurrentCopy());
  @@ -170,8 +171,8 @@
            throw new IllegalStateException("CachedSetInterceptor.invoke(). current_ is null.");
   
         return CollectionInterceptorUtil.invoke(invocation,
  +                                              this,
                                                 current_,
  -                                              methodMap_,
  -                                              managedMethods_);
  +                                              methodMap_, managedMethods_);
      }
   }
  
  
  



More information about the jboss-cvs-commits mailing list