Java Source Code: org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCSelectorBridge


   1: /*
   2:  * JBoss, the OpenSource J2EE webOS
   3:  *
   4:  * Distributable under LGPL license.
   5:  * See terms of license at gnu.org.
   6:  */
   7: 
   8: package org.jboss.ejb.plugins.cmp.jdbc.bridge;
   9: 
  10: import java.lang.reflect.Method;
  11: import java.util.ArrayList;
  12: import java.util.Collection;
  13: import java.util.HashSet;
  14: import java.util.Set;
  15: import javax.ejb.EJBException;
  16: import javax.ejb.FinderException;
  17: import javax.ejb.ObjectNotFoundException;
  18: import javax.transaction.Transaction;
  19: import javax.transaction.TransactionManager;
  20: 
  21: import org.jboss.ejb.plugins.cmp.bridge.SelectorBridge;
  22: import org.jboss.ejb.plugins.cmp.jdbc.JDBCQueryCommand;
  23: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
  24: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
  25: import org.jboss.ejb.EntityEnterpriseContext;
  26: import org.jboss.ejb.EntityContainer;
  27: 
  28: /**
  29:  * JDBCSelectorBridge represents one ejbSelect method.
  30:  * <p/>
  31:  * Life-cycle:
  32:  * Tied to the EntityBridge.
  33:  * <p/>
  34:  * Multiplicity:
  35:  * One for each entity bean ejbSelect method.
  36:  *
  37:  * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
  38:  * @version $Revision: 1.7.4.2 $
  39:  */
  40: public class JDBCSelectorBridge implements SelectorBridge
  41:	  {
  42:   private final JDBCQueryMetaData queryMetaData;
  43:   private final JDBCStoreManager manager;
  44:   private TransactionManager tm;
  45:   private boolean syncBeforeSelect;
  46:
  47:   public JDBCSelectorBridge(JDBCStoreManager manager, JDBCQueryMetaData queryMetaData)
  48:	     {
  49:      this.queryMetaData = queryMetaData;
  50:      this.manager = manager;
  51:
  52:      EntityContainer container = manager.getContainer();
  53:      tm = container.getTransactionManager();
  54:      syncBeforeSelect = !container.getBeanMetaData().getContainerConfiguration().getSyncOnCommitOnly();
  55:   }
  56:
  57:   // BridgeInvoker implementation
  58:
  59:   public Object invoke(EntityEnterpriseContext ctx, Method method, Object[] args)
  60:      throws Exception
  61:	     {
  62:      Transaction tx = (ctx != null ? ctx.getTransaction() : tm.getTransaction());
  63:
  64:      if(syncBeforeSelect)
  65:	        {
  66:         EntityContainer.synchronizeEntitiesWithinTransaction(tx);
  67:      }
  68:
  69:      return execute(args);
  70:   }
  71:
  72:   // SelectorBridge implementation
  73:
  74:   public String getSelectorName()
  75:	     {
  76:      return queryMetaData.getMethod().getName();
  77:   }
  78:
  79:   public Method getMethod()
  80:	     {
  81:      return queryMetaData.getMethod();
  82:   }
  83:
  84:   private Class getReturnType()
  85:	     {
  86:      return queryMetaData.getMethod().getReturnType();
  87:   }
  88:
  89:   public Object execute(Object[] args) throws FinderException
  90:	     {
  91:      Collection retVal;
  92:      try
  93:	        {
  94:         JDBCQueryCommand query =
  95:            manager.getQueryManager().getQueryCommand(getMethod());
  96:         retVal = query.execute(getMethod(), args, null);
  97:      }
  98:      catch(FinderException e)
  99:	        {
 100:         throw e;
 101:      }
 102:      catch(EJBException e)
 103:	        {
 104:         throw e;
 105:      }
 106:      catch(Exception e)
 107:	        {
 108:         throw new EJBException("Error in " + getSelectorName(), e);
 109:      }
 110:
 111:      if(!Collection.class.isAssignableFrom(getReturnType()))
 112:	        {
 113:         // single object
 114:         if(retVal.size() == 0)
 115:	           {
 116:            throw new ObjectNotFoundException();
 117:         }
 118:         if(retVal.size() > 1)
 119:	           {
 120:            throw new FinderException(getSelectorName() +
 121:               " returned " + retVal.size() + " objects");
 122:         }
 123:         return retVal.iterator().next();
 124:      }
 125:      else
 126:	        {
 127:         // collection or set
 128:         if(Set.class.isAssignableFrom(getReturnType()))
 129:	           {
 130:            return new HashSet(retVal);
 131:         }
 132:         else
 133:	           {
 134:            return new ArrayList(retVal);
 135:         }
 136:      }
 137:   }
 138:}