1 45 package org.openejb.client; 46 47 import java.io.Serializable ; 48 import java.lang.reflect.Method ; 49 import java.rmi.NoSuchObjectException ; 50 import java.util.HashSet ; 51 import java.util.Hashtable ; 52 import java.util.Iterator ; 53 54 import org.openejb.client.proxy.InvocationHandler; 55 56 61 public abstract class EJBInvocationHandler implements InvocationHandler, Serializable , ResponseCodes, RequestMethods { 62 63 protected static final Method EQUALS = getMethod(Object .class, "equals", null); 64 protected static final Method HASHCODE = getMethod(Object .class, "hashCode", null); 65 protected static final Method TOSTRING = getMethod(Object .class, "toString", null); 66 67 94 protected static final Hashtable liveHandleRegistry = new Hashtable (); 95 96 99 protected transient boolean inProxyMap = false; 100 101 104 protected transient boolean isInvalidReference = false; 105 106 107 115 118 protected transient EJBRequest request; 119 120 124 protected transient EJBMetaDataImpl ejb; 125 129 protected transient ServerMetaData server; 130 134 protected transient ClientMetaData client; 135 136 140 protected transient Object primaryKey; 141 142 145 public EJBInvocationHandler(){ 146 } 147 148 public EJBInvocationHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){ 149 this.ejb = ejb; 150 this.server = server; 151 this.client = client; 152 } 153 154 public EJBInvocationHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client, Object primaryKey){ 155 this(ejb, server, client); 156 this.primaryKey = primaryKey; 157 } 158 159 protected static Method getMethod(Class c, String method, Class [] params){ 160 try{ 161 return c.getMethod(method, params ); 162 }catch(NoSuchMethodException nse){ 163 } 166 return null; 167 } 168 169 179 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 180 if (isInvalidReference) throw new NoSuchObjectException ("reference is invalid"); 181 182 Object returnObj = null; 183 returnObj = _invoke(proxy,method,args); 184 return returnObj; 185 } 186 187 198 protected abstract Object _invoke(Object proxy, Method method, Object [] args) throws Throwable ; 199 200 public static void print(String s){ 201 } 203 204 public static void println(String s){ 205 } 207 208 protected EJBResponse request(EJBRequest req) throws Exception { 209 return (EJBResponse) Client.request(req, new EJBResponse(), server); 210 } 211 212 219 protected void invalidateReference(){ 220 this.server = null; 221 this.client = null; 222 this.ejb = null; 223 this.inProxyMap = false; 224 this.isInvalidReference = true; 225 this.primaryKey = null; 226 } 227 228 233 protected static void invalidateAllHandlers(Object key){ 234 235 HashSet set = (HashSet )liveHandleRegistry.remove( key ); 236 if ( set == null ) return; 237 238 synchronized ( set ) { 239 Iterator handlers = set.iterator(); 240 while(handlers.hasNext()){ 241 EJBInvocationHandler handler = (EJBInvocationHandler)handlers.next(); 242 handler.invalidateReference(); 243 } 244 set.clear(); 245 } 246 } 247 248 254 protected static void registerHandler(Object key, EJBInvocationHandler handler){ 255 HashSet set = (HashSet )liveHandleRegistry.get(key); 256 257 if ( set == null ) { 258 set = new HashSet (); 259 liveHandleRegistry.put( key, set ); 260 } 261 262 synchronized (set) { 263 set.add(handler); 264 } 265 } 266 } | Popular Tags |