1 22 package org.jboss.ejb.plugins.cmp.jdbc.bridge; 23 24 import java.lang.reflect.Method ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 import javax.ejb.EJBException ; 29 import javax.ejb.FinderException ; 30 import javax.ejb.ObjectNotFoundException ; 31 import javax.transaction.Transaction ; 32 import javax.transaction.TransactionManager ; 33 34 import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge; 35 import org.jboss.ejb.plugins.cmp.jdbc.JDBCQueryCommand; 36 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager; 37 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 38 import org.jboss.ejb.EntityEnterpriseContext; 39 import org.jboss.ejb.EntityContainer; 40 import org.jboss.ejb.GenericEntityObjectFactory; 41 42 54 public class JDBCSelectorBridge implements SelectorBridge 55 { 56 private final JDBCQueryMetaData queryMetaData; 57 private final JDBCStoreManager manager; 58 private TransactionManager tm; 59 private boolean syncBeforeSelect; 60 61 public JDBCSelectorBridge(JDBCStoreManager manager, JDBCQueryMetaData queryMetaData) 62 { 63 this.queryMetaData = queryMetaData; 64 this.manager = manager; 65 66 EntityContainer container = manager.getContainer(); 67 tm = container.getTransactionManager(); 68 syncBeforeSelect = !container.getBeanMetaData().getContainerConfiguration().getSyncOnCommitOnly(); 69 } 70 71 73 public Object invoke(EntityEnterpriseContext ctx, Method method, Object [] args) 74 throws Exception 75 { 76 Transaction tx = (ctx != null ? ctx.getTransaction() : tm.getTransaction()); 77 78 if(syncBeforeSelect) 79 { 80 EntityContainer.synchronizeEntitiesWithinTransaction(tx); 81 } 82 83 return execute(args); 84 } 85 86 88 public String getSelectorName() 89 { 90 return queryMetaData.getMethod().getName(); 91 } 92 93 public Method getMethod() 94 { 95 return queryMetaData.getMethod(); 96 } 97 98 private Class getReturnType() 99 { 100 return queryMetaData.getMethod().getReturnType(); 101 } 102 103 public Object execute(Object [] args) throws FinderException 104 { 105 Collection retVal; 106 Method method = getMethod(); 107 try 108 { 109 JDBCQueryCommand query = manager.getQueryManager().getQueryCommand(method); 110 GenericEntityObjectFactory factory = (queryMetaData.isResultTypeMappingLocal() ? 111 (GenericEntityObjectFactory)query.getSelectManager().getContainer().getLocalProxyFactory() : 112 query.getSelectManager().getContainer().getProxyFactory()); 113 retVal = query.execute(method, args, null, factory); 114 } 115 catch(FinderException e) 116 { 117 throw e; 118 } 119 catch(EJBException e) 120 { 121 throw e; 122 } 123 catch(Exception e) 124 { 125 throw new EJBException ("Error in " + getSelectorName(), e); 126 } 127 128 if(!Collection .class.isAssignableFrom(getReturnType())) 129 { 130 if(retVal.size() == 0) 132 { 133 throw new ObjectNotFoundException (); 134 } 135 if(retVal.size() > 1) 136 { 137 throw new FinderException (getSelectorName() + 138 " returned " + retVal.size() + " objects"); 139 } 140 141 Object o = retVal.iterator().next(); 142 if(o == null && method.getReturnType().isPrimitive()) 143 { 144 throw new FinderException ( 145 "Cannot return null as a value of primitive type " + method.getReturnType().getName() 146 ); 147 } 148 149 return o; 150 } 151 else 152 { 153 if(Set .class.isAssignableFrom(getReturnType())) 155 { 156 return new HashSet (retVal); 157 } 158 else 159 { 160 return retVal; 161 } 162 } 163 } 164 } 165 | Popular Tags |