1 22 package org.jboss.ejb.plugins.cmp.jdbc2.bridge; 23 24 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge; 25 import org.jboss.ejb.plugins.cmp.jdbc2.QueryCommand; 26 import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2; 27 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema; 28 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 29 import org.jboss.ejb.EntityEnterpriseContext; 30 import org.jboss.ejb.EntityContainer; 31 import org.jboss.ejb.GenericEntityObjectFactory; 32 33 import javax.ejb.FinderException ; 34 import javax.transaction.Transaction ; 35 import javax.transaction.TransactionManager ; 36 import java.lang.reflect.Method ; 37 import java.util.Collection ; 38 39 43 public class EJBSelectBridge 44 implements SelectorBridge 45 { 46 private final static byte SINGLE = 0; 47 private final static byte COLLECTION = 2; 48 49 private final JDBCQueryMetaData metadata; 50 private final QueryCommand command; 51 private final byte returnType; 52 private final Schema schema; 53 private boolean syncBeforeSelect; 54 private TransactionManager tm; 55 56 public EJBSelectBridge(EntityContainer container, Schema schema, JDBCQueryMetaData metadata, QueryCommand command) 57 { 58 this.schema = schema; 59 this.metadata = metadata; 60 this.command = command; 61 62 Class type = metadata.getMethod().getReturnType(); 63 if(Collection .class.isAssignableFrom(type)) 64 { 65 returnType = COLLECTION; 66 } 67 else 68 { 69 returnType = SINGLE; 70 } 71 72 tm = container.getTransactionManager(); 73 syncBeforeSelect = !container.getBeanMetaData().getContainerConfiguration().getSyncOnCommitOnly(); 74 } 75 76 78 public Object invoke(EntityEnterpriseContext instance, Method method, Object [] args) 79 throws Exception 80 { 81 Transaction tx = (instance != null ? instance.getTransaction() : tm.getTransaction()); 82 83 if(syncBeforeSelect) 84 { 85 EntityContainer.synchronizeEntitiesWithinTransaction(tx); 86 } 87 88 return execute(args); 89 } 90 91 93 public String getSelectorName() 94 { 95 return metadata.getMethod().getName(); 96 } 97 98 public Method getMethod() 99 { 100 return metadata.getMethod(); 101 } 102 103 public Object execute(Object [] args) throws FinderException 104 { 105 JDBCStoreManager2 manager = command.getStoreManager(); 106 GenericEntityObjectFactory factory = (metadata.isResultTypeMappingLocal() ? 107 (GenericEntityObjectFactory)manager.getContainer().getLocalProxyFactory() : manager.getContainer().getProxyFactory()); 108 109 Object result; 110 switch(returnType) 111 { 112 case SINGLE: 113 result = command.fetchOne(schema, factory, args); 114 if(result == null && getMethod().getReturnType().isPrimitive()) 115 { 116 throw new FinderException ( 117 "Cannot return null as a value of primitive type " + getMethod().getReturnType().getName() 118 ); 119 } 120 break; 121 case COLLECTION: 122 result = command.fetchCollection(schema, factory, args); 123 break; 124 default: 125 throw new IllegalStateException ("Unexpected return type: " + returnType); 126 } 127 return result; 128 } 129 } 130 | Popular Tags |