[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/memory ...
Ben Wang
bwang at jboss.com
Sat Jan 13 10:55:07 EST 2007
User: bwang
Date: 07/01/13 10:55:07
Added: src/org/jboss/cache/pojo/memory
ArgumentPersistentReference.java
EmptyReference.java FieldPersistentReference.java
MethodPersistentReference.java
ConstructorPersistentReference.java
PersistentReference.java
Log:
JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
Revision Changes Path
1.1 date: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/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.pojo.memory;
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
{
ArgumentPersistentReference(Class clazz, Object referencedObject, int referenceType)
{
super(clazz, referencedObject, referenceType);
}
private WeakReference[] arguments;
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]);
}
}
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: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/EmptyReference.java
Index: EmptyReference.java
===================================================================
package org.jboss.cache.pojo.memory;
public class EmptyReference extends PersistentReference
{
public EmptyReference()
{
super(null, null, REFERENCE_WEAK);
}
public Object rebuildReference() throws Exception
{
return null;
}
}
1.1 date: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/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.pojo.memory;
import java.lang.reflect.Field;
/**
* Creates a persistentReference for Fields
*
* @author csuconic
*/
public class FieldPersistentReference extends PersistentReference
{
private String name;
public FieldPersistentReference(Field field, int referenceType)
{
super(field != null ? field.getDeclaringClass() : null, field, referenceType);
if (field != null)
this.name = field.getName();
}
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: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/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.pojo.memory;
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());
}
}
private 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: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/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.pojo.memory;
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: 2007/01/13 15:55:07; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/memory/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.pojo.memory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.serial.classmetamodel.ClassMetaData;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
/**
* 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
{
static final int REFERENCE_WEAK = 1;
public static final int REFERENCE_SOFT = 2;
private static final Log log = LogFactory.getLog(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
*/
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;
}
protected abstract Object rebuildReference() throws Exception;
/**
* This method should be called from a synchronized block
*/
void buildReference(Object obj)
{
if (obj == null)
{
referencedObject = null;
} else
{
if (referenceType == REFERENCE_WEAK)
{
referencedObject = new WeakReference(obj);
} else
{
referencedObject = new SoftReference(obj);
}
}
}
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;
}
}
More information about the jboss-cvs-commits
mailing list