KickJava   Java API By Example, From Geeks To Geeks.

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


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.object.ObjectID;
9 import com.tc.object.dna.api.DNA;
10 import com.tc.object.dna.api.DNACursor;
11 import com.tc.object.dna.api.DNAWriter;
12 import com.tc.object.dna.api.LiteralAction;
13 import com.tc.object.dna.impl.ClassInstance;
14 import com.tc.object.dna.impl.ClassLoaderInstance;
15 import com.tc.object.dna.impl.UTF8ByteDataHolder;
16 import com.tc.object.loaders.Namespace;
17 import com.tc.objectserver.mgmt.ManagedObjectFacade;
18 import com.tc.objectserver.mgmt.PhysicalManagedObjectFacade;
19 import com.tc.text.PrettyPrintable;
20 import com.tc.text.PrettyPrinter;
21 import com.tc.util.Assert;
22
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInput JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 public class LiteralTypesManagedObjectState extends AbstractManagedObjectState implements PrettyPrintable {
34   private Object JavaDoc references;
35
36   LiteralTypesManagedObjectState() {
37     super();
38   }
39
40   public int hashCode() {
41     throw new TCRuntimeException("Don't hash me!");
42   }
43
44   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
45     final int actionCount = cursor.getActionCount();
46     if (actionCount != 1) {
47       // At one point in time, this assertion was getting tripped by concurrent txns in the same client to commit the
48
// same newly shared literal instance
49
throw new AssertionError JavaDoc("Invalid action count: " + actionCount + "\nThis object is " + toString() + "\nDNA is "
50                                + cursor.toString() + "\nApply object ID: " + objectID);
51     }
52
53     cursor.next();
54     LiteralAction a = (LiteralAction) cursor.getAction();
55     // PhysicalAction a = cursor.getPhysicalAction();
56
// Assert.assertTrue(a.isLiteralType());
57
references = a.getObject();
58   }
59
60   public void dehydrate(ObjectID objectID, DNAWriter writer) {
61     writer.addLiteralValue(references);
62   }
63
64   public String JavaDoc toString() {
65     // XXX: Um... this is gross.
66
StringWriter JavaDoc writer = new StringWriter JavaDoc();
67     PrintWriter JavaDoc pWriter = new PrintWriter JavaDoc(writer);
68     new PrettyPrinter(pWriter).visit(this);
69     return writer.getBuffer().toString();
70   }
71
72   public PrettyPrinter prettyPrint(PrettyPrinter out) {
73     PrettyPrinter rv = out;
74     out = out.print(getClass().getName()).duplicateAndIndent().println();
75     out.indent().print("references: " + references).println();
76     out.indent().print("listener: " + getListener()).println();
77     return rv;
78   }
79
80   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
81     // NOTE: limit is ignored for literal object facades
82

83     Map JavaDoc dataCopy = new HashMap JavaDoc();
84     dataCopy.put(className, references);
85
86     return new PhysicalManagedObjectFacade(objectID, ObjectID.NULL_ID, className, dataCopy, false, DNA.NULL_ARRAY_SIZE,
87                                            false);
88   }
89
90   public Set JavaDoc getObjectReferences() {
91     return Collections.EMPTY_SET;
92   }
93
94   public void addObjectReferencesTo(ManagedObjectTraverser traverser) {
95     // Do Nothing
96
}
97
98   public byte getType() {
99     return LITERAL_TYPE;
100   }
101
102   public void writeTo(ObjectOutput JavaDoc o) throws IOException JavaDoc {
103     o.writeObject(references);
104   }
105
106   static LiteralTypesManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
107     LiteralTypesManagedObjectState lmos = new LiteralTypesManagedObjectState();
108     lmos.references = in.readObject();
109     return lmos;
110   }
111
112   protected boolean basicEquals(AbstractManagedObjectState o) {
113     LiteralTypesManagedObjectState lmos = (LiteralTypesManagedObjectState) o;
114     return references.equals(lmos.references);
115   }
116
117   public String JavaDoc getClassName() {
118     Assert.assertNotNull(references);
119
120     if (references instanceof ClassInstance) {
121       return "java.lang.Class";
122     } else if (references instanceof ClassLoaderInstance) {
123       return "java.lang.ClassLoader";
124     } else if (references instanceof UTF8ByteDataHolder) { return "java.lang.String"; }
125
126     return references.getClass().getName();
127   }
128
129   public String JavaDoc getLoaderDescription() {
130     return Namespace.getStandardBootstrapLoaderName();
131   }
132
133 }
134
Popular Tags