comparison src/org/dancres/blitz/mangler/MangledEntry.java @ 5:27ca8522be85

Fix bugs in integrity checking and hashCode.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 23 May 2009 09:17:17 +0100
parents 3dc0c5604566
children
comparison
equal deleted inserted replaced
4:7b6200980805 5:27ca8522be85
1 package org.dancres.blitz.mangler; 1 package org.dancres.blitz.mangler;
2 2
3 import java.io.*; 3 import java.io.*;
4 4
5 import com.sun.jini.proxy.MarshalledWrapper; 5 import com.sun.jini.proxy.MarshalledWrapper;
6 import java.util.Collection;
7 import java.util.Iterator;
6 import net.jini.entry.AbstractEntry; 8 import net.jini.entry.AbstractEntry;
9 import net.jini.io.ObjectStreamContext;
10 import net.jini.io.context.IntegrityEnforcement;
7 11
8 /** 12 /**
9 <p>Represents a packaged up Entry ready for unpacking or passing to the 13 <p>Represents a packaged up Entry ready for unpacking or passing to the
10 server for matching. We include null fields as well so as to ensure 14 server for matching. We include null fields as well so as to ensure
11 we have a complete, ordered, list of fields which allows as to build 15 we have a complete, ordered, list of fields which allows as to build
29 private String theCodebase; 33 private String theCodebase;
30 private boolean isWildcard; 34 private boolean isWildcard;
31 private boolean isSnapshot; 35 private boolean isSnapshot;
32 36
33 /** 37 /**
34 * Set to <code>true</code> in <code>readObject</code> method 38 * Set to <code>true</code> in <code>readExternal</code> method
35 * (called when we're deserialized). When we unpack the contents using 39 * (called when we're deserialized). When we unpack the contents using
36 * EntryMangler, we check this flag and perform integrity checks if 40 * EntryMangler, we check this flag and perform integrity checks if
37 * required. 41 * required.
38 */ 42 */
39 private transient boolean checkIntegrity = false; 43 private transient boolean checkIntegrity = false;
45 * nevermind for now. 49 * nevermind for now.
46 */ 50 */
47 public static final MangledEntry NULL_TEMPLATE = 51 public static final MangledEntry NULL_TEMPLATE =
48 new MangledEntry("java.lang.Object", null, new MangledField[0], 52 new MangledEntry("java.lang.Object", null, new MangledField[0],
49 new String[0], true); 53 new String[0], true);
54
55 static boolean integrityEnforced(ObjectInput aStream) {
56 if (aStream instanceof ObjectStreamContext) {
57 Collection ctx =
58 ((ObjectStreamContext) aStream).getObjectStreamContext();
59 for (Iterator i = ctx.iterator(); i.hasNext();) {
60 Object obj = i.next();
61 if (obj instanceof IntegrityEnforcement) {
62 return ((IntegrityEnforcement) obj).integrityEnforced();
63 }
64 }
65 }
66
67 return false;
68 }
50 69
51 public MangledEntry() { 70 public MangledEntry() {
52 71
53 } 72 }
54 73
89 } 108 }
90 109
91 public void readExternal(ObjectInput objectInput) throws IOException, 110 public void readExternal(ObjectInput objectInput) throws IOException,
92 ClassNotFoundException { 111 ClassNotFoundException {
93 112
113 checkIntegrity = integrityEnforced(objectInput);
94 theParents = (String[]) objectInput.readObject(); 114 theParents = (String[]) objectInput.readObject();
95 theFields = new MangledField[objectInput.readInt()]; 115 theFields = new MangledField[objectInput.readInt()];
96 116
97 for (int i = 0; i < theFields.length; i++) { 117 for (int i = 0; i < theFields.length; i++) {
98 int myObjectSize = objectInput.readInt(); 118 int myObjectSize = objectInput.readInt();
195 215
196 public int hashCode() { 216 public int hashCode() {
197 int myHash = 0; 217 int myHash = 0;
198 218
199 for (int i = 0; i < theFields.length; i++) { 219 for (int i = 0; i < theFields.length; i++) {
200 myHash ^= theFields.hashCode(); 220 myHash ^= theFields[i].hashCode();
201 } 221 }
202 222
203 return myHash; 223 return myHash;
204 } 224 }
205 225