KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.tc.objectserver.managedobject;
6
7 import com.tc.exception.TCRuntimeException;
8 import com.tc.logging.TCLogger;
9 import com.tc.logging.TCLogging;
10 import com.tc.object.ObjectID;
11 import com.tc.object.dna.api.DNA;
12 import com.tc.object.dna.api.DNACursor;
13 import com.tc.object.dna.api.DNAWriter;
14 import com.tc.object.dna.api.PhysicalAction;
15 import com.tc.objectserver.managedobject.bytecode.ClassNotCompatableException;
16 import com.tc.objectserver.mgmt.ManagedObjectFacade;
17 import com.tc.objectserver.mgmt.PhysicalManagedObjectFacade;
18 import com.tc.text.PrettyPrintable;
19 import com.tc.text.PrettyPrinter;
20
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInput JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * State for Physically managed objects. This class is abstract and generated classes of Physical State Class Loader
33  * extends this class So changes to this class needs to be done cautiously.
34  */

35 public abstract class PhysicalManagedObjectState extends AbstractManagedObjectState implements Serializable JavaDoc,
36     PrettyPrintable {
37
38   private static final TCLogger logger = TCLogging.getLogger(PhysicalManagedObjectState.class);
39
40   public PhysicalManagedObjectState() {
41     super();
42   }
43
44   /**
45    * This is only for testing, its highly inefficient
46    */

47   protected boolean basicEquals(AbstractManagedObjectState o) {
48     PhysicalManagedObjectState cmp = (PhysicalManagedObjectState) o;
49     boolean result = getParentID().equals(cmp.getParentID()) && getClassName().equals(cmp.getClassName())
50                      && getLoaderDescription().equals(cmp.getLoaderDescription());
51     if (!result) return result;
52     Map JavaDoc mine = addValues(new HashMap JavaDoc());
53     Map JavaDoc his = cmp.addValues(new HashMap JavaDoc());
54     return mine.equals(his);
55   }
56
57   public ObjectID getParentID() {
58     return ObjectID.NULL_ID;
59   }
60
61   public void setParentID(ObjectID id) {
62     // This is over-riden when needed
63
}
64
65   public int hashCode() {
66     throw new TCRuntimeException("Don't hash me!");
67   }
68
69   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
70     ManagedObjectChangeListener listener = getListener();
71     while (cursor.next()) {
72       PhysicalAction a = cursor.getPhysicalAction();
73       Object JavaDoc value = a.getObject();
74       String JavaDoc fieldName = a.getFieldName();
75
76       if (value == null) { throw new AssertionError JavaDoc("attempt to apply null value to field " + fieldName + " in "
77                                                     + objectID + ", " + toString()); }
78
79       Object JavaDoc old = set(fieldName, value);
80       ObjectID oldValue = old instanceof ObjectID ? (ObjectID) old : ObjectID.NULL_ID;
81       ObjectID newValue = value instanceof ObjectID ? (ObjectID) value : ObjectID.NULL_ID;
82       listener.changed(objectID, oldValue, newValue);
83     }
84   }
85
86   /**
87    * @return old Value
88    */

89   public Object JavaDoc set(String JavaDoc fieldName, Object JavaDoc value) {
90     try {
91       return basicSet(fieldName, value);
92     } catch (ClassNotCompatableException cne) {
93       // This exception triggers a regeneration of the state class !
94
logger.warn("Recoverable Incompatible Class Change Identified : " + cne.getMessage());
95       throw cne;
96     } catch (ClassCastException JavaDoc cce) {
97       // This is due to a change in the type of the fields which is currently not supported.
98
// Not throwing the exception 'coz we dont want to crash the server because of an
99
// incompatable change
100
cce.printStackTrace();
101       logger.error("Unrecoverable Incompatible Class Change : fieldName = " + fieldName + " value = " + value, cce);
102       return null;
103     } catch (Exception JavaDoc e) {
104       e.printStackTrace();
105       logger.error("Incompatible Change : Class Does not support it", e);
106       return null;
107     }
108   }
109
110   public void addObjectReferencesTo(ManagedObjectTraverser traverser) {
111     traverser.addReachableObjectIDs(getObjectReferences());
112   }
113
114   /**
115    * This method is generated by PhysicalStateClassLoader. It adds all the values of the fields into the map. This is
116    * just a convinent methods for printing the values, creating facade and checking for equals, but this should not be
117    * used in any other case as there is an overhead involved.
118    */

119   public abstract Map JavaDoc addValues(Map JavaDoc m);
120
121   public abstract Set JavaDoc getObjectReferences();
122
123   /**
124    * This method is generated by PhysicalStateClassLoader.
125    */

126   protected abstract void basicDehydrate(DNAWriter writer);
127
128   /**
129    * This method is generated by PhysicalStateClassLoader.
130    */

131   protected abstract int getClassId();
132
133   /**
134    * This method is generated by PhysicalStateClassLoader.
135    *
136    * @return old Value
137    */

138   protected abstract Object JavaDoc basicSet(String JavaDoc fieldName, Object JavaDoc value);
139
140   /**
141    * This method is generated by PhysicalStateClassLoader.
142    */

143   protected abstract void readObject(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc;
144
145   /**
146    * This method is generated by PhysicalStateClassLoader.
147    *
148    * @return old Value
149    */

150   protected abstract void writeObject(ObjectOutput JavaDoc out) throws IOException JavaDoc;
151
152   public void dehydrate(ObjectID objectID, DNAWriter writer) {
153     basicDehydrate(writer);
154     writer.setParentObjectID(getParentID());
155   }
156
157   public String JavaDoc toString() {
158     // XXX: Um... this is gross.
159
StringWriter JavaDoc writer = new StringWriter JavaDoc();
160     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
161     new PrettyPrinter(pWriter).visit(this);
162     return writer.getBuffer().toString();
163   }
164
165   public PrettyPrinter prettyPrint(PrettyPrinter out) {
166     PrettyPrinter rv = out;
167     out = out.print(getClass().getName()).duplicateAndIndent().println();
168     out.indent().print("parentID : " + getParentID());
169     out.indent().print("className : " + getClassName());
170     out.indent().print("loaderDesc: " + getLoaderDescription());
171     out.indent().print("references: " + addValues(new HashMap JavaDoc())).println();
172     out.indent().print("listener: " + getListener()).println();
173     return rv;
174   }
175
176   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
177     // NOTE: limit is ignored for physical object facades
178

179     Map JavaDoc dataCopy = addValues(new HashMap JavaDoc());
180
181     ObjectID parentID = getParentID();
182     boolean isInner = !parentID.isNull();
183
184     return new PhysicalManagedObjectFacade(objectID, parentID, className, dataCopy, isInner, DNA.NULL_ARRAY_SIZE, false);
185   }
186
187   public byte getType() {
188     return PHYSICAL_TYPE;
189   }
190
191   public void writeTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
192     // write the class identifier
193
out.writeInt(this.getClassId());
194     ObjectID parentID = getParentID();
195     if (ObjectID.NULL_ID.equals(parentID)) {
196       out.writeBoolean(false);
197     } else {
198       out.writeBoolean(true);
199       out.writeLong(parentID.toLong());
200     }
201
202     writeObject(out);
203   }
204
205   static PhysicalManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
206     // read the class identifier
207
int classId = in.readInt();
208     ObjectID pid = ObjectID.NULL_ID;
209     if (in.readBoolean()) {
210       pid = new ObjectID(in.readLong());
211     }
212
213     PhysicalManagedObjectState pmos = ManagedObjectStateFactory.getInstance().createPhysicalState(pid, classId);
214     pmos.readObject(in);
215     return pmos;
216   }
217
218 }
219
Popular Tags