1 22 package org.jboss.ejb; 23 24 import java.io.Externalizable ; 25 import java.io.ObjectOutput ; 26 import java.io.ObjectInput ; 27 import java.io.IOException ; 28 import java.lang.reflect.Method ; 29 import java.rmi.MarshalledObject ; 30 31 import org.jboss.logging.Logger; 32 33 50 public class CacheKey 51 implements Externalizable 52 { 53 static final long serialVersionUID = -7108821554259950778L; 55 56 58 70 protected Object id; 71 72 public Object getId() 73 { 74 return id; 75 } 76 77 78 protected MarshalledObject mo; 79 80 81 protected int hashCode; 82 83 85 87 public CacheKey() 88 { 89 } 91 92 public CacheKey(Object id) 93 { 94 if (id == null) throw new Error ("id may not be null"); 96 97 this.id = id; 98 try 99 { 100 104 try 105 { 106 Class [] equalsArgs = {Object .class}; 107 Method equals = id.getClass().getDeclaredMethod("equals", equalsArgs); 108 Class [] hashCodeArgs = {}; 109 Method hash = id.getClass().getDeclaredMethod("hashCode", hashCodeArgs); 110 hashCode = id.hashCode(); 112 } 113 catch(NoSuchMethodException ex) 114 { 115 mo = new MarshalledObject (id); 117 hashCode = mo.hashCode(); 119 } 120 } 121 catch (Exception e) 122 { 123 Logger log = Logger.getLogger(getClass()); 124 log.error("failed to initialize, id="+id, e); 125 } 126 } 127 128 130 132 134 136 public void writeExternal(ObjectOutput out) 137 throws IOException 138 { 139 out.writeObject(id); 140 out.writeObject(mo); 141 out.writeInt(hashCode); 142 } 143 144 public void readExternal(ObjectInput in) 145 throws IOException , ClassNotFoundException 146 { 147 id = in.readObject(); 148 mo = (MarshalledObject ) in.readObject(); 149 hashCode = in.readInt(); 150 } 151 152 154 158 public int hashCode() 159 { 160 return hashCode; 162 } 163 164 170 public boolean equals(Object object) 171 { 172 boolean equals = false; 173 if (object instanceof CacheKey) 174 { 175 CacheKey ckey = (CacheKey) object; 176 Object key = ckey.id; 177 if( mo == null ) 179 equals = id.equals(key); 180 else 181 equals = mo.equals(ckey.mo); 182 } 183 return equals; 184 } 185 186 public String toString() 187 { 188 return id.toString(); 189 } 190 191 } 193 | Popular Tags |