Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 142   Methods: 5
NCLOC: 83   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PersistentReference.java 27.8% 37.9% 60% 36.5%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    * Copyright 2005, JBoss Inc., and individual contributors as indicated
 4    * by the @authors tag. See the copyright.txt in the distribution for a
 5    * full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22   
 23    package org.jboss.cache.pojo.memory;
 24   
 25    import org.apache.commons.logging.Log;
 26    import org.apache.commons.logging.LogFactory;
 27   
 28    import java.lang.ref.Reference;
 29    import java.lang.ref.SoftReference;
 30    import java.lang.ref.WeakReference;
 31   
 32    /**
 33    * Base class for persistent references.
 34    * Persistent reference is a Weak/Soft reference to a reflection object.
 35    * If the reflection object is garbage collected, the reference is then rebuilt using reflection operations.
 36    *
 37    * @author csuconic
 38    */
 39    public abstract class PersistentReference
 40    {
 41    static final int REFERENCE_WEAK = 1;
 42    public static final int REFERENCE_SOFT = 2;
 43   
 44    private static final Log log = LogFactory.getLog(PersistentReference.class);
 45    private static final boolean isDebug = log.isDebugEnabled();
 46   
 47    private WeakReference classReference;
 48    private Reference referencedObject;
 49    private int referenceType = 0;
 50   
 51    /**
 52    * @param clazz The clazz being used on this object (where we will do reflection operations)
 53    * @param referencedObject The reflection object being used
 54    * @param referenceType if REFERENCE_WEAK will use a WeakReference, and if REFERENCE_SOFT will use a SoftReference for referencedObject
 55    */
 56  4353 public PersistentReference(Class clazz, Object referencedObject, int referenceType)
 57    {
 58  4353 this.referenceType = referenceType;
 59  4353 if (clazz != null)
 60    {
 61  4353 classReference = new WeakReference(clazz);
 62    }
 63  4353 buildReference(referencedObject);
 64    }
 65   
 66    /**
 67    * Checks the reference but doesn't perform rebuild if empty
 68    */
 69  0 Object internalGet()
 70    {
 71  0 if (referencedObject == null)
 72  0 return null;
 73   
 74  0 return referencedObject.get();
 75   
 76   
 77    }
 78   
 79  22841 public Object get()
 80    {
 81  22841 if (referencedObject == null)
 82  0 return null;
 83   
 84  22841 Object returnValue = referencedObject.get();
 85  22841 if (returnValue == null)
 86    {
 87  0 try
 88    {
 89  0 if (isDebug) log.debug("Reference on " + this.getClass().getName() + " being rebuilt");
 90   
 91    // Return ths value straight from the rebuild, to guarantee the value is not destroyed if a GC happens during the rebuild reference
 92  0 return rebuildReference();
 93    }
 94    catch (RuntimeException re)
 95    {
 96  0 throw re;
 97    }
 98    catch (Exception e)
 99    {
 100  0 throw new RuntimeException(e.getMessage(), e);
 101    }
 102    }
 103   
 104  22841 return returnValue;
 105    }
 106   
 107    protected abstract Object rebuildReference() throws Exception;
 108   
 109    /**
 110    * This method should be called from a synchronized block
 111    */
 112  4353 void buildReference(Object obj)
 113    {
 114  4353 if (obj == null)
 115    {
 116  0 referencedObject = null;
 117    }
 118    else
 119    {
 120  4353 if (referenceType == REFERENCE_WEAK)
 121    {
 122  0 referencedObject = new WeakReference(obj);
 123    }
 124    else
 125    {
 126  4353 referencedObject = new SoftReference(obj);
 127    }
 128    }
 129    }
 130   
 131  0 Class getMappedClass()
 132    {
 133  0 if (classReference == null) return null;
 134  0 Class returnClass = (Class) classReference.get();
 135  0 if (returnClass == null)
 136    {
 137  0 throw new RuntimeException("Class was already unloaded");
 138    }
 139  0 return (Class) returnClass;
 140    }
 141   
 142    }