1 45 package org.openejb.core.ivm; 46 47 import java.io.Externalizable ; 48 import java.io.IOException ; 49 import java.io.InvalidObjectException ; 50 import java.io.ObjectInput ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectStreamException ; 53 import java.util.ArrayList ; 54 import java.util.List ; 55 56 import org.openejb.util.FastThreadLocal; 57 58 133 public class IntraVmArtifact implements Externalizable { 134 135 139 private int instanceHandle; 140 141 149 private static FastThreadLocal thread = new FastThreadLocal(); 150 151 154 private static final String NO_MAP_ERROR = "There is no HashMap stored in the thread. This object may have been moved outside the Virtual Machine."; 155 156 159 private static final String NO_ARTIFACT_ERROR = "The artifact this object represents could not be found."; 160 161 167 public IntraVmArtifact(Object obj) { 168 List list = (List )thread.get(); 171 if (list == null) { 172 list = new ArrayList (); 173 thread.set(list); 174 } 175 instanceHandle = list.size(); 176 list.add(obj); 177 } 178 179 184 public IntraVmArtifact() { 185 } 186 187 193 public void writeExternal(ObjectOutput out) throws IOException { 194 out.write(instanceHandle); 195 } 196 197 203 public void readExternal(ObjectInput in) throws IOException { 204 instanceHandle = in.read(); 205 } 206 207 216 private Object readResolve() throws ObjectStreamException { 217 List list = (List ) thread.get(); 218 if (list == null) throw new InvalidObjectException (NO_MAP_ERROR); 219 Object artifact = list.get(instanceHandle); 220 if (artifact == null) throw new InvalidObjectException (NO_ARTIFACT_ERROR+instanceHandle); 221 if(list.size()==instanceHandle+1) { 222 list.clear(); 223 } 224 return artifact; 225 } 226 227 } 228 | Popular Tags |