1 25 26 package org.objectweb.easybeans.rpc; 27 28 import java.io.Serializable ; 29 import java.lang.reflect.InvocationHandler ; 30 import java.lang.reflect.Method ; 31 import java.rmi.Remote ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.ejb.EJBException ; 36 37 41 public abstract class AbsInvocationHandler implements InvocationHandler , Serializable { 42 43 46 private boolean removed = false; 47 48 51 private String containerId = null; 52 53 56 private String factoryName = null; 57 58 61 private Long beanId = null; 62 63 66 private transient Map <Method , Long > hashedMethods = null; 67 68 71 private String interfaceClassName = null; 72 73 76 private boolean isItfExtendingRmiRemote = false; 77 78 81 private boolean useID = false; 82 83 91 public AbsInvocationHandler(final String containerId, final String factoryName, final boolean useID) { 92 this.containerId = containerId; 93 this.factoryName = factoryName; 94 this.useID = useID; 95 this.hashedMethods = new HashMap <Method , Long >(); 96 } 97 98 110 protected Object handleObjectMethods(final Method method, final Object [] args) { 111 String methodName = method.getName(); 112 113 if (methodName.equals("equals")) { 114 return Boolean.valueOf(this.toString().equals(args[0].toString())); 115 } else if (methodName.equals("toString")) { 116 return this.toString(); 117 } else if (methodName.equals("hashCode")) { 118 return Integer.valueOf(this.toString().hashCode()); 119 } else { 120 throw new IllegalStateException ("Method '" + methodName + "' is not present on Object.class."); 121 } 122 } 123 124 132 protected void handleThrowable(final Throwable originalThrowable, final boolean isApplicationException, 133 final Method method) throws Exception { 134 135 138 Class [] exceptions = method.getExceptionTypes(); 139 if (exceptions != null) { 140 for (Class clazz : exceptions) { 141 if (clazz.isInstance(originalThrowable) 143 && originalThrowable instanceof Exception 144 && !(originalThrowable instanceof RuntimeException )) { 145 throw (Exception ) originalThrowable; 146 } 147 } 148 } 149 150 if (originalThrowable instanceof EJBException ) { 152 throw (EJBException ) originalThrowable; 153 } 154 155 if (originalThrowable instanceof RuntimeException ) { 159 if (isApplicationException) { 160 throw (RuntimeException ) originalThrowable; 161 } 162 throw new EJBException ((RuntimeException ) originalThrowable); 164 } 165 166 if (originalThrowable instanceof Exception ) { 168 throw new EJBException ((Exception ) originalThrowable.getCause()); 169 } 170 171 throw new EJBException (new Exception ("Unexpected Exception", originalThrowable)); 174 } 175 176 180 protected void setBeanId(final Long beanId) { 181 this.beanId = beanId; 182 } 183 184 188 protected void setHashedMethods(final Map <Method , Long > hashedMethods) { 189 this.hashedMethods = hashedMethods; 190 } 191 192 196 protected Long getBeanId() { 197 return beanId; 198 } 199 200 203 protected String getContainerId() { 204 return containerId; 205 } 206 207 210 protected String getFactoryName() { 211 return factoryName; 212 } 213 214 217 protected Map <Method , Long > getHashedMethods() { 218 return hashedMethods; 219 } 220 221 225 protected void setContainerId(final String containerId) { 226 this.containerId = containerId; 227 } 228 229 233 protected void setFactoryName(final String factoryName) { 234 this.factoryName = factoryName; 235 } 236 237 240 public String getInterfaceClassName() { 241 return interfaceClassName; 242 } 243 244 248 protected void setInterfaceClassName(final String interfaceClassName) { 249 this.interfaceClassName = interfaceClassName; 250 } 251 252 256 public void setInterfaceClass(final Class clz) { 257 if (Remote .class.isAssignableFrom(clz)) { 258 isItfExtendingRmiRemote = true; 259 } 260 setInterfaceClassName(clz.getName()); 261 } 262 263 264 268 public void setExtendingRmiRemote(final boolean isItfExtendingRmiRemote) { 269 this.isItfExtendingRmiRemote = isItfExtendingRmiRemote; 270 } 271 272 275 public boolean isExtendingRmiRemote() { 276 return isItfExtendingRmiRemote; 277 } 278 279 283 public void setUseID(final boolean useID) { 284 this.useID = useID; 285 } 286 287 290 public boolean isUsingID() { 291 return useID; 292 } 293 294 298 @Override 299 public String toString() { 300 StringBuilder sb = new StringBuilder (); 301 sb.append(factoryName); 302 sb.append("_"); 303 sb.append(interfaceClassName); 304 sb.append("/"); 305 sb.append(containerId); 306 if (useID) { 307 sb.append("@"); 308 sb.append(System.identityHashCode(this)); 309 } 310 return sb.toString(); 311 } 312 313 316 public boolean isRemoved() { 317 return removed; 318 } 319 320 324 public void setRemoved(final boolean removed) { 325 this.removed = removed; 326 } 327 } 328 | Popular Tags |