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

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


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

  Added:       old/src/org/jboss/cache/aop/references      
                        ArgumentPersistentReference.java
                        FieldPersistentReference.java
                        MethodPersistentReference.java
                        ConstructorPersistentReference.java
                        PersistentReference.java EmptyReference.java
  Log:
  Deprecated files moved to old dir.
  
  Revision  Changes    Path
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/ArgumentPersistentReference.java
  
  Index: ArgumentPersistentReference.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.cache.aop.references;
  
  import java.lang.ref.WeakReference;
  
  /**
   * Abstract class used where reflection operations with arguments are used (like Methods and Constructors)
   * @author csuconic
   *
   */
  public abstract class ArgumentPersistentReference extends PersistentReference 
  {
  	public ArgumentPersistentReference(Class clazz, Object referencedObject, int referenceType) {
  		super(clazz, referencedObject, referenceType);
  	}
  
  	WeakReference[] arguments;
  	
  	public void setArguments(Class[] parguments)
  	{
  		this.arguments = new WeakReference[parguments.length];
  		for (int i=0;i<arguments.length;i++)
  		{
  			this.arguments[i] = new WeakReference(parguments[i]);
  		}
  	}
  	
  	public Class[] getArguments()
  	{
  		if (arguments==null)
  		{
  			return null;
  		}
  		else
  		{
  			Class argumentsReturn[] = new Class[arguments.length];
  			for (int i=0;i<arguments.length;i++)
  			{
  				argumentsReturn[i] = (Class)arguments[i].get();
  			}
  			return argumentsReturn;
  		}
  	}
  	
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/FieldPersistentReference.java
  
  Index: FieldPersistentReference.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.cache.aop.references;
  
  import java.lang.reflect.Field;
  
  /**
   * Creates a persistentReference for Fields
   * @author csuconic
   */
  public class FieldPersistentReference extends PersistentReference
  {
  	public FieldPersistentReference (Field field,int referenceType)
  	{
  		super(field!=null?field.getDeclaringClass():null,field,referenceType);
  		this.name=field.getName();
  	}
  	String name;
  	
  	public synchronized Object rebuildReference() throws Exception
  	{
  		// A reference to guarantee the value is not being GCed during while the value is being rebuilt
  		Object returnValue=null;
  		if ((returnValue=internalGet())!=null) return returnValue;
  		
  		Field field = getMappedClass().getDeclaredField(name);
  		field.setAccessible(true);
  		buildReference(field);
  		return field;
  	}
  	
  	public Field getField() 
  	{
  		return (Field) get();
  	}
  }
  
  
  
  
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/MethodPersistentReference.java
  
  Index: MethodPersistentReference.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.cache.aop.references;
  
  import java.lang.ref.WeakReference;
  import java.lang.reflect.Method;
  
  /** A reference to a field.
   * In case the reference is released, the reference will be reconstructed*/
  public class MethodPersistentReference extends ArgumentPersistentReference
  {
  	public MethodPersistentReference (Method method, int referenceType)
  	{
  		super(method!=null?method.getDeclaringClass():null,method,referenceType);
  		if (method!=null)
  		{
  			this.name=method.getName();
  			setArguments(method.getParameterTypes());
  		}
  	}
  	String name;
  	
  
  	public synchronized Object rebuildReference() throws Exception
  	{
  		// A reference to guarantee the value is not being GCed during while the value is being rebuilt
  		Object returnValue=null;
  		if ((returnValue=internalGet())!=null) return returnValue;
  		
  		Method aMethod = getMappedClass().getDeclaredMethod(name,getArguments());
  		aMethod.setAccessible(true);
  		buildReference(aMethod);
  		return aMethod;
  	}
  
  	public Method getMethod()
  	{
  		return (Method) get();
  	}
  	
  }
  
  
  
  
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/ConstructorPersistentReference.java
  
  Index: ConstructorPersistentReference.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.cache.aop.references;
  
  import java.lang.reflect.Constructor;
  
  /**
   * This class is not used by JBossSerialization itself, as the constructor used is slightly different (GhostConstructor), but I kept the implementation here as a reference for others.
   * @author Clebert Suconic 
   */
  public class ConstructorPersistentReference extends ArgumentPersistentReference {
  
  	public ConstructorPersistentReference(Class clazz, Object referencedObject, int referenceType) {
  		super(clazz, referencedObject, referenceType);
  		this.setArguments(((Constructor)referencedObject).getParameterTypes());
  	}
  
  	public synchronized Object rebuildReference() throws Exception {
  		// A reference to guarantee the value is not being GCed during while the value is being rebuilt
  		Object returnValue=null;
  		if ((returnValue=internalGet())!=null) return returnValue;
  
  		Constructor constructor = getMappedClass().getConstructor(getArguments());
  		buildReference(constructor);
  		return constructor;
  	}
  
  }
  
  
  
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/PersistentReference.java
  
  Index: PersistentReference.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  
  package org.jboss.cache.aop.references;
  
  import java.lang.ref.Reference;
  import java.lang.ref.SoftReference;
  import java.lang.ref.WeakReference;
  
  import org.apache.log4j.Logger;
  import org.jboss.serial.classmetamodel.ClassMetaData;
  
  /** 
   * Base class for persistent references.
   * Persistent reference is a Weak/Soft reference to a reflection object.
   * If the reflection object is garbage collected, the reference is then rebuilt using reflection operations.
   * @author csuconic
   *
   */
  public abstract class PersistentReference
  {
  	public static final int REFERENCE_WEAK=1;
  	public static final int REFERENCE_SOFT=2;
  	
  	private static final Logger log = Logger.getLogger(ClassMetaData.class);
     	private static final boolean isDebug = log.isDebugEnabled();
  
     	private WeakReference classReference;
     	private Reference referencedObject;
  	private int referenceType=0;
  
     	/**
     	 * 
     	 * @param clazz The clazz being used on this object (where we will do reflection operations)
     	 * @param referencedObject  The reflection object being used
     	 * @param referenceType if REFERENCE_WEAK will use a WeakReference, and if REFERENCE_SOFT will use a SoftReference for referencedObject
     	 */
     	public PersistentReference(Class clazz, Object referencedObject, int referenceType)
  	{
     		this.referenceType=referenceType;
  		if (clazz!=null)
  		{
  			classReference = new WeakReference(clazz);
  		}
  		buildReference(referencedObject);
  	}
  	
  	/** Checks the reference but doesn't perform rebuild if empty */
  	protected Object internalGet()
  	{
  		if (referencedObject==null)
  			return null;
  
  		return referencedObject.get();
  		
  		    		
  	}
  	
  	public Object get()
  	{
  		if (referencedObject==null)
  			return null;
  
  		Object returnValue = referencedObject.get();
  		if (returnValue==null)
  		{
  			try
  			{
  				if (isDebug) log.debug("Reference on " + this.getClass().getName() + " being rebuilt");
  				
  				// Return ths value straight from the rebuild, to guarantee the value is not destroyed if a GC happens during the rebuild reference
  				return rebuildReference();
  			}
  			catch (Exception e)
  			{
  				throw new RuntimeException(e.getMessage(),e);
  			}
  		}
  		
  		return returnValue;
  	}
  	
  	public abstract Object rebuildReference() throws Exception;
  	
  	/** This method should be called from a synchronized block */
  	protected void buildReference(Object obj)
  	{
  		if (obj==null)
  		{
  			referencedObject = null;
  		}
  		else
  		{
  			if (referenceType==REFERENCE_WEAK)
  			{
  				referencedObject = new WeakReference(obj);
  			}
  			else
  			{
  				referencedObject = new SoftReference(obj);
  			}
  		}
  	}
  	
  	public Class getMappedClass()
  	{
  		if (classReference==null) return null;
  		Class returnClass = (Class)classReference.get();
  		if (returnClass==null)
  		{
  			throw new RuntimeException("Class was already unloaded");
  		}
  		return (Class)returnClass;
  	}
  	
  }
  
  
  
  1.1      date: 2006/10/31 08:01:13;  author: bwang;  state: Exp;JBossCache/old/src/org/jboss/cache/aop/references/EmptyReference.java
  
  Index: EmptyReference.java
  ===================================================================
  package org.jboss.cache.aop.references;
  
  public class EmptyReference extends PersistentReference
  {
  	
  	public EmptyReference() {
  		super(null, null,REFERENCE_WEAK);
  	}
  
  	public Object rebuildReference() throws Exception 
  	{
  		return null;
  	}
  	
  }
  
  
  
  



More information about the jboss-cvs-commits mailing list