1 22 package org.jboss.ejb.plugins.cmp.bridge; 23 24 import java.lang.reflect.Method ; 25 import java.util.Map ; 26 27 import javax.ejb.EJBException ; 28 import javax.ejb.FinderException ; 29 30 import org.jboss.ejb.EntityEnterpriseContext; 31 import org.jboss.proxy.compiler.InvocationHandler; 32 33 49 public class EntityBridgeInvocationHandler implements InvocationHandler 50 { 51 private final Class beanClass; 52 private final Map fieldMap; 53 private final Map selectorMap; 54 private EntityEnterpriseContext ctx; 55 56 59 public EntityBridgeInvocationHandler(Map fieldMap, Map selectorMap, Class beanClass) 60 { 61 this.beanClass = beanClass; 62 this.fieldMap = fieldMap; 63 this.selectorMap = selectorMap; 64 } 65 66 public void setContext(EntityEnterpriseContext ctx) 67 { 68 if(ctx != null && !beanClass.isInstance(ctx.getInstance())) 69 { 70 throw new EJBException ("Instance must be an instance of beanClass"); 71 } 72 this.ctx = ctx; 73 } 74 75 public Object invoke(Object proxy, Method method, Object [] args) 76 throws FinderException 77 { 78 87 String methodName = method.getName(); 88 89 BridgeInvoker invoker = (BridgeInvoker) fieldMap.get(methodName); 90 if(invoker == null) 91 { 92 invoker = (BridgeInvoker) selectorMap.get(method); 94 95 if(invoker == null) 96 { 97 throw new EJBException ("Method is not a known CMP field " + 98 "accessor, CMR field accessor, or ejbSelect method: " + 99 "methodName=" + methodName); 100 } 101 } 102 103 try 104 { 105 return invoker.invoke(ctx, method, args); 106 } 107 catch(RuntimeException e) 108 { 109 throw e; 110 } 111 catch(FinderException e) 112 { 113 throw e; 114 } 115 catch(Exception e) 116 { 117 throw new EJBException ("Internal error", e); 118 } 119 } 120 121 123 public interface BridgeInvoker 124 { 125 Object invoke(EntityEnterpriseContext ctx, Method method, Object [] args) throws FinderException , Exception ; 126 } 127 128 public static class FieldGetInvoker implements BridgeInvoker 129 { 130 private final FieldBridge field; 131 132 public FieldGetInvoker(FieldBridge field) 133 { 134 this.field = field; 135 } 136 137 public Object invoke(EntityEnterpriseContext ctx, Method method, Object [] args) 138 { 139 if(ctx == null) 142 { 143 throw new EJBException ("EJB home methods are not allowed to " + 144 "access CMP or CMR fields: methodName=" + method.getName()); 145 } 146 147 return field.getValue(ctx); 148 } 149 } 150 151 public static class FieldSetInvoker implements BridgeInvoker 152 { 153 private final FieldBridge field; 154 155 public FieldSetInvoker(FieldBridge field) 156 { 157 this.field = field; 158 } 159 160 public Object invoke(EntityEnterpriseContext ctx, Method method, Object [] args) 161 { 162 if(ctx == null) 165 { 166 throw new EJBException ("EJB home methods are not allowed to " + 167 "access CMP or CMR fields: methodName=" + method.getName()); 168 } 169 170 field.setValue(ctx, args[0]); 171 return null; 172 } 173 } 174 } 175 | Popular Tags |