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

Ben Wang bwang at jboss.com
Tue Nov 14 07:57:54 EST 2006


  User: bwang   
  Date: 06/11/14 07:57:54

  Added:       src-50/org/jboss/cache/pojo/interceptors 
                        StaticFieldInterceptor.java
  Log:
  upd
  
  Revision  Changes    Path
  1.1      date: 2006/11/14 12:57:54;  author: bwang;  state: Exp;JBossCache/src-50/org/jboss/cache/pojo/interceptors/StaticFieldInterceptor.java
  
  Index: StaticFieldInterceptor.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.pojo.interceptors;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.Advisor;
  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.Fqn;
  import org.jboss.cache.CacheSPI;
  import org.jboss.cache.pojo.CachedType;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.InternalConstant;
  import org.jboss.cache.pojo.PojoCacheException;
  import org.jboss.cache.pojo.impl.PojoCacheImpl;
  
  import java.lang.reflect.Field;
  
  /**
   * interceptor to intercept for static field replication.
   *
   * @author Ben Wang
   */
  
  public class StaticFieldInterceptor implements Interceptor
  {
     private final Log log_ = LogFactory.getLog(StaticFieldInterceptor.class);
     private CacheSPI cache_;
     private PojoCacheImpl pCache_;
     private Fqn fqn_;
     private String name_;
     private String key_;
  
     public StaticFieldInterceptor()
     {
     }
  
     public String getName()
     {
        if (name_ == null)
        {
           this.name_ = "StaticFieldInterceptor on [" + fqn_ + "]";
        }
        return name_;
     }
  
     public Object invoke(Invocation invocation) throws Throwable
     {
        // Kind of ad hoc now. MethodInvocation should not invoke this.
        if(invocation instanceof MethodInvocation)
           return invocation.invokeNext();
  
        needInit(((FieldInvocation)invocation).getField());
        if (invocation instanceof FieldWriteInvocation)
        {
           FieldInvocation fieldInvocation =
                   (FieldInvocation) invocation;
  
           Advisor advisor = fieldInvocation.getAdvisor();
           Field field = fieldInvocation.getField();
           if(log_.isTraceEnabled())
           {
              log_.trace("invoke(): field write interception for fqn: " +fqn_ + " and field: " +field);
           }
  
           // Only if this field is replicatable. static, transient and final are not.
           Object value = ((FieldWriteInvocation) fieldInvocation).getValue();
  
           cache_.put(fqn_, key_, value);
           Object obj = fieldInvocation.getTargetObject();
        } else if (invocation instanceof FieldReadInvocation)
        {
           FieldInvocation fieldInvocation =
                   (FieldInvocation) invocation;
           Field field = fieldInvocation.getField();
           Advisor advisor = fieldInvocation.getAdvisor();
           return cache_.get(fqn_, key_);
        }
  
        return invocation.invokeNext();
  
     }
  
     private void needInit(Field field)
     {
        if(pCache_ == null)
        {
           String cn = field.getDeclaringClass().getName();
           fqn_ = Fqn.fromString(InternalConstant.JBOSS_INTERNAL_STATIC + "/" + cn);
           key_ = field.getName();
        }
     }
  
     boolean isChildOf(Fqn parentFqn)
     {
        return fqn_.isChildOf(parentFqn);
     }
  
     public Fqn getFqn()
     {
        return fqn_;
     }
  
     public void setFqn(Fqn fqn)
     {
        this.fqn_ = fqn;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list