Java Source Code: org.jboss.test.cmp2.passivation.ejb.RapidlyPassivatedEntityBean


   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.test.cmp2.passivation.ejb;
   9: 
  10: import java.rmi.RemoteException;
  11: 
  12: import javax.ejb.CreateException;
  13: import javax.ejb.EJBException;
  14: import javax.ejb.EntityBean;
  15: import javax.ejb.EntityContext;
  16: import javax.ejb.RemoveException;
  17: import javax.ejb.EJBObject;
  18: import javax.ejb.EJBLocalObject;
  19: 
  20: import org.jboss.logging.Logger;
  21: 
  22: /**
  23:  * An entity bean to test the entity activation/passivation mechanism
  24:  * provided by JBoss.
  25:  * 
  26:  * It is associated with a container configuration which causes it to be passivated shortly
  27:  * after it has been accessed.
  28:  * 
  29:  * It has been designed to expose the bug described at
  30:  * <a href="https://sourceforge.net/tracker/?group_id=22866&atid=376685&func=detail&aid=742197">
  31:  *    Detail:769139 entityCtx.getEJBLocalObject() returns wrong instance</a>
  32:  * and
  33:  * <a href="https://sourceforge.net/tracker/?func=detail&atid=376685&aid=769139&group_id=22866">
  34:  *    Detail:742197 getEJBLocalObject()  bug</a>
  35:  *
  36:  * @ejb.bean
  37:  *    name="RapidlyPassivatedEntity"
  38:  *    jndi-name="ejb/remote/RapidlyPassivatedEntity"
  39:  *    local-jndi-name="ejb/local/RapidlyPassivatedEntity"
  40:  *    view-type="both"
  41:  *    type="CMP"
  42:  *    reentrant="false"
  43:  *    cmp-version="2.x"
  44:  * @ejb.pk
  45:  *    class="java.lang.Object"
  46:  * @jboss.create-table "true"
  47:  * @jboss.remove-table "true"
  48:  * @jboss.unknown-pk
  49:  *    class="java.lang.String"
  50:  * @jboss.entity-command
  51:  *    name="key-generator"
  52:  * @jboss.container-configuration
  53:  *    name="Short lived CMP 2.0 Entity Bean"
  54:  *
  55:  * @author <a href="mailto:steve@resolvesw.com">Steve Coy</a>
  56:  */
  57: public abstract class RapidlyPassivatedEntityBean implements EntityBean
  58:	  {
  59:   // Attributes ----------------------------------------------------
  60:
  61:   private static Logger   log = Logger.getLogger(RapidlyPassivatedEntityBean.class);
  62:
  63:   private EntityContext   mEntityContext;
  64:
  65:   
  66:   // Entity Attributes ----------------------------------------------------
  67:
  68:   /**
  69:    * @ejb.persistent-field
  70:    * @ejb.interface-method
  71:    */
  72:   public abstract String getData(); 
  73:   public abstract void setData(String data);
  74:   
  75:
  76:   // Business Methods ----------------------------------------------------
  77:
  78:   /**
  79:    * Return the pk of the object returned by {@link EntityContext#getEJBLocalObject}
  80:    * @ejb.interface-method
  81:    */
  82:   public Object getIdViaEJBLocalObject()
  83:	     {
  84:      Object key = mEntityContext.getPrimaryKey();
  85:      EJBLocalObject local = mEntityContext.getEJBLocalObject();
  86:      Object lkey = local.getPrimaryKey();
  87:      log.info("key: "+key+", lkey: "+lkey+", local: "+local);
  88:      return (Object)mEntityContext.getEJBLocalObject().getPrimaryKey();
  89:   }
  90:  
  91:
  92:   /**
  93:    * Return the pk of the object returned by {@link EntityContext#getEJBObject}
  94:    * @ejb.interface-method
  95:    */
  96:   public Object getIdViaEJBObject()
  97:	     {
  98:      try
  99:	        {
 100:         return (Object)mEntityContext.getEJBObject().getPrimaryKey();
 101:      }
 102:      catch (RemoteException e)
 103:	        {
 104:         throw new EJBException(e);
 105:      }
 106:   }
 107:  
 108:
 109:   // EJB Implementation ----------------------------------------------------
 110:
 111:   /**
 112:    * @ejb.create-method
 113:    */
 114:   public Object ejbCreate(String s)
 115:      throws CreateException
 116:	     {
 117:      setData(s);
 118:      return null;   // as required by CMP 2.0 spec
 119:   }
 120:   
 121:   public void ejbPostCreate(String s)
 122:	     {
 123:      log.info("ejbPostCreate, ctx:"+mEntityContext
 124:         +", pk:"+mEntityContext.getPrimaryKey()
 125:         +", local:"+mEntityContext.getEJBLocalObject());
 126:   }
 127:   
 128:  
 129:   public void ejbActivate() throws EJBException, RemoteException
 130:	     {
 131:      log.info("ejbActivate, ctx:"+mEntityContext
 132:         +", pk:"+mEntityContext.getPrimaryKey()
 133:         +", local:"+mEntityContext.getEJBLocalObject());
 134:   }
 135:
 136:   public void ejbLoad() throws EJBException, RemoteException
 137:	     {
 138:      log.info("ejbLoad, ctx:"+mEntityContext
 139:         +", pk:"+mEntityContext.getPrimaryKey()
 140:         +", local:"+mEntityContext.getEJBLocalObject());
 141:   }
 142:
 143:   public void ejbPassivate() throws EJBException, RemoteException
 144:	     {
 145:      log.info("ejbPassivate, ctx:"+mEntityContext
 146:         +", pk:"+mEntityContext.getPrimaryKey()
 147:         +", local:"+mEntityContext.getEJBLocalObject());
 148:   }
 149:
 150:   public void ejbRemove()
 151:      throws RemoveException, EJBException, RemoteException
 152:	     {
 153:   }
 154:
 155:   public void ejbStore() throws EJBException, RemoteException
 156:	     {
 157:   }
 158:
 159:   public void setEntityContext(EntityContext ctx)
 160:      throws EJBException, RemoteException
 161:	     {
 162:      mEntityContext = ctx;
 163:   }
 164:
 165:   public void unsetEntityContext() throws EJBException, RemoteException
 166:	     {
 167:      mEntityContext = null;
 168:   }
 169:
 170:}