1 22 package org.jboss.ejb.plugins.local; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.Serializable ; 28 import java.lang.reflect.Method ; 29 import javax.ejb.EJBLocalObject ; 30 import javax.naming.InitialContext ; 31 32 38 public abstract class LocalProxy implements Serializable 39 { 40 static final long serialVersionUID = 8387750757101826407L; 42 44 46 47 protected static final Object [] EMPTY_ARGS = {}; 48 49 50 protected static final Method TO_STRING; 51 52 53 protected static final Method HASH_CODE; 54 55 56 protected static final Method EQUALS; 57 58 59 protected static final Method GET_PRIMARY_KEY; 60 61 62 protected static final Method GET_EJB_HOME; 63 64 65 protected static final Method IS_IDENTICAL; 66 67 68 protected static final Method REMOVE; 69 70 protected String jndiName; 71 protected transient BaseLocalProxyFactory factory; 72 73 76 static 77 { 78 try 79 { 80 final Class [] empty = {}; 81 final Class type = EJBLocalObject .class; 82 83 GET_PRIMARY_KEY = type.getMethod("getPrimaryKey", empty); 84 GET_EJB_HOME = type.getMethod("getEJBLocalHome", empty); 85 IS_IDENTICAL = type.getMethod("isIdentical", new Class [] { type }); 86 REMOVE = type.getMethod("remove", empty); 87 } 88 catch (Exception e) 89 { 90 e.printStackTrace(); 91 throw new ExceptionInInitializerError (e); 92 } 93 } 94 95 98 static 99 { 100 try 101 { 102 final Class [] empty = {}; 103 final Class type = Object .class; 104 105 TO_STRING = type.getMethod("toString", empty); 106 HASH_CODE = type.getMethod("hashCode", empty); 107 EQUALS = type.getMethod("equals", new Class [] { type }); 108 } 109 catch (Exception e) 110 { 111 e.printStackTrace(); 112 throw new ExceptionInInitializerError (e); 113 } 114 } 115 116 protected String getJndiName() 117 { 118 return jndiName; 119 } 120 protected abstract Object getId(); 121 122 public LocalProxy(String jndiName, BaseLocalProxyFactory factory) 123 { 124 this.jndiName = jndiName; 125 this.factory = factory; 126 } 127 128 137 Boolean isIdentical(final Object a, final Object b) 138 { 139 final EJBLocalObject ejb = (EJBLocalObject )a; 140 Boolean isIdentical = Boolean.FALSE; 141 if( ejb != null ) 142 { 143 isIdentical = new Boolean (ejb.toString().equals(b)); 144 } 145 return isIdentical; 146 } 147 148 152 String toStringImpl() 153 { 154 return jndiName + ":" + getId(); 155 } 156 157 public Object invoke(final Object proxy, final Method m, Object [] args) 158 throws Throwable 159 { 160 Object id = getId(); 161 Object retValue = null; 162 163 if (m.equals(TO_STRING)) 165 { 166 retValue = toStringImpl(); 167 } 168 else if (m.equals(EQUALS)) 169 { 170 retValue = invoke(proxy, IS_IDENTICAL, args ); 171 } 172 else if (m.equals(HASH_CODE)) 173 { 174 retValue = new Integer (id.hashCode()); 175 } 176 177 else if (m.equals(GET_PRIMARY_KEY)) 179 { 180 retValue = id; 181 } 182 else if (m.equals(GET_EJB_HOME)) 183 { 184 InitialContext ctx = new InitialContext (); 185 return ctx.lookup(jndiName); 186 } 187 else if (m.equals(IS_IDENTICAL)) 188 { 189 retValue = isIdentical(args[0], toStringImpl()); 190 } 191 return retValue; 192 } 193 194 197 private void readObject(ObjectInputStream in) 198 throws IOException , ClassNotFoundException 199 { 200 in.defaultReadObject(); 201 factory = (BaseLocalProxyFactory) BaseLocalProxyFactory.invokerMap.get(jndiName); 202 } 203 204 private void writeObject(ObjectOutputStream out) 205 throws IOException 206 { 207 out.defaultWriteObject(); 208 } 209 210 } 211 | Popular Tags |