1 22 package org.jboss.proxy.ejb; 23 24 import java.lang.reflect.Method ; 25 26 import javax.ejb.EJBHome ; 27 import javax.ejb.EJBMetaData ; 28 import javax.ejb.RemoveException ; 29 import javax.ejb.Handle ; 30 import javax.ejb.EJBHome ; 31 import javax.ejb.EJBObject ; 32 import javax.ejb.HomeHandle ; 33 34 import org.jboss.proxy.ejb.handle.HomeHandleImpl; 35 import org.jboss.invocation.Invocation; 36 import org.jboss.invocation.InvocationContext; 37 import org.jboss.invocation.InvocationKey; 38 import org.jboss.invocation.InvocationType; 39 40 46 public class HomeInterceptor 47 extends GenericEJBInterceptor 48 { 49 50 private static final long serialVersionUID = 1333656107035759718L; 51 52 54 protected static final Object [] EMPTY_ARGS = {}; 55 56 57 protected static final Method GET_EJB_META_DATA; 58 59 60 protected static final Method GET_HOME_HANDLE; 61 62 63 protected static final Method REMOVE_BY_HANDLE; 64 65 66 protected static final Method REMOVE_BY_PRIMARY_KEY; 67 68 69 protected static final Method REMOVE_OBJECT; 70 71 static 72 { 73 try 74 { 75 final Class empty[] = {}; 76 final Class type = EJBHome .class; 77 78 GET_EJB_META_DATA = type.getMethod("getEJBMetaData", empty); 79 GET_HOME_HANDLE = type.getMethod("getHomeHandle", empty); 80 REMOVE_BY_HANDLE = type.getMethod("remove", new Class [] { 81 Handle .class 82 }); 83 REMOVE_BY_PRIMARY_KEY = type.getMethod("remove", new Class [] { 84 Object .class 85 }); 86 87 REMOVE_OBJECT = EJBObject .class.getMethod("remove", empty); 89 } 90 catch (Exception e) { 91 e.printStackTrace(); 92 throw new ExceptionInInitializerError (e); 93 } 94 } 95 96 98 100 103 public HomeInterceptor() {} 104 105 107 112 public Object invoke(Invocation invocation) 113 throws Throwable 114 { 115 InvocationContext ctx = invocation.getInvocationContext(); 116 117 Method m = invocation.getMethod(); 118 119 if (m.equals(TO_STRING)) 121 { 122 return ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home"; 123 } 124 else if (m.equals(EQUALS)) 125 { 126 Object [] args = invocation.getArguments(); 128 String argsString = args[0] != null ? args[0].toString() : ""; 129 String thisString = ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home"; 130 return new Boolean (thisString.equals(argsString)); 131 } 132 else if (m.equals(HASH_CODE)) 133 { 134 return new Integer (this.hashCode()); 135 } 136 137 else if (m.equals(GET_HOME_HANDLE)) 139 { 140 return new HomeHandleImpl( 141 (String )ctx.getValue(InvocationKey.JNDI_NAME)); 142 } 143 else if (m.equals(GET_EJB_META_DATA)) 144 { 145 return ctx.getValue(InvocationKey.EJB_METADATA); 146 } 147 else if (m.equals(REMOVE_BY_HANDLE)) 148 { 149 EJBObject object = 151 ((Handle ) invocation.getArguments()[0]).getEJBObject(); 152 153 object.remove(); 155 156 return Void.TYPE; 158 } 159 else if (m.equals(REMOVE_BY_PRIMARY_KEY)) 160 { 161 if(((EJBMetaData )ctx.getValue(InvocationKey.EJB_METADATA)).isSession()) 163 throw new RemoveException ("Session beans cannot be removed " + 164 "by primary key."); 165 166 Object id = invocation.getArguments()[0]; 169 170 invocation.setId(id); 172 invocation.setType(InvocationType.REMOTE); 173 invocation.setMethod(REMOVE_OBJECT); 174 invocation.setArguments(EMPTY_ARGS); 175 return getNext().invoke(invocation); 176 } 177 178 else 180 { 181 182 invocation.setType(InvocationType.HOME); 183 return getNext().invoke(invocation); 185 } 186 } 187 } 188 | Popular Tags |