KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.tc.object.ObjectID;
7 import com.tc.object.SerializationUtil;
8 import com.tc.object.dna.api.DNACursor;
9 import com.tc.object.dna.api.DNAWriter;
10 import com.tc.object.dna.api.LogicalAction;
11 import com.tc.object.dna.api.PhysicalAction;
12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade;
13 import com.tc.objectserver.mgmt.ManagedObjectFacade;
14 import com.tc.util.Assert;
15
16 import java.io.IOException JavaDoc;
17 import java.io.ObjectInput JavaDoc;
18 import java.io.ObjectOutput JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * Server representation of a list
26  */

27 public class QueueManagedObjectState extends LogicalManagedObjectState {
28   private static final String JavaDoc TAKE_LOCK_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.takeLock";
29   private static final String JavaDoc PUT_LOCK_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.putLock";
30   private static final String JavaDoc CAPACITY_FIELD_NAME = "java.util.concurrent.LinkedBlockingQueue.capacity";
31
32   private ObjectID takeLockField;
33   private ObjectID putLockField;
34   private Object JavaDoc capacityField;
35
36   private List JavaDoc references;
37
38   QueueManagedObjectState(ObjectInput JavaDoc in) throws IOException JavaDoc {
39     super(in);
40   }
41
42   protected QueueManagedObjectState(long classID) {
43     super(classID);
44     references = new LinkedList JavaDoc();
45   }
46
47   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
48     while (cursor.next()) {
49       Object JavaDoc action = cursor.getAction();
50       if (action instanceof LogicalAction) {
51         LogicalAction logicalAction = (LogicalAction) action;
52         int method = logicalAction.getMethod();
53         Object JavaDoc[] params = logicalAction.getParameters();
54         applyMethod(objectID, includeIDs, method, params);
55       } else if (action instanceof PhysicalAction) {
56         PhysicalAction physicalAction = (PhysicalAction) action;
57         updateReference(physicalAction.getFieldName(), physicalAction.getObject());
58       }
59     }
60   }
61
62   private void updateReference(String JavaDoc fieldName, Object JavaDoc value) {
63     if (TAKE_LOCK_FIELD_NAME.equals(fieldName)) {
64       takeLockField = (ObjectID) value;
65     } else if (PUT_LOCK_FIELD_NAME.equals(fieldName)) {
66       putLockField = (ObjectID) value;
67     } else if (CAPACITY_FIELD_NAME.equals(fieldName)) {
68       capacityField = value;
69     }
70   }
71
72   public void applyMethod(ObjectID objectID, BackReferences includeIDs, int method, Object JavaDoc[] params) {
73     switch (method) {
74       case SerializationUtil.PUT:
75         addChangeToCollector(objectID, params[0], includeIDs);
76         references.add(params[0]);
77         break;
78       case SerializationUtil.TAKE:
79         references.remove(0);
80         break;
81       case SerializationUtil.CLEAR:
82         references.clear();
83         break;
84       case SerializationUtil.REMOVE_FIRST_N:
85         int n = ((Integer JavaDoc) params[0]).intValue();
86         for (int i = 0; i < n; i++) {
87           references.remove(0);
88         }
89         break;
90       case SerializationUtil.REMOVE_AT:
91         int i = ((Integer JavaDoc) params[0]).intValue();
92         Assert.assertTrue(references.size() > i);
93         references.remove(i);
94         break;
95       default:
96         throw new AssertionError JavaDoc("Invalid method:" + method + " state:" + this);
97     }
98   }
99
100   // Since LinkedBlockingQueue supports partial collection, we are not adding it to back references
101
private void addChangeToCollector(ObjectID objectID, Object JavaDoc newValue, BackReferences includeIDs) {
102     if (newValue instanceof ObjectID) {
103       getListener().changed(objectID, null, (ObjectID) newValue);
104     }
105   }
106   
107   public void addObjectReferencesTo(ManagedObjectTraverser traverser) {
108     traverser.addReachableObjectIDs(getObjectReferences());
109   }
110
111
112   protected void addAllObjectReferencesTo(Set JavaDoc refs) {
113     addAllObjectReferencesFromIteratorTo(references.iterator(), refs);
114     if (takeLockField != null) {
115       refs.add(takeLockField);
116     }
117     if (putLockField != null) {
118       refs.add(putLockField);
119     }
120   }
121
122   public void dehydrate(ObjectID objectID, DNAWriter writer) {
123     dehydrateFields(objectID, writer);
124     dehydrateMembers(objectID, writer);
125   }
126
127   private void dehydrateFields(ObjectID objectId, DNAWriter writer) {
128     writer.addPhysicalAction(TAKE_LOCK_FIELD_NAME, takeLockField);
129     writer.addPhysicalAction(PUT_LOCK_FIELD_NAME, putLockField);
130     writer.addPhysicalAction(CAPACITY_FIELD_NAME, capacityField);
131   }
132
133   private void dehydrateMembers(ObjectID objectID, DNAWriter writer) {
134     for (Iterator JavaDoc i = references.iterator(); i.hasNext();) {
135       Object JavaDoc o = i.next();
136       writer.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { o });
137     }
138   }
139
140   public String JavaDoc toString() {
141     return "QueueManagedStateObject(" + references + ")";
142   }
143
144   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
145     final int size = references.size();
146
147     if (limit < 0) {
148       limit = size;
149     } else {
150       limit = Math.min(limit, size);
151     }
152
153     Object JavaDoc[] data = new Object JavaDoc[limit];
154
155     int index = 0;
156     for (Iterator JavaDoc iter = references.iterator(); iter.hasNext() && index < limit; index++) {
157       data[index] = iter.next();
158     }
159
160     return LogicalManagedObjectFacade.createListInstance(objectID, className, data, size);
161   }
162
163   public byte getType() {
164     return QUEUE_TYPE;
165   }
166
167   private void writeField(ObjectOutput JavaDoc out, String JavaDoc fieldName, Object JavaDoc fieldValue) throws IOException JavaDoc {
168     out.writeUTF(fieldName);
169     if (fieldValue == null) {
170       out.writeBoolean(false);
171     } else {
172       out.writeBoolean(true);
173       if (fieldValue instanceof ObjectID) {
174         out.writeLong(((ObjectID) fieldValue).toLong());
175       } else {
176         out.writeObject(fieldValue);
177       }
178     }
179   }
180   
181   protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
182     writeField(out, TAKE_LOCK_FIELD_NAME, takeLockField);
183     writeField(out, PUT_LOCK_FIELD_NAME, putLockField);
184     writeField(out, CAPACITY_FIELD_NAME, capacityField);
185
186     out.writeInt(references.size());
187     for (Iterator JavaDoc i = references.iterator(); i.hasNext();) {
188       out.writeObject(i.next());
189     }
190   }
191
192   protected boolean basicEquals(LogicalManagedObjectState o) {
193     QueueManagedObjectState mo = (QueueManagedObjectState) o;
194     return ((takeLockField == mo.takeLockField) || (takeLockField != null && takeLockField.equals(mo.takeLockField)))
195            && ((putLockField == mo.putLockField) || (putLockField != null && putLockField.equals(mo.putLockField)))
196            && ((capacityField == mo.capacityField) || (capacityField != null && capacityField.equals(mo.capacityField)))
197            && references.equals(mo.references);
198   }
199   
200   private static void readField(ObjectInput JavaDoc in, QueueManagedObjectState mo) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
201     String JavaDoc fieldName = in.readUTF();
202     boolean fieldExist = in.readBoolean();
203     if (fieldExist) {
204       if (fieldName.equals(TAKE_LOCK_FIELD_NAME)) {
205         mo.takeLockField = new ObjectID(in.readLong());
206       } else if (fieldName.equals(PUT_LOCK_FIELD_NAME)) {
207         mo.putLockField = new ObjectID(in.readLong());
208       } else if (fieldName.equals(CAPACITY_FIELD_NAME)) {
209         mo.capacityField = in.readObject();
210       } else {
211         throw new AssertionError JavaDoc("Field not recognized in QueueManagedObjectState.readFrom().");
212       }
213     }
214   }
215
216   static QueueManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
217     QueueManagedObjectState mo = new QueueManagedObjectState(in);
218     readField(in, mo);
219     readField(in, mo);
220     readField(in, mo);
221     
222     
223     int size = in.readInt();
224     LinkedList JavaDoc list = new LinkedList JavaDoc();
225     for (int i = 0; i < size; i++) {
226       list.add(in.readObject());
227     }
228     mo.references = list;
229     return mo;
230   }
231 }
Popular Tags