1 29 30 package nextapp.echo2.webcontainer.util; 31 32 import java.io.IOException ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.io.Serializable ; 36 import java.lang.ref.Reference ; 37 import java.lang.ref.ReferenceQueue ; 38 import java.lang.ref.WeakReference ; 39 import java.util.HashMap ; 40 import java.util.HashSet ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 import java.util.Set ; 44 45 import nextapp.echo2.app.RenderIdSupport; 46 47 52 public class IdTable 53 implements Serializable { 54 55 private boolean hasHardReferences = false; 56 private transient Map idToReferenceMap = new HashMap (); 57 private transient ReferenceQueue referenceQueue = new ReferenceQueue (); 58 59 64 public void register(RenderIdSupport object) { 65 purge(); 66 String id = object.getRenderId(); 67 WeakReference weakReference; 68 synchronized(idToReferenceMap) { 69 if (!idToReferenceMap.containsKey(id)) { 70 weakReference = new WeakReference (object, referenceQueue); 71 idToReferenceMap.put(id, weakReference); 72 } 73 } 74 } 75 76 83 public Object getObject(String id) { 84 purge(); 85 WeakReference weakReference; 86 synchronized(idToReferenceMap) { 87 weakReference = (WeakReference ) idToReferenceMap.get(id); 88 } 89 if (weakReference == null) { 90 return null; 91 } 92 Object object = weakReference.get(); 93 return object; 94 } 95 96 100 private void purge() { 101 if (hasHardReferences) { 103 synchronized (idToReferenceMap) { 104 Iterator idIt = idToReferenceMap.keySet().iterator(); 105 while (idIt.hasNext()) { 106 String id = (String ) idIt.next(); 107 Object object = idToReferenceMap.get(id); 108 if (!(object instanceof WeakReference )) { 109 WeakReference weakReference = new WeakReference (object, referenceQueue); 110 idToReferenceMap.put(id, weakReference); 111 } 112 } 113 hasHardReferences = false; 114 } 115 } 116 117 Reference reference = referenceQueue.poll(); 119 if (reference == null) { 120 return; 122 } 123 Set referenceSet = new HashSet (); 124 while (reference != null) { 125 referenceSet.add(reference); 126 reference = referenceQueue.poll(); 127 } 128 129 synchronized (idToReferenceMap) { 130 Iterator idIt = idToReferenceMap.keySet().iterator(); 131 while (idIt.hasNext()) { 132 String id = (String ) idIt.next(); 133 if (referenceSet.contains(idToReferenceMap.get(id))) { 134 idIt.remove(); 135 } 136 } 137 } 138 } 139 140 147 private void readObject(ObjectInputStream in) 148 throws IOException , ClassNotFoundException { 149 in.defaultReadObject(); 150 151 idToReferenceMap = new HashMap (); 152 referenceQueue = new ReferenceQueue (); 153 154 String id = (String ) in.readObject(); 155 if (id != null) { 156 hasHardReferences = true; 158 159 while (id != null) { 161 RenderIdSupport object = (RenderIdSupport) in.readObject(); 162 idToReferenceMap.put(id, object); 163 id = (String ) in.readObject(); 164 } 165 } 166 } 167 168 171 private void writeObject(ObjectOutputStream out) 172 throws IOException { 173 out.defaultWriteObject(); 174 Iterator it = idToReferenceMap.keySet().iterator(); 175 while (it.hasNext()) { 176 String id = (String ) it.next(); 177 out.writeObject(id); 178 Object object = idToReferenceMap.get(id); 179 if (object instanceof WeakReference ) { 180 object = ((WeakReference ) object).get(); 181 } 182 out.writeObject(object); 183 } 184 out.writeObject(null); 186 } 187 } 188 | Popular Tags |