org.objectweb.jonas_ejb.container
Class JEntityFactory

java.lang.Object
  extended byorg.objectweb.jonas_ejb.container.JFactory
      extended byorg.objectweb.jonas_ejb.container.JEntityFactory
All Implemented Interfaces:
BeanFactory, javax.ejb.TimerService
Direct Known Subclasses:
JormFactory

public class JEntityFactory
extends JFactory
implements javax.ejb.TimerService

This class is a factory for an Entity Bean. It is responsible for - managing Home and LocalHome. - managing a pool of instances/contexts - keeping the list of PKs and the associated JEntitySwitch's - keeping the JNDI context for this component (java:comp/env)

Author:
Philippe Coq, Philippe Durieux

Field Summary
protected  java.util.List bctxlist
          instance pool management (list of available JEntityContext objects)
protected  java.lang.Object datasource
           
protected  JEntityHome home
          optional home
protected  int inactivityTimeout
           
protected  int instanceCount
           
protected  JEntityLocalHome localhome
          optional local home
protected  int lockPolicy
          lock policy
protected  int maxCacheSize
           
protected  int minPoolSize
          initial value for pool size
protected  java.util.HashMap pklist
          List of JEntitySwitch objects At each PK is associated a JEntitySwitch object
protected  boolean prefetch
          enable the prefetch for CMP2 bean
protected  boolean reentrant
          reentrant if instance can be used concurrently
protected  boolean shared
          shared if the EJB container is not the only one to modify the bean state on the database, or if a cluster of container access the bean concurrently.
protected  java.util.HashMap txlist
          List of Transaction Listeners.
 
Fields inherited from class org.objectweb.jonas_ejb.container.JFactory
beanclass, cont, dd, ejb10Env, ejbname, ejbTimeoutSignature, JNDICtx, myTimerService, naming, timerTxAttr, tm, txbeanmanaged, wm
 
Constructor Summary
JEntityFactory()
          constructor must be without parameters (required by Jorm)
 
Method Summary
 void bindEJB(java.lang.Object pk, JEntitySwitch bs)
          Bind a PK with a JEntitySwitch
 int calculateAutomaticPk()
          Calculate a new uid for automatic pk creation Used by JEntityCmpJdbc.vm
 void checkTransaction(RequestCtx rctx)
          Check Transaction before calling a method on a bean.
protected  JEntityContext createNewContext(javax.ejb.EntityBean bean)
           
protected  JEntityContext createNewInstance(JEntitySwitch es)
          Create a new instance of the bean and its EntityContext In case of CMP, the bean class is derived to manage entity persistence.
 javax.ejb.Timer createTimer(java.util.Date arg0, long arg1, java.io.Serializable arg2)
           
 javax.ejb.Timer createTimer(java.util.Date arg0, java.io.Serializable arg1)
           
 javax.ejb.Timer createTimer(long arg0, long arg1, java.io.Serializable arg2)
           
 javax.ejb.Timer createTimer(long arg0, java.io.Serializable arg1)
           
 java.io.Serializable decodePK(java.io.Serializable strpk)
          Decode PK.
 boolean dirtyInstances()
           
 void dummyFinderException(boolean dummy)
          Dummy method that defines the FinderException in the throws clause to can catch this exception in any case in the JentityHome.vm
 java.io.Serializable encodePK(java.io.Serializable pk)
          Encode PK.
 JEntitySwitch existEJB(java.lang.Object pk, JEntitySwitch bs)
          get EJB by its PK.
 int getCacheSize()
           
 java.lang.Object getDataSource()
           
 JEntitySwitch getEJB(java.lang.Object pk)
          get EJB by its PK Creates if not exist yet.
 EntityCounters getEntityCounters()
          Take a dump of current entity counters and return them
 JHome getHome()
          returns the home if exist or null if not
 int getInactivityTimeout()
           
 JEntityContext getJContext(JEntitySwitch es)
          Get a Context from the pool, or create a new one if no more available in the pool.
 JEntitySwitch getJEntitySwitch()
          This method allocates a new JEntitySwitch.
 JLocalHome getLocalHome()
          returns the local home if exist or null if not
 int getMaxCacheSize()
           
 int getMinPoolSize()
           
 int getPoolSize()
          Get the size of the instance pool for this bean
 java.util.Collection getTimers()
           
 javax.ejb.TimerService getTimerService()
          Obtains the TimerService associated for this Bean
 void init(EntityDesc dd, JContainer cont)
          Init this object
 void initInstancePool()
          Init the pool of instances
 boolean isBlocked(javax.transaction.Transaction tx)
          Return True if this Tx is blocked waiting an instance
 boolean isBlocking(javax.transaction.Transaction tx)
          Return True if this Tx is blocking an instance
 boolean isPrefetch()
           
 boolean isReentrant()
           
 boolean isShared()
           
 int lockPolicy()
           
 void printAllPks()
          print the list of PKs for DEBUG only
 boolean rebindEJB(javax.transaction.Transaction tx, JEntityContext bctx, java.lang.Object pk)
          rebind a PK with a JEntitySwitch (called by create methods)
 void reduceCache()
          Reduce number of instances in memory
 boolean registerContext(javax.transaction.Transaction tx, JEntityContext ec)
          Register a Context/Instance in the transaction.
 void registerEJB(JEntitySwitch ejb)
          Register an EntitySwitch in the dirty list.
 void releaseJContext(JContext ctx)
          Release a Context
 void removeEJB(java.lang.Object pk)
          remove an EJB by its PK
 void removeTxListener(javax.transaction.Transaction tx)
          Remove a Transaction Listener from the list.
 void stop()
          stop this EJB.
 void sync()
          Synchronize all dirty instances
 void syncForFind(javax.transaction.Transaction tx)
          synchronize data modified in this transaction.
 void synchronizeEntities()
          Ask swapper to synchronize all dirty EntitySwitch
 
Methods inherited from class org.objectweb.jonas_ejb.container.JFactory
checkJonasVersion, checkSecurity, checkTransactionContainer, getContainer, getDeploymentDescriptor, getEjb10Environment, getEJBName, getEjbTimeoutSignature, getEnv, getInitialContext, getTimerTxAttribute, getTransactionManager, getWorkManager, init, isClassAvailable, isTxBeanManaged, myClassLoader, postInvoke, postInvokeRemote, preInvoke, preInvokeRemote, resetComponentContext, setComponentContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

home

protected JEntityHome home
optional home


localhome

protected JEntityLocalHome localhome
optional local home


reentrant

protected boolean reentrant
reentrant if instance can be used concurrently


shared

protected boolean shared
shared if the EJB container is not the only one to modify the bean state on the database, or if a cluster of container access the bean concurrently.


lockPolicy

protected int lockPolicy
lock policy


prefetch

protected boolean prefetch
enable the prefetch for CMP2 bean


bctxlist

protected java.util.List bctxlist
instance pool management (list of available JEntityContext objects)


minPoolSize

protected int minPoolSize
initial value for pool size


instanceCount

protected int instanceCount

maxCacheSize

protected int maxCacheSize

inactivityTimeout

protected int inactivityTimeout

pklist

protected java.util.HashMap pklist
List of JEntitySwitch objects At each PK is associated a JEntitySwitch object


txlist

protected java.util.HashMap txlist
List of Transaction Listeners. At each Transaction is associated a TxListener object


datasource

protected java.lang.Object datasource
Constructor Detail

JEntityFactory

public JEntityFactory()
constructor must be without parameters (required by Jorm)

Method Detail

init

public void init(EntityDesc dd,
                 JContainer cont)
Init this object

Parameters:
dd - the deployment descriptor
cont - the Container

initInstancePool

public void initInstancePool()
Init the pool of instances

Specified by:
initInstancePool in interface BeanFactory
Specified by:
initInstancePool in class JFactory

getEJB

public JEntitySwitch getEJB(java.lang.Object pk)
get EJB by its PK Creates if not exist yet.

Parameters:
pk - The Primary Key Object
Returns:
The JEntitySwitch matching the PK.

existEJB

public JEntitySwitch existEJB(java.lang.Object pk,
                              JEntitySwitch bs)
get EJB by its PK. If not exist yet, map the EntitySwitch to the PK.

Parameters:
pk - The Primary Key Object
bs - the Entityswitch
Returns:
The JEntitySwitch matching the PK, or null if none exist.

rebindEJB

public boolean rebindEJB(javax.transaction.Transaction tx,
                         JEntityContext bctx,
                         java.lang.Object pk)
rebind a PK with a JEntitySwitch (called by create methods)

Parameters:
tx - current Transaction
bctx - The EntityContext
pk - The Primary Key Object
Returns:
true if bs has been added to the PK list.

bindEJB

public void bindEJB(java.lang.Object pk,
                    JEntitySwitch bs)
Bind a PK with a JEntitySwitch

Parameters:
pk - The Primary Key Object
bs - The JEntitySwitch

getJEntitySwitch

public JEntitySwitch getJEntitySwitch()
This method allocates a new JEntitySwitch. But no association has been done between the primary key and the new JEntitySwitch. Therefore the initialisation is not done.

Returns:
The JEntitySwitch.

removeEJB

public void removeEJB(java.lang.Object pk)
remove an EJB by its PK

Parameters:
pk - The Primary Key Object

registerEJB

public void registerEJB(JEntitySwitch ejb)
Register an EntitySwitch in the dirty list. should be called each time a new instance is modified outside transaction

Parameters:
ejb - The Entity Switch to be registered

synchronizeEntities

public void synchronizeEntities()
Ask swapper to synchronize all dirty EntitySwitch


dirtyInstances

public boolean dirtyInstances()
Returns:
true if dirty list is not empty

getInactivityTimeout

public int getInactivityTimeout()
Returns:
inactivity timeout in sec.

getPoolSize

public int getPoolSize()
Description copied from interface: BeanFactory
Get the size of the instance pool for this bean

Specified by:
getPoolSize in interface BeanFactory
Specified by:
getPoolSize in class JFactory
Returns:
the Instance pool size for this Ejb

stop

public void stop()
stop this EJB. Mainly unregister it in JNDI.

Specified by:
stop in interface BeanFactory

sync

public void sync()
Synchronize all dirty instances

Specified by:
sync in interface BeanFactory

reduceCache

public void reduceCache()
Reduce number of instances in memory

Specified by:
reduceCache in interface BeanFactory

getHome

public JHome getHome()
Description copied from interface: BeanFactory
returns the home if exist or null if not

Specified by:
getHome in interface BeanFactory
Returns:
the home if it exists

getLocalHome

public JLocalHome getLocalHome()
Description copied from interface: BeanFactory
returns the local home if exist or null if not

Specified by:
getLocalHome in interface BeanFactory
Returns:
the local home if it exists

getJContext

public JEntityContext getJContext(JEntitySwitch es)
Get a Context from the pool, or create a new one if no more available in the pool. This JContext must be initialized then by the caller.

Returns:
a JEntityContext, not initialized.

releaseJContext

public void releaseJContext(JContext ctx)
Release a Context

Parameters:
ctx - - The JContext to release.

getTimerService

public javax.ejb.TimerService getTimerService()
Obtains the TimerService associated for this Bean

Specified by:
getTimerService in class JFactory
Returns:
a JTimerService instance.

getMinPoolSize

public int getMinPoolSize()
Returns:
min pool size for Jmx

getMaxCacheSize

public int getMaxCacheSize()
Returns:
max cache size for Jmx

getCacheSize

public int getCacheSize()
Returns:
current cache size for Jmx

isReentrant

public boolean isReentrant()
Returns:
true if reentrant

isShared

public boolean isShared()
Returns:
true if shared

lockPolicy

public int lockPolicy()
Returns:
lockPolicy

isPrefetch

public boolean isPrefetch()
Returns:
true if prefetch enable

getDataSource

public java.lang.Object getDataSource()
Returns:
the Datasource used for CMP

checkTransaction

public void checkTransaction(RequestCtx rctx)
Check Transaction before calling a method on a bean. For Entity beans, the only possible case is "Container Managed Tx"

Parameters:
rctx - The Request Context

syncForFind

public void syncForFind(javax.transaction.Transaction tx)
synchronize data modified in this transaction. this is necessary in case of finder method, because ejb-ql looks for on disk.

Parameters:
tx - the Transaction

isBlocking

public boolean isBlocking(javax.transaction.Transaction tx)
Return True if this Tx is blocking an instance


isBlocked

public boolean isBlocked(javax.transaction.Transaction tx)
Return True if this Tx is blocked waiting an instance


removeTxListener

public void removeTxListener(javax.transaction.Transaction tx)
Remove a Transaction Listener from the list.

Parameters:
tx - the Transaction to remove

registerContext

public boolean registerContext(javax.transaction.Transaction tx,
                               JEntityContext ec)
                        throws java.lang.IllegalStateException
Register a Context/Instance in the transaction.

Parameters:
tx - current Transaction
Returns:
true if instance has been registered.
Throws:
java.lang.IllegalStateException

getEntityCounters

public EntityCounters getEntityCounters()
Take a dump of current entity counters and return them

Returns:
EntityCounters

calculateAutomaticPk

public int calculateAutomaticPk()
Calculate a new uid for automatic pk creation Used by JEntityCmpJdbc.vm

Returns:
int (unique pk)

dummyFinderException

public void dummyFinderException(boolean dummy)
                          throws javax.ejb.FinderException
Dummy method that defines the FinderException in the throws clause to can catch this exception in any case in the JentityHome.vm

Parameters:
dummy - if true do nothing, else throw the FinderException
Throws:
javax.ejb.FinderException - if dummy is false

printAllPks

public void printAllPks()
print the list of PKs for DEBUG only


createNewInstance

protected JEntityContext createNewInstance(JEntitySwitch es)
                                    throws java.lang.Exception
Create a new instance of the bean and its EntityContext In case of CMP, the bean class is derived to manage entity persistence.

Returns:
JEntityContext
Throws:
java.lang.Exception - cannot instantiate bean

createNewContext

protected JEntityContext createNewContext(javax.ejb.EntityBean bean)

encodePK

public java.io.Serializable encodePK(java.io.Serializable pk)
Encode PK. This does nothing, except in case of CMP2

Returns:
String representation of the PK

decodePK

public java.io.Serializable decodePK(java.io.Serializable strpk)
Decode PK. This does nothing, except in case of CMP2

Returns:
PK matching the String

createTimer

public javax.ejb.Timer createTimer(long arg0,
                                   java.io.Serializable arg1)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException

createTimer

public javax.ejb.Timer createTimer(long arg0,
                                   long arg1,
                                   java.io.Serializable arg2)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException

createTimer

public javax.ejb.Timer createTimer(java.util.Date arg0,
                                   java.io.Serializable arg1)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException

createTimer

public javax.ejb.Timer createTimer(java.util.Date arg0,
                                   long arg1,
                                   java.io.Serializable arg2)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException

getTimers

public java.util.Collection getTimers()
                               throws java.lang.IllegalStateException,
                                      javax.ejb.EJBException
Specified by:
getTimers in interface javax.ejb.TimerService
Throws:
java.lang.IllegalStateException
javax.ejb.EJBException