User: sg      
  Date: 00/07/11 02:41:01

  Modified:    src/main/org/jboss/ejb/plugins BMPPersistenceManager.java
  Log:
  Added support for BMP entity bean handling. Multiple and single finder methods 
implemented. load, store, activate, passivate and remove methods connected. Added 
extended ejb method checking summited by Gerolf Scherr. Thanks Gerolf!
  
  Revision  Changes    Path
  1.2       +141 -20   jboss/src/main/org/jboss/ejb/plugins/BMPPersistenceManager.java
  
  Index: BMPPersistenceManager.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/BMPPersistenceManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BMPPersistenceManager.java        2000/04/22 14:30:12     1.1
  +++ BMPPersistenceManager.java        2000/07/11 09:41:00     1.2
  @@ -11,6 +11,8 @@
   import java.rmi.RemoteException;
   import java.rmi.ServerException;
   import java.util.Collection;
  +import java.util.ArrayList;
  +import java.util.Enumeration;
   
   import javax.ejb.EntityBean;
   import javax.ejb.CreateException;
  @@ -25,7 +27,7 @@
    *      
    *   @see <related>
    *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @version $Revision: 1.1 $
  + *   @version $Revision: 1.2 $
    */
   public class BMPPersistenceManager
      implements EntityPersistenceManager
  @@ -39,7 +41,8 @@
      Method ejbStore;
      Method ejbActivate;
      Method ejbPassivate;
  -    
  +   Method ejbRemove;
  +  
      // Static --------------------------------------------------------
      
      // Constructors --------------------------------------------------
  @@ -57,6 +60,7 @@
         ejbStore = EntityBean.class.getMethod("ejbStore", new Class[0]);
         ejbActivate = EntityBean.class.getMethod("ejbActivate", new Class[0]);
         ejbPassivate = EntityBean.class.getMethod("ejbPassivate", new Class[0]);
  +      ejbRemove = EntityBean.class.getMethod("ejbRemove", new Class[0]);
      }
      
      public void start()
  @@ -77,26 +81,52 @@
         // Get methods
         try
         {
  -         Method createMethod = con.getBeanClass().getMethod("ejbCreate", 
m.getParameterTypes());
  -         Method postCreateMethod = con.getBeanClass().getMethod("ejbPostCreate", 
m.getParameterTypes());
  +        Method createMethod = null;
  +        Method postCreateMethod = null;
  +        
  +        // try to get the create method
  +        try {
  +          createMethod = con.getBeanClass().getMethod("ejbCreate", 
m.getParameterTypes());
  +        } catch (NoSuchMethodException nsme) {
  +          throw new CreateException("corresponding ejbCreate not found " + 
parametersToString(m.getParameterTypes()) + nsme);
  +        }
  +        
  +        // try to get the post create method
  +        try {
  +          postCreateMethod = con.getBeanClass().getMethod("ejbPostCreate", 
m.getParameterTypes());
  +        } catch (NoSuchMethodException nsme) {
  +          throw new CreateException("corresponding ejbPostCreate not found " + 
parametersToString(m.getParameterTypes()) + nsme);
  +        }
         
  -         // Call ejbCreate
  -         Object id = createMethod.invoke(ctx.getInstance(), args);
  -         ctx.setId(id);
  +        Object id = null;
  +        try {
  +          // Call ejbCreate
  +          id = createMethod.invoke(ctx.getInstance(), args);
  +        } catch (InvocationTargetException ite) {
  +          throw new CreateException("Create failed(could not call ejbCreate):"+ite);
  +        }
  +        
  +        // set the id
  +        ctx.setId(id);
            
  -         // Lock instance in cache
  -         con.getInstanceCache().insert(ctx);
  +        // Lock instance in cache
  +        con.getInstanceCache().insert(ctx);
            
  -         // Create EJBObject
  -         ctx.setEJBObject(con.getContainerInvoker().getEntityEJBObject(id));
  +        // Create EJBObject
  +        ctx.setEJBObject(con.getContainerInvoker().getEntityEJBObject(id));
   
  -         postCreateMethod.invoke(ctx.getInstance(), args);
  -      } catch (InvocationTargetException e)
  -      {
  -         throw new CreateException("Create failed:"+e);
  -      } catch (NoSuchMethodException e)
  -      {
  -         throw new CreateException("Create methods not found:"+e);
  +        try {
  +          postCreateMethod.invoke(ctx.getInstance(), args);
  +        } catch (InvocationTargetException ite) {
  +          throw new CreateException("Create failed(could not call ejbPostCreate):" 
+ ite);
  +        }
  +        
  +//      } catch (InvocationTargetException e)
  +//      {
  +//         throw new CreateException("Create failed:"+e);
  +//      } catch (NoSuchMethodException e)
  +//      {
  +//         throw new CreateException("Create methods not found:"+e);
         } catch (IllegalAccessException e)
         {
            throw new CreateException("Could not create entity:"+e);
  @@ -106,23 +136,66 @@
      public Object findEntity(Method finderMethod, Object[] args, 
EntityEnterpriseContext ctx)
         throws RemoteException
      {
  -      return null;
  +      // call the finder method
  +      Object result = callFinderMethod(finderMethod, args, ctx);
  +      
  +      // maybe we should check if the returned object is an instance of the
  +      // primary key class?
  +      return result;
      }
        
      public Collection findEntities(Method finderMethod, Object[] args, 
EntityEnterpriseContext ctx)
         throws RemoteException
      {
  -      return new java.util.ArrayList();
  +      // call the finder method
  +      Object result = callFinderMethod(finderMethod, args, ctx);
  +
  +      if (result == null) {
  +        // for EJB 1.0 compliance
  +        // if the bean couldn't find any matching entities
  +        // it returns null, so we return an empty collection
  +        return new ArrayList();
  +      }
  +      
  +      if (result instanceof java.util.Enumeration) {
  +        // to preserve 1.0 spec compatiblity
  +        ArrayList array = new ArrayList();
  +        Enumeration enum = (Enumeration) result;
  +        while (enum.hasMoreElements() == true) {
  +          array.add(enum.nextElement());
  +        }
  +        return array;
  +      } else if (result instanceof java.util.Collection) {
  +        return (Collection) result;
  +      } else {
  +        // so we received something that's not valid
  +        // throw an exception reporting it
  +        throw new RemoteException("result of finder method is not a valid return 
type: " + result.getClass());
  +      }
      }
   
      public void activateEntity(EntityEnterpriseContext ctx)
         throws RemoteException
      {
  +      try
  +      {
  +         ejbActivate.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (Exception e)
  +      {
  +         throw new ServerException("Activate failed", e);
  +      }
      }
      
      public void loadEntity(EntityEnterpriseContext ctx)
         throws RemoteException
      {
  +      try
  +      {
  +         ejbLoad.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (Exception e)
  +      {
  +         throw new ServerException("Load failed", e);
  +      }
      }
         
      public void storeEntity(EntityEnterpriseContext ctx)
  @@ -141,11 +214,25 @@
      public void passivateEntity(EntityEnterpriseContext ctx)
         throws RemoteException
      {
  +       try
  +      {
  +         ejbPassivate.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (Exception e)
  +      {
  +         throw new ServerException("Passivate failed", e);
  +      }
      }
         
      public void removeEntity(EntityEnterpriseContext ctx)
         throws RemoteException
      {
  +      try
  +      {
  +         ejbRemove.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (Exception e)
  +      {
  +         throw new ServerException("Remove failed", e);
  +      }
      }
      // Z implementation ----------------------------------------------
       
  @@ -154,6 +241,40 @@
      // Protected -----------------------------------------------------
       
      // Private -------------------------------------------------------
  +   private Object callFinderMethod(Method finderMethod, Object[] args, 
EntityEnterpriseContext ctx) throws RemoteException {
  +      // get the finder method
  +      Method callMethod = null;
  +      try {
  +        callMethod = getFinderMethod(con.getBeanClass(), finderMethod, args);
  +      } catch (NoSuchMethodException me) {
  +        // debug
  +        me.printStackTrace();
  +        throw new RemoteException("couldn't find finder method in bean class. " + 
me.toString());
  +      }
  +
  +      // invoke the finder method
  +      Object result = null;
  +      try {
  +        result = callMethod.invoke(ctx.getInstance(), args);
  +      } catch (Exception e) {
  +        // debug
  +        e.printStackTrace();
  +        throw new RemoteException("exception occured while invoking finder method:" 
+ e.toString());
  +      }
  +      
  +      return result;
  +   }
  +
  +   private Method getFinderMethod(Class beanClass, Method finderMethod, Object[] 
args) throws NoSuchMethodException {
  +     String methodName = "ejbF" + finderMethod.getName().substring(1);
  +     return beanClass.getDeclaredMethod(methodName, 
finderMethod.getParameterTypes());
  +   }
  +
  +   private String parametersToString(Object []a) {
  +        String r = new String();
  +        for(int i=0;i<a.length;i++) r = r + ", " + a[i];
  +        return r;
  +   }
   
      // Inner classes -------------------------------------------------
   }
  
  
  

Reply via email to