1 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 ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 import java.io.PrintWriter ; 25 import java.io.Serializable ; 26 import java.io.StringWriter ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 35 public abstract class PhysicalManagedObjectState extends AbstractManagedObjectState implements Serializable , 36 PrettyPrintable { 37 38 private static final TCLogger logger = TCLogging.getLogger(PhysicalManagedObjectState.class); 39 40 public PhysicalManagedObjectState() { 41 super(); 42 } 43 44 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 mine = addValues(new HashMap ()); 53 Map his = cmp.addValues(new HashMap ()); 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 } 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 { 70 ManagedObjectChangeListener listener = getListener(); 71 while (cursor.next()) { 72 PhysicalAction a = cursor.getPhysicalAction(); 73 Object value = a.getObject(); 74 String fieldName = a.getFieldName(); 75 76 if (value == null) { throw new AssertionError ("attempt to apply null value to field " + fieldName + " in " 77 + objectID + ", " + toString()); } 78 79 Object 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 89 public Object set(String fieldName, Object value) { 90 try { 91 return basicSet(fieldName, value); 92 } catch (ClassNotCompatableException cne) { 93 logger.warn("Recoverable Incompatible Class Change Identified : " + cne.getMessage()); 95 throw cne; 96 } catch (ClassCastException cce) { 97 cce.printStackTrace(); 101 logger.error("Unrecoverable Incompatible Class Change : fieldName = " + fieldName + " value = " + value, cce); 102 return null; 103 } catch (Exception 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 119 public abstract Map addValues(Map m); 120 121 public abstract Set getObjectReferences(); 122 123 126 protected abstract void basicDehydrate(DNAWriter writer); 127 128 131 protected abstract int getClassId(); 132 133 138 protected abstract Object basicSet(String fieldName, Object value); 139 140 143 protected abstract void readObject(ObjectInput in) throws IOException , ClassNotFoundException ; 144 145 150 protected abstract void writeObject(ObjectOutput out) throws IOException ; 151 152 public void dehydrate(ObjectID objectID, DNAWriter writer) { 153 basicDehydrate(writer); 154 writer.setParentObjectID(getParentID()); 155 } 156 157 public String toString() { 158 StringWriter writer = new StringWriter (); 160 PrintWriter pWriter = new PrintWriter (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 ())).println(); 172 out.indent().print("listener: " + getListener()).println(); 173 return rv; 174 } 175 176 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 177 179 Map dataCopy = addValues(new HashMap ()); 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 out) throws IOException { 192 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 in) throws IOException , ClassNotFoundException { 206 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 |