KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > ManagedObjectStateFactory


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
20 import java.io.ObjectInput JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * Creates state for managed objects
25  */

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   /**
35    * I know singletons are BAD, but this way we save about 16 bytes for every shared object we store in the server and
36    * that is huge ! So I can compromise here.
37    */

38   private static volatile ManagedObjectStateFactory singleton;
39
40   // this is present for tests
41
private static boolean disableAssertions = false;
42
43   private final PersistentCollectionFactory persistentCollectionFactory;
44
45
46   static {
47     classNameToStateMap.put(java.util.IdentityHashMap JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.MAP_TYPE));
48     classNameToStateMap.put(java.util.Hashtable JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.PARTIAL_MAP_TYPE));
49     classNameToStateMap.put(java.util.Properties JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.PARTIAL_MAP_TYPE));
50     classNameToStateMap.put(gnu.trove.THashMap.class.getName(), new Byte JavaDoc(ManagedObjectState.MAP_TYPE));
51     classNameToStateMap.put(java.util.HashMap JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.PARTIAL_MAP_TYPE));
52     classNameToStateMap
53         .put(java.util.Collections.EMPTY_MAP.getClass().getName(), new Byte JavaDoc(ManagedObjectState.MAP_TYPE));
54     classNameToStateMap.put(java.util.LinkedHashMap JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.LINKED_HASHMAP_TYPE));
55     classNameToStateMap.put(java.util.TreeMap JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.TREE_MAP_TYPE));
56     classNameToStateMap.put(gnu.trove.THashSet.class.getName(), new Byte JavaDoc(ManagedObjectState.SET_TYPE));
57     classNameToStateMap.put(java.util.HashSet JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.SET_TYPE));
58     classNameToStateMap.put(java.util.LinkedHashSet JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.SET_TYPE));
59     classNameToStateMap
60         .put(java.util.Collections.EMPTY_SET.getClass().getName(), new Byte JavaDoc(ManagedObjectState.SET_TYPE));
61     classNameToStateMap.put(java.util.TreeSet JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.TREE_SET_TYPE));
62     classNameToStateMap.put(java.util.LinkedList JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.LIST_TYPE));
63     classNameToStateMap.put(java.util.ArrayList JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.LIST_TYPE));
64     classNameToStateMap.put(java.util.Vector JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.LIST_TYPE));
65     classNameToStateMap.put(java.util.Stack JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.LIST_TYPE));
66     classNameToStateMap.put(java.util.Collections.EMPTY_LIST.getClass().getName(),
67                             new Byte JavaDoc(ManagedObjectState.LIST_TYPE));
68     classNameToStateMap.put(java.util.Date JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.DATE_TYPE));
69     classNameToStateMap.put(java.sql.Date JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.DATE_TYPE));
70     classNameToStateMap.put(java.sql.Time JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.DATE_TYPE));
71     classNameToStateMap.put(java.sql.Timestamp JavaDoc.class.getName(), new Byte JavaDoc(ManagedObjectState.DATE_TYPE));
72     // These 1.5 classes needs to be compiled in 1.4 !!
73
classNameToStateMap.put("java.util.concurrent.LinkedBlockingQueue", new Byte JavaDoc(ManagedObjectState.QUEUE_TYPE));
74     classNameToStateMap.put("java.util.concurrent.ConcurrentHashMap",
75                             new Byte JavaDoc(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   /*
88    * @see comments above
89    */

90   public static synchronized ManagedObjectStateFactory createInstance(
91                                                                       ManagedObjectChangeListenerProvider listenerProvider,
92                                                                       Persistor persistor) {
93     if (singleton != null && !disableAssertions) {
94       // not good !!
95
throw new AssertionError JavaDoc("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   // This is provided only for testing
104
public static synchronized void disableSingleton(boolean b) {
105     disableAssertions = b;
106   }
107
108   // This is provided only for testing
109
public static synchronized void setInstance(ManagedObjectStateFactory factory) {
110     Assert.assertNotNull(factory);
111     singleton = factory;
112   }
113
114   /**
115    * This method is not synchronized as the creation and access happens sequencially and this is a costly method to
116    * synchronize and singleton is a volatile variable
117    */

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 JavaDoc className, String JavaDoc 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     // Unreachable
166
throw new AssertionError JavaDoc("Type : " + type + " is unknown !");
167   }
168
169   private long getClassID(String JavaDoc className, String JavaDoc loaderDesc) {
170     return getStringIndex().getOrCreateIndexFor(loaderDesc + Namespace.getClassNameAndLoaderSeparator() + className);
171   }
172
173   public String JavaDoc getClassName(long classID) {
174     String JavaDoc s = null;
175     try {
176       String JavaDoc separator = Namespace.getClassNameAndLoaderSeparator();
177       s = getStringIndex().getStringFor(classID);
178       return s.substring(s.indexOf(separator) + separator.length());
179     } catch (Exception JavaDoc ex) {
180       throw new AssertionError JavaDoc("loaderDesc://:ClassName string for classId " + classID + " not in the right format : "
181                                + s);
182     }
183   }
184
185   public String JavaDoc getLoaderDescription(long classID) {
186     String JavaDoc s = null;
187     try {
188       String JavaDoc separator = Namespace.getClassNameAndLoaderSeparator();
189       s = getStringIndex().getStringFor(classID);
190       return s.substring(0, s.indexOf(separator));
191     } catch (Exception JavaDoc ex) {
192       throw new AssertionError JavaDoc("loaderDesc://:ClassName string for classId " + classID + " not in the right format : "
193                                + s);
194     }
195   }
196
197   private byte getStateObjectTypeFor(String JavaDoc className) {
198     String JavaDoc logicalExtendingClassName = Namespace.parseLogicalNameIfNeceesary(className);
199     if (logicalExtendingClassName != null) {
200       Byte JavaDoc t = (Byte JavaDoc) 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 JavaDoc type = (Byte JavaDoc) 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 JavaDoc {
214     return physicalMOFactory.create(parentID, classId);
215   }
216
217   public ManagedObjectState readManagedObjectStateFrom(ObjectInput JavaDoc 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 JavaDoc("Unknown type : " + type + " : Dont know how to deserialize this type !");
248       }
249     } catch (IOException JavaDoc e) {
250       throw new TCRuntimeException(e);
251     } catch (ClassNotFoundException JavaDoc e) {
252       throw new TCRuntimeException(e);
253     }
254   }
255
256   public ManagedObjectState recreateState(ObjectID id, ObjectID pid, String JavaDoc className, String JavaDoc 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