1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler; 25 import org.jboss.ejb.plugins.cmp.bridge.FieldBridge; 26 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2; 27 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.EJBSelectBridge; 28 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema; 29 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 30 import org.jboss.ejb.EntityContainer; 31 import org.jboss.proxy.compiler.Proxy; 32 import org.jboss.proxy.compiler.InvocationHandler; 33 import org.jboss.deployment.DeploymentException; 34 35 import javax.ejb.FinderException ; 36 import java.lang.reflect.Constructor ; 37 import java.lang.reflect.Method ; 38 import java.lang.reflect.Modifier ; 39 import java.util.Map ; 40 import java.util.HashMap ; 41 import java.util.List ; 42 import java.util.Collections ; 43 import java.util.Iterator ; 44 import java.util.Collection ; 45 46 47 51 public class InstanceFactory 52 { 53 private final Class beanClass; 54 private final Constructor beanProxyConstructor; 55 private final Map fieldMap; 56 private final Map selectorMap; 57 58 public InstanceFactory(JDBCStoreManager2 manager, JDBCEntityBridge2 entity) 59 throws Exception 60 { 61 EntityContainer theContainer = manager.getContainer(); 62 beanClass = theContainer.getBeanClass(); 63 fieldMap = createFieldMap(entity); 64 selectorMap = createSelectorMap(entity, manager.getQueryFactory()); 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 newInstance(); 77 } 78 79 public void destroy() 80 { 81 Proxy.forgetProxyForClass(beanClass); 82 } 83 84 public Object newInstance() throws Exception 85 { 86 EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(fieldMap, selectorMap, beanClass); 87 return beanProxyConstructor.newInstance(new Object []{handler}); 88 } 89 90 private static Map getAbstractAccessors(Class beanClass) 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 static Map createFieldMap(JDBCEntityBridge2 entityBridge) throws DeploymentException 110 { 111 Map abstractAccessors = getAbstractAccessors(entityBridge.getMetaData().getEntityClass()); 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)) + fieldName.substring(1); 122 String getterName = "get" + fieldBaseName; 123 String setterName = "set" + fieldBaseName; 124 125 Method getterMethod = (Method ) abstractAccessors.get(getterName); 127 Method setterMethod = (Method ) abstractAccessors.get(setterName); 128 129 if(getterMethod != null && setterMethod == null) 131 { 132 throw new DeploymentException("Getter was found but, no setter was found for field: " + fieldName); 133 } 134 else if(getterMethod == null && setterMethod != null) 135 { 136 throw new DeploymentException("Setter was found but, no getter was found for field: " + fieldName); 137 } 138 else if(getterMethod != null && setterMethod != null) 139 { 140 map.put(getterMethod.getName(), new EntityBridgeInvocationHandler.FieldGetInvoker(field)); 142 map.put(setterMethod.getName(), new EntityBridgeInvocationHandler.FieldSetInvoker(field)); 143 144 abstractAccessors.remove(getterName); 146 abstractAccessors.remove(setterName); 147 } 148 } 149 return Collections.unmodifiableMap(map); 150 } 151 152 private static Map createSelectorMap(JDBCEntityBridge2 entityBridge, QueryFactory queryFactory) 153 throws DeploymentException 154 { 155 Collection queries = entityBridge.getMetaData().getQueries(); 156 Map selectorsByMethod = new HashMap (queries.size()); 157 Iterator definedFinders = queries.iterator(); 158 while(definedFinders.hasNext()) 159 { 160 JDBCQueryMetaData metadata = (JDBCQueryMetaData)definedFinders.next(); 161 if(metadata.getMethod().getName().startsWith("ejbSelect")) 162 { 163 try 164 { 165 QueryCommand queryCommand = queryFactory.getQueryCommand(metadata.getMethod()); 166 Schema schema = ((JDBCStoreManager2)entityBridge.getManager()).getSchema(); 167 EJBSelectBridge ejbSelectBridge = new EJBSelectBridge(entityBridge.getContainer(), schema, metadata, queryCommand); 168 selectorsByMethod.put(metadata.getMethod(), ejbSelectBridge); 169 } 170 catch(FinderException e) 171 { 172 throw new DeploymentException(e.getMessage()); 173 } 174 } 175 } 176 177 return selectorsByMethod; 178 } 179 } 180 | Popular Tags |