1 4 package com.tc.objectserver.managedobject; 5 6 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 7 8 import com.tc.exception.TCRuntimeException; 9 import com.tc.io.serializer.api.StringIndex; 10 import com.tc.object.LiteralValues; 11 import com.tc.object.ObjectID; 12 import com.tc.object.dna.api.DNACursor; 13 import com.tc.object.loaders.Namespace; 14 import com.tc.objectserver.core.api.ManagedObjectState; 15 import com.tc.objectserver.persistence.api.PersistentCollectionFactory; 16 import com.tc.objectserver.persistence.api.Persistor; 17 import com.tc.util.Assert; 18 19 import java.io.IOException ; 20 import java.io.ObjectInput ; 21 import java.util.Map ; 22 23 26 public class ManagedObjectStateFactory { 27 28 private static final LiteralValues literalValues = new LiteralValues(); 29 private static final Map classNameToStateMap = new ConcurrentHashMap(); 30 private final ManagedObjectChangeListenerProvider listenerProvider; 31 private final StringIndex stringIndex; 32 private final PhysicalManagedObjectStateFactory physicalMOFactory; 33 34 38 private static volatile ManagedObjectStateFactory singleton; 39 40 private static boolean disableAssertions = false; 42 43 private final PersistentCollectionFactory persistentCollectionFactory; 44 45 46 static { 47 classNameToStateMap.put(java.util.IdentityHashMap .class.getName(), new Byte (ManagedObjectState.MAP_TYPE)); 48 classNameToStateMap.put(java.util.Hashtable .class.getName(), new Byte (ManagedObjectState.PARTIAL_MAP_TYPE)); 49 classNameToStateMap.put(java.util.Properties .class.getName(), new Byte (ManagedObjectState.PARTIAL_MAP_TYPE)); 50 classNameToStateMap.put(gnu.trove.THashMap.class.getName(), new Byte (ManagedObjectState.MAP_TYPE)); 51 classNameToStateMap.put(java.util.HashMap .class.getName(), new Byte (ManagedObjectState.PARTIAL_MAP_TYPE)); 52 classNameToStateMap 53 .put(java.util.Collections.EMPTY_MAP.getClass().getName(), new Byte (ManagedObjectState.MAP_TYPE)); 54 classNameToStateMap.put(java.util.LinkedHashMap .class.getName(), new Byte (ManagedObjectState.LINKED_HASHMAP_TYPE)); 55 classNameToStateMap.put(java.util.TreeMap .class.getName(), new Byte (ManagedObjectState.TREE_MAP_TYPE)); 56 classNameToStateMap.put(gnu.trove.THashSet.class.getName(), new Byte (ManagedObjectState.SET_TYPE)); 57 classNameToStateMap.put(java.util.HashSet .class.getName(), new Byte (ManagedObjectState.SET_TYPE)); 58 classNameToStateMap.put(java.util.LinkedHashSet .class.getName(), new Byte (ManagedObjectState.SET_TYPE)); 59 classNameToStateMap 60 .put(java.util.Collections.EMPTY_SET.getClass().getName(), new Byte (ManagedObjectState.SET_TYPE)); 61 classNameToStateMap.put(java.util.TreeSet .class.getName(), new Byte (ManagedObjectState.TREE_SET_TYPE)); 62 classNameToStateMap.put(java.util.LinkedList .class.getName(), new Byte (ManagedObjectState.LIST_TYPE)); 63 classNameToStateMap.put(java.util.ArrayList .class.getName(), new Byte (ManagedObjectState.LIST_TYPE)); 64 classNameToStateMap.put(java.util.Vector .class.getName(), new Byte (ManagedObjectState.LIST_TYPE)); 65 classNameToStateMap.put(java.util.Stack .class.getName(), new Byte (ManagedObjectState.LIST_TYPE)); 66 classNameToStateMap.put(java.util.Collections.EMPTY_LIST.getClass().getName(), 67 new Byte (ManagedObjectState.LIST_TYPE)); 68 classNameToStateMap.put(java.util.Date .class.getName(), new Byte (ManagedObjectState.DATE_TYPE)); 69 classNameToStateMap.put(java.sql.Date .class.getName(), new Byte (ManagedObjectState.DATE_TYPE)); 70 classNameToStateMap.put(java.sql.Time .class.getName(), new Byte (ManagedObjectState.DATE_TYPE)); 71 classNameToStateMap.put(java.sql.Timestamp .class.getName(), new Byte (ManagedObjectState.DATE_TYPE)); 72 classNameToStateMap.put("java.util.concurrent.LinkedBlockingQueue", new Byte (ManagedObjectState.QUEUE_TYPE)); 74 classNameToStateMap.put("java.util.concurrent.ConcurrentHashMap", 75 new Byte (ManagedObjectState.CONCURRENT_HASHMAP_TYPE)); 76 } 77 78 private ManagedObjectStateFactory(ManagedObjectChangeListenerProvider listenerProvider, StringIndex stringIndex, 79 PhysicalManagedObjectStateFactory physicalMOFactory, 80 PersistentCollectionFactory factory) { 81 this.listenerProvider = listenerProvider; 82 this.stringIndex = stringIndex; 83 this.physicalMOFactory = physicalMOFactory; 84 this.persistentCollectionFactory = factory; 85 } 86 87 90 public static synchronized ManagedObjectStateFactory createInstance( 91 ManagedObjectChangeListenerProvider listenerProvider, 92 Persistor persistor) { 93 if (singleton != null && !disableAssertions) { 94 throw new AssertionError ("This class is singleton. It is not to be instanciated more than once. " + singleton); 96 } 97 singleton = new ManagedObjectStateFactory(listenerProvider, persistor.getStringIndex(), 98 new PhysicalManagedObjectStateFactory(persistor.getClassPersistor()), 99 persistor.getPersistentCollectionFactory()); 100 return singleton; 101 } 102 103 public static synchronized void disableSingleton(boolean b) { 105 disableAssertions = b; 106 } 107 108 public static synchronized void setInstance(ManagedObjectStateFactory factory) { 110 Assert.assertNotNull(factory); 111 singleton = factory; 112 } 113 114 118 public static ManagedObjectStateFactory getInstance() { 119 Assert.assertNotNull(singleton); 120 return singleton; 121 } 122 123 public StringIndex getStringIndex() { 124 return stringIndex; 125 } 126 127 public ManagedObjectChangeListener getListener() { 128 return listenerProvider.getListener(); 129 } 130 131 public ManagedObjectState createState(ObjectID oid, ObjectID parentID, String className, String loaderDesc, 132 DNACursor cursor) { 133 byte type = getStateObjectTypeFor(className); 134 135 if (type == ManagedObjectState.LITERAL_TYPE) { return new LiteralTypesManagedObjectState(); } 136 137 final long classID = getClassID(className, loaderDesc); 138 139 if (type == ManagedObjectState.PHYSICAL_TYPE) { return physicalMOFactory.create(classID, parentID, className, 140 loaderDesc, cursor); } 141 switch (type) { 142 case ManagedObjectState.ARRAY_TYPE: 143 return new ArrayManagedObjectState(classID); 144 case ManagedObjectState.MAP_TYPE: 145 return new MapManagedObjectState(classID, persistentCollectionFactory.createPersistentMap(oid)); 146 case ManagedObjectState.PARTIAL_MAP_TYPE: 147 return new PartialMapManagedObjectState(classID, persistentCollectionFactory.createPersistentMap(oid)); 148 case ManagedObjectState.LINKED_HASHMAP_TYPE: 149 return new LinkedHashMapManagedObjectState(classID); 150 case ManagedObjectState.TREE_MAP_TYPE: 151 return new TreeMapManagedObjectState(classID); 152 case ManagedObjectState.SET_TYPE: 153 return new SetManagedObjectState(classID); 154 case ManagedObjectState.TREE_SET_TYPE: 155 return new TreeSetManagedObjectState(classID); 156 case ManagedObjectState.LIST_TYPE: 157 return new ListManagedObjectState(classID); 158 case ManagedObjectState.QUEUE_TYPE: 159 return new QueueManagedObjectState(classID); 160 case ManagedObjectState.DATE_TYPE: 161 return new DateManagedObjectState(classID); 162 case ManagedObjectState.CONCURRENT_HASHMAP_TYPE: 163 return new ConcurrentHashMapManagedObjectState(classID); 164 } 165 throw new AssertionError ("Type : " + type + " is unknown !"); 167 } 168 169 private long getClassID(String className, String loaderDesc) { 170 return getStringIndex().getOrCreateIndexFor(loaderDesc + Namespace.getClassNameAndLoaderSeparator() + className); 171 } 172 173 public String getClassName(long classID) { 174 String s = null; 175 try { 176 String separator = Namespace.getClassNameAndLoaderSeparator(); 177 s = getStringIndex().getStringFor(classID); 178 return s.substring(s.indexOf(separator) + separator.length()); 179 } catch (Exception ex) { 180 throw new AssertionError ("loaderDesc://:ClassName string for classId " + classID + " not in the right format : " 181 + s); 182 } 183 } 184 185 public String getLoaderDescription(long classID) { 186 String s = null; 187 try { 188 String separator = Namespace.getClassNameAndLoaderSeparator(); 189 s = getStringIndex().getStringFor(classID); 190 return s.substring(0, s.indexOf(separator)); 191 } catch (Exception ex) { 192 throw new AssertionError ("loaderDesc://:ClassName string for classId " + classID + " not in the right format : " 193 + s); 194 } 195 } 196 197 private byte getStateObjectTypeFor(String className) { 198 String logicalExtendingClassName = Namespace.parseLogicalNameIfNeceesary(className); 199 if (logicalExtendingClassName != null) { 200 Byte t = (Byte ) classNameToStateMap.get(logicalExtendingClassName); 201 if (t != null) { return t.byteValue(); } 202 203 className = Namespace.parseClassNameIfNecessary(className); 204 } 205 206 if (className.startsWith("[")) { return ManagedObjectState.ARRAY_TYPE; } 207 Byte type = (Byte ) classNameToStateMap.get(className); 208 if (type != null) { return type.byteValue(); } 209 if (literalValues.isLiteral(className)) { return ManagedObjectState.LITERAL_TYPE; } 210 return ManagedObjectState.PHYSICAL_TYPE; 211 } 212 213 public PhysicalManagedObjectState createPhysicalState(ObjectID parentID, int classId) throws ClassNotFoundException { 214 return physicalMOFactory.create(parentID, classId); 215 } 216 217 public ManagedObjectState readManagedObjectStateFrom(ObjectInput in, byte type) { 218 try { 219 switch (type) { 220 case ManagedObjectState.PHYSICAL_TYPE: 221 return PhysicalManagedObjectState.readFrom(in); 222 case ManagedObjectState.MAP_TYPE: 223 return MapManagedObjectState.readFrom(in); 224 case ManagedObjectState.PARTIAL_MAP_TYPE: 225 return PartialMapManagedObjectState.readFrom(in); 226 case ManagedObjectState.LINKED_HASHMAP_TYPE: 227 return LinkedHashMapManagedObjectState.readFrom(in); 228 case ManagedObjectState.ARRAY_TYPE: 229 return ArrayManagedObjectState.readFrom(in); 230 case ManagedObjectState.DATE_TYPE: 231 return DateManagedObjectState.readFrom(in); 232 case ManagedObjectState.LITERAL_TYPE: 233 return LiteralTypesManagedObjectState.readFrom(in); 234 case ManagedObjectState.LIST_TYPE: 235 return ListManagedObjectState.readFrom(in); 236 case ManagedObjectState.SET_TYPE: 237 return SetManagedObjectState.readFrom(in); 238 case ManagedObjectState.TREE_SET_TYPE: 239 return TreeSetManagedObjectState.readFrom(in); 240 case ManagedObjectState.TREE_MAP_TYPE: 241 return TreeMapManagedObjectState.readFrom(in); 242 case ManagedObjectState.CONCURRENT_HASHMAP_TYPE: 243 return ConcurrentHashMapManagedObjectState.readFrom(in); 244 case ManagedObjectState.QUEUE_TYPE: 245 return QueueManagedObjectState.readFrom(in); 246 default: 247 throw new AssertionError ("Unknown type : " + type + " : Dont know how to deserialize this type !"); 248 } 249 } catch (IOException e) { 250 throw new TCRuntimeException(e); 251 } catch (ClassNotFoundException e) { 252 throw new TCRuntimeException(e); 253 } 254 } 255 256 public ManagedObjectState recreateState(ObjectID id, ObjectID pid, String className, String loaderDesc, 257 DNACursor cursor, ManagedObjectState oldState) { 258 Assert.assertEquals(ManagedObjectState.PHYSICAL_TYPE, oldState.getType()); 259 final long classID = getClassID(className, loaderDesc); 260 return physicalMOFactory.recreate(classID, pid, className, loaderDesc, cursor, 261 (PhysicalManagedObjectState) oldState); 262 } 263 } 264 | Popular Tags |