001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.managedobject;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.SerializationUtil;
008: import com.tc.object.dna.api.DNACursor;
009: import com.tc.object.dna.api.DNAWriter;
010: import com.tc.object.dna.api.LogicalAction;
011: import com.tc.object.dna.api.PhysicalAction;
012: import com.tc.objectserver.mgmt.ManagedObjectFacade;
013: import com.tc.util.Assert;
014:
015: import java.io.IOException;
016: import java.io.ObjectInput;
017: import java.io.ObjectOutput;
018: import java.util.Iterator;
019: import java.util.LinkedHashMap;
020: import java.util.Map.Entry;
021:
022: /**
023: * state for maps
024: */
025: public class LinkedHashMapManagedObjectState extends
026: PartialMapManagedObjectState {
027: private static final String ACCESS_ORDER_FIELDNAME = "java.util.LinkedHashMap.accessOrder";
028: private boolean accessOrder = false;
029:
030: // TODO:: Come back and make this partial too
031: LinkedHashMapManagedObjectState(long classID) {
032: super (classID, new LinkedHashMap(1));
033: }
034:
035: protected LinkedHashMapManagedObjectState(ObjectInput in)
036: throws IOException {
037: super (in);
038: }
039:
040: public void apply(ObjectID objectID, DNACursor cursor,
041: BackReferences includeIDs) throws IOException {
042: if (!cursor.next()) {
043: return;
044: }
045: Object action = cursor.getAction();
046: if (action instanceof PhysicalAction) {
047: PhysicalAction physicalAction = (PhysicalAction) action;
048: Assert.assertEquals(ACCESS_ORDER_FIELDNAME, physicalAction
049: .getFieldName());
050: setAccessOrder(physicalAction.getObject());
051: } else {
052: LogicalAction logicalAction = (LogicalAction) action;
053: int method = logicalAction.getMethod();
054: Object[] params = logicalAction.getParameters();
055: applyMethod(objectID, includeIDs, method, params);
056: }
057:
058: while (cursor.next()) {
059: LogicalAction logicalAction = cursor.getLogicalAction();
060: int method = logicalAction.getMethod();
061: Object[] params = logicalAction.getParameters();
062: applyMethod(objectID, includeIDs, method, params);
063: }
064: }
065:
066: protected void applyMethod(ObjectID objectID,
067: BackReferences includeIDS, int method, Object[] params) {
068: switch (method) {
069: case SerializationUtil.GET:
070: ((LinkedHashMap) references).get(params[0]);
071: break;
072: default:
073: super .applyMethod(objectID, includeIDS, method, params);
074: }
075: }
076:
077: private void setAccessOrder(Object accessOrderObject) {
078: try {
079: Assert.assertTrue(accessOrderObject instanceof Boolean);
080: accessOrder = ((Boolean) accessOrderObject).booleanValue();
081: references = new LinkedHashMap(1, 0.75f, accessOrder);
082: } catch (Exception e) {
083: throw new RuntimeException(e);
084: }
085: }
086:
087: public void dehydrate(ObjectID objectID, DNAWriter writer) {
088: writer.addPhysicalAction(ACCESS_ORDER_FIELDNAME, new Boolean(
089: accessOrder));
090: super .dehydrate(objectID, writer);
091: }
092:
093: // TODO: The Facade does not include the access order.
094: public ManagedObjectFacade createFacade(ObjectID objectID,
095: String className, int limit) {
096: return super .createFacade(objectID, className, limit);
097: }
098:
099: public byte getType() {
100: return LINKED_HASHMAP_TYPE;
101: }
102:
103: //TODO:: Until partial collections support is enabled for this class
104: protected void basicWriteTo(ObjectOutput out) throws IOException {
105: out.writeBoolean(accessOrder);
106: out.writeInt(references.size());
107: for (Iterator i = references.entrySet().iterator(); i.hasNext();) {
108: Entry entry = (Entry) i.next();
109: out.writeObject(entry.getKey());
110: out.writeObject(entry.getValue());
111: }
112: }
113:
114: // TODO:: Until CollectionsPersistor saves retrieves data in references map.
115: static MapManagedObjectState readFrom(ObjectInput in)
116: throws IOException, ClassNotFoundException {
117: LinkedHashMapManagedObjectState linkedsmo = new LinkedHashMapManagedObjectState(
118: in);
119: linkedsmo.accessOrder = in.readBoolean();
120: int size = in.readInt();
121: LinkedHashMap map = new LinkedHashMap(size, 0.75f,
122: linkedsmo.accessOrder);
123: for (int i = 0; i < size; i++) {
124: Object key = in.readObject();
125: Object value = in.readObject();
126: map.put(key, value);
127: }
128: linkedsmo.setMap(map);
129: return linkedsmo;
130: }
131:
132: }
|