1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 import java.util.*; 28 29 import org.jboss.ejb.EntityContainer; 30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 31 import org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler; 32 import org.jboss.ejb.plugins.cmp.bridge.FieldBridge; 33 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge; 34 import org.jboss.proxy.compiler.Proxy; 35 import org.jboss.proxy.compiler.InvocationHandler; 36 import org.jboss.deployment.DeploymentException; 37 38 48 49 public final class JDBCCreateBeanClassInstanceCommand 50 { 51 private final JDBCEntityBridge entityBridge; 52 private final Class beanClass; 53 private final Constructor beanProxyConstructor; 54 private final Map fieldMap; 55 private final Map selectorMap; 56 57 public JDBCCreateBeanClassInstanceCommand(JDBCStoreManager manager) 58 throws Exception 59 { 60 EntityContainer theContainer = manager.getContainer(); 61 entityBridge = (JDBCEntityBridge) manager.getEntityBridge(); 62 beanClass = theContainer.getBeanClass(); 63 fieldMap = createFieldMap(); 64 selectorMap = createSelectorMap(); 65 EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(fieldMap, selectorMap, beanClass); 67 Class [] classes = new Class []{beanClass}; 68 ClassLoader classLoader = beanClass.getClassLoader(); 69 70 Object o = Proxy.newProxyInstance(classLoader, classes, handler); 71 72 beanProxyConstructor = o.getClass().getConstructor(new Class []{InvocationHandler.class}); 74 75 execute(); 77 } 78 79 public void destroy() 80 { 81 Proxy.forgetProxyForClass(beanClass); 82 } 83 84 public Object execute() throws Exception 85 { 86 EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(fieldMap, selectorMap, beanClass); 87 return beanProxyConstructor.newInstance(new Object []{handler}); 88 } 89 90 private Map getAbstractAccessors() 91 { 92 Method [] methods = beanClass.getMethods(); 93 Map abstractAccessors = new HashMap(methods.length); 94 95 for(int i = 0; i < methods.length; i++) 96 { 97 if(Modifier.isAbstract(methods[i].getModifiers())) 98 { 99 String methodName = methods[i].getName(); 100 if(methodName.startsWith("get") || methodName.startsWith("set")) 101 { 102 abstractAccessors.put(methodName, methods[i]); 103 } 104 } 105 } 106 return abstractAccessors; 107 } 108 109 private Map createFieldMap() throws DeploymentException 110 { 111 Map abstractAccessors = getAbstractAccessors(); 112 113 List fields = entityBridge.getFields(); 114 Map map = new HashMap(fields.size() * 2); 115 for(int i = 0; i < fields.size(); i++) 116 { 117 FieldBridge field = (FieldBridge) fields.get(i); 118 119 String fieldName = field.getFieldName(); 121 String fieldBaseName = Character.toUpperCase(fieldName.charAt(0)) + 122 fieldName.substring(1); 123 String getterName = "get" + fieldBaseName; 124 String setterName = "set" + fieldBaseName; 125 126 Method getterMethod = (Method ) abstractAccessors.get(getterName); 128 Method setterMethod = (Method ) abstractAccessors.get(setterName); 129 130 if(getterMethod != null && setterMethod == null) 132 { 133 throw new DeploymentException("Getter was found but no setter was found for field " + fieldName 134 + " in entity " + entityBridge.getEntityName()); 135 } 136 else if(getterMethod == null && setterMethod != null) 137 { 138 throw new DeploymentException("Setter was found but no getter was found for field " + fieldName 139 + " in entity " + entityBridge.getEntityName()); 140 } 141 else if(getterMethod != null && setterMethod != null) 142 { 143 map.put(getterMethod.getName(), new EntityBridgeInvocationHandler.FieldGetInvoker(field)); 145 map.put(setterMethod.getName(), new EntityBridgeInvocationHandler.FieldSetInvoker(field)); 146 147 abstractAccessors.remove(getterName); 149 abstractAccessors.remove(setterName); 150 } 151 } 152 return Collections.unmodifiableMap(map); 153 } 154 155 private Map createSelectorMap() 156 { 157 Collection selectors = entityBridge.getSelectors(); 158 Map map = new HashMap(selectors.size()); 159 for(Iterator iter = selectors.iterator(); iter.hasNext();) 160 { 161 SelectorBridge selector = (SelectorBridge) iter.next(); 162 map.put(selector.getMethod(), selector); 164 } 165 return Collections.unmodifiableMap(map); 166 } 167 } 168 | Popular Tags |