[jboss-cvs] JBossCache/old/src/org/jboss/cache/aop/util ...

Ben Wang bwang at jboss.com
Tue Oct 31 03:01:15 EST 2006


  User: bwang   
  Date: 06/10/31 03:01:15

  Added:       old/src/org/jboss/cache/aop/util    ObjectUtil.java
                        AopUtil.java Null.java
  Log:
  Deprecated files moved to old dir.
  
  Revision  Changes    Path
  1.1      date: 2006/10/31 08:01:15;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/util/ObjectUtil.java
  
  Index: ObjectUtil.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.util;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.Advised;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.CacheException;
  import org.jboss.cache.aop.PojoCache;
  import org.jboss.cache.aop.CachedType;
  import org.jboss.cache.aop.references.FieldPersistentReference;
  
  import java.lang.reflect.Field;
  import java.util.HashSet;
  import java.util.Set;
  import java.util.Iterator;
  import java.util.Collection;
  import java.util.Map;
  
  /**
   * Unitlity methods for pojo object.
   *
   * @author Ben Wang
   */
  public class ObjectUtil
  {
     static Log log=LogFactory.getLog(ObjectUtil.class.getName());
  
     /**
      * Static methos to check if <code>thisObject</code> is reachable from <code>originalObject</code>.
      * @param cache
      * @param originalObject
      * @param thisObject
      * @return
      * @throws CacheException
      */
     public static boolean isReachable(PojoCache cache, Object originalObject, Object thisObject)
        throws CacheException
     {
        HashSet objSet = new HashSet();
  
        return isReachableInner(cache, originalObject, thisObject, objSet);
     }
  
     public static boolean isReachableInner(PojoCache cache, Object originalObject,
                                            Object thisObject, Set objSet)
        throws CacheException
     {
        // Currently we don't support recursive Collection
        if( !(originalObject instanceof Advised) )
           throw new RuntimeException("ObjectUtil.isReachable(): originalObject is not Advised.");
  
        if(log.isTraceEnabled()) {
           log.trace("isReachable(): current object: " + originalObject + " this object: " +thisObject);
        }
  
        if(originalObject.equals(thisObject))
        {
           if(log.isTraceEnabled()) {
              log.trace("isReachable(): object found reachable.");
           }
  
           return true;
        }
  
        if( !objSet.contains(originalObject))
        {
           objSet.add(originalObject);
        } else
        {  // We have been here before so let's return.
           return false;
        }
  
        CachedType type = cache.getCachedType(originalObject.getClass());
        for (Iterator i = type.getFields().iterator(); i.hasNext();) {
           Field field = (Field)(((FieldPersistentReference) i.next())).get();
           Object value = null;
           try {
              value=field.get(originalObject); // Reflection may not work here.
           }
           catch(IllegalAccessException e) {
              throw new CacheException("field access failed", e);
           }
           CachedType fieldType = cache.getCachedType(field.getType());
           if (fieldType.isImmediate()) {
           } else {
              if(value instanceof Map)
              {
                 Set set = ((Map)value).keySet();
                 for(Iterator it=set.iterator(); it.hasNext();)
                 {
                    if(isReachableInner(cache, it.next(), thisObject, objSet))
                       return true;
                 }
  
                 continue;
              } else if (value instanceof Collection)
              {
                 for(Iterator it = ((Collection)value).iterator(); it.hasNext();)
                 {
                    if(isReachableInner(cache, it.next(), thisObject, objSet))
                       return true;
                 }
  
                 continue;
              }
  
              if(!(value instanceof Advised))
                 continue;   // TODO We don't care about Collection now.
  
              if(isReachableInner(cache, value, thisObject, objSet))
                 return true;
           }
        }
  
        return false;
     }
  
     public static String getIndirectFqn(Fqn fqn)
     {
        // TODO Need to generate a unique id here
        // Let's strip off the line separator and use underscoe instead.
        return getIndirectFqn(fqn.toString());
     }
  
     public static String getIndirectFqn(String fqn)
     {
        // TODO Need to generate a unique id here
        // Let's strip off the line separator and use underscoe instead.
        return fqn.replace('/', '_');
     }
  }
  
  
  
  1.1      date: 2006/10/31 08:01:15;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/util/AopUtil.java
  
  Index: AopUtil.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.util;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.aop.Advised;
  import org.jboss.aop.InstanceAdvisor;
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.aop.BaseInterceptor;
  import org.jboss.cache.aop.CacheInterceptor;
  import java.io.Serializable;
  
  /**
   * Unitlity methods for put, get and remove Collection classes object.
   *
   * @author Ben Wang
   */
  public class AopUtil
  {
     static Log log=LogFactory.getLog(AopUtil.class.getName());
  
     /**
      * Find cache interceptor with exact fqn.
      * @param advisor
      * @param fqn
      * @return Interceptor
      */
     static public Interceptor findCacheInterceptor(InstanceAdvisor advisor, Fqn fqn)
     {
        org.jboss.aop.advice.Interceptor[] interceptors = advisor.getInterceptors();
        // Step Check for cross references
        for (int i = 0; i < interceptors.length; i++) {
           Interceptor interceptor = interceptors[i];
           if (interceptor instanceof CacheInterceptor) {
              CacheInterceptor inter = (CacheInterceptor)interceptor;
              if (inter != null && inter.getFqn().equals(fqn))
              {
                 return interceptor;
              }
           }
        }
        return null;
     }
  
     /**
      * Find existing cache interceptor. Since there is supposedly only one cache interceptor per
      * pojo, this call should suffice. In addition, in cases of cross or circular reference,
      * fqn can be different anyway.
      * @param advisor
      * @return Interceptor
      */
     static public Interceptor findCacheInterceptor(InstanceAdvisor advisor)
     {
        // TODO we assume there is only one interceptor now.
        Interceptor[] interceptors = advisor.getInterceptors();
        // Step Check for cross references
        for (int i = 0; i < interceptors.length; i++) {
           Interceptor interceptor = interceptors[i];
           if (interceptor instanceof CacheInterceptor) {
                 return interceptor;
           }
        }
        return null;
     }
  
     /**
      * Find existing Collection interceptor. Since there is supposedly only one Collection interceptor per
      * instance, this call should suffice. In addition, in cases of cross or circular reference,
      * fqn can be different anyway.
      * @param advisor
      * @return Interceptor
      */
     static public Interceptor findCollectionInterceptor(InstanceAdvisor advisor)
     {
        // TODO we assume there is only one interceptor now.
        Interceptor[] interceptors = advisor.getInterceptors();
        // Step Check for cross references
        for (int i = 0; i < interceptors.length; i++) {
           Interceptor interceptor = interceptors[i];
           if (interceptor instanceof BaseInterceptor) {
                 return interceptor;
           }
        }
        return null;
     }
  
     /**
      * Check whether the object type is valid. An object type is valid if it is either: aspectized,
      * Serializable, or primitive type. Otherwise a runtime exception is thrown.
      *
      * @param obj
      */
     public static void checkObjectType(Object obj) {
        if(obj == null) return;
        if( ! (obj instanceof Advised) )
        {
            boolean allowedType = (obj instanceof Serializable) || (obj.getClass().isArray() && obj.getClass().getComponentType().isPrimitive());
  
            if( !allowedType)
            {
                 throw new IllegalArgumentException("PojoCache.putObject(): Object type is neither " +
                    " aspectized nor Serializable nor an array of primitives. Object class name is " +obj.getClass().getName());
            }
        }
     }
  
  
     public static Fqn constructFqn(Fqn baseFqn, Object relative)
     {
        // TODO Don't know why. But this will fail the CachedSetAopTest clear() method since look up is always
        // Null at the last index. But why?
        // TODO also see JBCACHE-282
        return new Fqn(baseFqn, relative.toString());
  
  //      String tmp = baseFqn.toString() +"/" + relative.toString();
  //      return Fqn.fromString(tmp);
  //      Fqn fqn = new Fqn((String)relative);
  //      return new Fqn(baseFqn, fqn);
     }
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:15;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/util/Null.java
  
  Index: Null.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.aop.util;
  
  import java.io.Serializable;
  
  /**
   * Represent null values (based on org.jboss.util.Null)
   *
   * @author Scott Marlow
   */
  public final class Null implements Serializable
  {
     static final long serialVersionUID = -402153636437493134L;
     /** singleton instance of Null. */
     private static final org.jboss.cache.aop.util.Null NULL_OBJECT_MARKER = new org.jboss.cache.aop.util.Null();
  
     // String representation of Null
     private static final String NULL_AS_STRING_REPRESENTATION = "jboss.cache._NULL_";
  
     /** Do not allow public construction. */
     private Null() {}
  
  
     /**
      * Represents null as a special Null object marker.
      * @param aObject
      * @return if aObject is null, return Null.NULL_OBJECT_MARKER otherwise return aObject.
      */
     public final static Object toNullObject( Object aObject )
     {
        if ( aObject == null ) {
           return org.jboss.cache.aop.util.Null.NULL_OBJECT_MARKER;
        }
        else {
           return aObject;
        }
     }
  
     /**
      *  Represents null key as a special string value.
      * @param aObject
      * @return if aObject is null, return Null.NULL_AS_STRING_REPRESENTATION otherwise return aObject.
      */
     public final static Object toNullKeyObject( Object aObject )
     {
        if ( aObject == null ) {
           return org.jboss.cache.aop.util.Null.NULL_AS_STRING_REPRESENTATION;
        }
        else {
           return aObject;
        }
     }
  
     /**
      * If the passed object represents null (instance of Null.NULL_OBJECT_MARKER), will replace with null value.
      * @param aObject
      * @return null if aObject is instance of Null.NULL_OBJECT_MARKER, otherwise return aObject.
      */
     public final static Object toNullValue( Object aObject )
     {
        if ( org.jboss.cache.aop.util.Null.NULL_OBJECT_MARKER.equals( aObject ) ) {
           return null;
        }
        else {
           return aObject;
        }
     }
  
     /**
      * Converts Null string representation back to null value.
      * @param aObject
      * @return null if aObject represents a null, otherwise return aObject.
      */
     public final static Object toNullKeyValue( Object aObject )
     {
        if ( org.jboss.cache.aop.util.Null.NULL_AS_STRING_REPRESENTATION.equals( aObject ) ) {
           return null;
        }
        else {
           return aObject;
        }
     }
  
  
     /**
      * Return a string representation of null value.
      *
      * @return Null.NULL_AS_STRING_REPRESENTATION;
      */
     public String toString() {
        return NULL_AS_STRING_REPRESENTATION;
     }
  
     /**
      * Hashcode of unknown (null) value will be zero.
      *
      * @return  Zero.
      */
     public int hashCode() {
        return 0;
     }
  
     /**
      * Check if the given object is a Null instance or <tt>null</tt>.
      *
      * @param obj  Object to test.
      * @return     True if the given object is a Null instance or <tt>null</tt>.
      */
     public boolean equals(final Object obj) {
        if (obj == this) return true;
        return (obj == null || obj.getClass() == getClass() || NULL_AS_STRING_REPRESENTATION.equals(obj));
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list