1 22 23 package org.jboss.aop.util.reference; 24 25 import java.lang.ref.Reference ; 26 import java.lang.ref.SoftReference ; 27 import java.lang.ref.WeakReference ; 28 29 38 public abstract class PersistentReference 39 { 40 public static final int REFERENCE_WEAK=1; 41 public static final int REFERENCE_SOFT=2; 42 43 private WeakReference classReference; 44 private Reference referencedObject; 45 private int referenceType=0; 46 47 53 public PersistentReference(Class clazz, Object referencedObject, int referenceType) 54 { 55 this.referenceType=referenceType; 56 if (clazz!=null) 57 { 58 classReference = new WeakReference (clazz); 59 } 60 buildReference(referencedObject); 61 } 62 63 64 protected Object internalGet() 65 { 66 if (referencedObject==null) 67 return null; 68 69 return referencedObject.get(); 70 71 72 } 73 74 public Object get() 75 { 76 if (referencedObject==null) 77 return null; 78 79 Object returnValue = referencedObject.get(); 80 if (returnValue==null) 81 { 82 try 83 { 84 return rebuildReference(); 86 } 87 catch (Exception e) 88 { 89 throw new RuntimeException (e.getMessage(),e); 90 } 91 } 92 93 return returnValue; 94 } 95 96 public abstract Object rebuildReference() throws Exception ; 97 public void buildReference(Object obj) 98 { 99 if (obj==null) 100 { 101 referencedObject = null; 102 } 103 else 104 { 105 if (referenceType==REFERENCE_WEAK) 106 { 107 referencedObject = new WeakReference (obj); 108 } 109 else 110 { 111 referencedObject = new SoftReference (obj); 112 } 113 } 114 } 115 116 public Class getMappedClass() 117 { 118 if (classReference==null) return null; 119 Class returnClass = (Class )classReference.get(); 120 if (returnClass==null) 121 { 122 throw new RuntimeException ("Class was already unloaded"); 123 } 124 return (Class )returnClass; 125 } 126 127 } 128 | Popular Tags |