KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > reference > PersistentReference


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.aop.util.reference;
24
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.SoftReference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28
29 /**
30  * Copied from org.jboss.serial.references;
31  *
32  * Base class for persistent references.
33  * Persistent reference is a Weak/Soft reference to a reflection object.
34  * If the reflection object is garbage collected, the reference is then rebuilt using reflection operations.
35  * @author csuconic
36  *
37  */

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 JavaDoc classReference;
44     private Reference JavaDoc referencedObject;
45     private int referenceType=0;
46
47     /**
48      *
49      * @param clazz The clazz being used on this object (where we will do reflection operations)
50      * @param referencedObject The reflection object being used
51      * @param referenceType if REFERENCE_WEAK will use a WeakReference, and if REFERENCE_SOFT will use a SoftReference for referencedObject
52      */

53     public PersistentReference(Class JavaDoc clazz, Object JavaDoc referencedObject, int referenceType)
54     {
55         this.referenceType=referenceType;
56         if (clazz!=null)
57         {
58             classReference = new WeakReference JavaDoc(clazz);
59         }
60         buildReference(referencedObject);
61     }
62     
63     /** Checks the reference but doesn't perform rebuild if empty */
64     protected Object JavaDoc internalGet()
65     {
66         if (referencedObject==null)
67             return null;
68
69         return referencedObject.get();
70         
71                     
72     }
73     
74     public Object JavaDoc get()
75     {
76         if (referencedObject==null)
77             return null;
78
79         Object JavaDoc returnValue = referencedObject.get();
80         if (returnValue==null)
81         {
82             try
83             {
84                 // Return ths value straight from the rebuild, to guarantee the value is not destroyed if a GC happens during the rebuild reference
85
return rebuildReference();
86             }
87             catch (Exception JavaDoc e)
88             {
89                 throw new RuntimeException JavaDoc(e.getMessage(),e);
90             }
91         }
92         
93         return returnValue;
94     }
95     
96     public abstract Object JavaDoc rebuildReference() throws Exception JavaDoc;
97     public void buildReference(Object JavaDoc obj)
98     {
99         if (obj==null)
100         {
101             referencedObject = null;
102         }
103         else
104         {
105             if (referenceType==REFERENCE_WEAK)
106             {
107                 referencedObject = new WeakReference JavaDoc(obj);
108             }
109             else
110             {
111                 referencedObject = new SoftReference JavaDoc(obj);
112             }
113         }
114     }
115     
116     public Class JavaDoc getMappedClass()
117     {
118         if (classReference==null) return null;
119         Class JavaDoc returnClass = (Class JavaDoc)classReference.get();
120         if (returnClass==null)
121         {
122             throw new RuntimeException JavaDoc("Class was already unloaded");
123         }
124         return (Class JavaDoc)returnClass;
125     }
126     
127 }
128
Popular Tags