org.objectweb.jonas_ejb.container
Class JEntityFactory

java.lang.Object
  extended by org.objectweb.jonas_ejb.container.JFactory
      extended by org.objectweb.jonas_ejb.container.JEntityFactory
All Implemented Interfaces:
javax.ejb.TimerService, BeanFactory
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
          freelist of JEntityContext's
protected  boolean cmp2
          True if CMP2 container
protected  java.lang.Object datasource
          Datasource in case of CMP
protected  boolean hardLimit
          True if cannot overtake max-cache-size
protected  JEntityHome home
          optional home
protected  int instanceCount
          Current number of instances in memory
protected  JEntityLocalHome localhome
          optional local home
protected  int lockPolicy
          lock policy
protected  boolean prefetch
          enable the prefetch for CMP2 bean
protected  int readTimeout
          Number of seconds before reading again instances for read-only.
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, isClusterReplicated, JNDICtx, maxCacheSize, minPoolSize, myTimerService, naming, passivationDir, stopped, 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)
          Illegal operation: timers are associated with a PK
 javax.ejb.Timer createTimer(java.util.Date arg0, java.io.Serializable arg1)
          Illegal operation: timers are associated with a PK
 javax.ejb.Timer createTimer(long arg0, long arg1, java.io.Serializable arg2)
          Illegal operation: timers are associated with a PK /* (non-Javadoc)
 javax.ejb.Timer createTimer(long arg0, java.io.Serializable arg1)
          Illegal operation: timers are associated with a PK
 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()
           
 int getCurrentWaiters()
           
 java.lang.Object getDataSource()
           
 int getDeadlockTimeout()
           
 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 getLockPolicy()
           
 int getMaxWaitTime()
           
 int getPassivationTimeout()
           
 int getPoolSize()
          Get the size of the instance pool for this bean
 int getReadTimeout()
           
 boolean getSelectForUpdate()
           
 java.util.Collection getTimers()
          Illegal operation: timers are associated with a PK
 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 isCMP2()
           
 boolean isDeadLocked(javax.transaction.Transaction suspect)
          Search if this transaction is blocked in a deadlock
 boolean isHardLimit()
           
 boolean isPrefetch()
           
 boolean isReentrant()
           
 boolean isShared()
           
 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 passivate all instances that are not busy and not used very recently
 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 [deprecated]
 void releaseJContext(JContext ctx, int poolaction)
          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 setDeadlockTimeout(int i)
          Set the deadlock timeout (jonas admin) Not used today
 void setInactivityTimeout(int i)
          Set the inactivity timeout (jonas admin) Not used today
 void setMaxWaitTime(int sec)
           
 void setPassivationTimeout(int i)
          Set the passivation timeout (jonas admin) Not used today
 void stop()
          stop this EJB.
 void storeInstances(javax.transaction.Transaction tx)
          For storing entities modified in tx
 void syncDirty(boolean alwaysStore)
          Synchronize all dirty instances Does nothing if not CS policy.
 void syncForFind(javax.transaction.Transaction tx)
          synchronize data modified in this transaction.
 void syncForSelect()
          synchronize data modified in the current transaction.
 void synchronizeEntities()
          Ask swapper to synchronize all dirty EntitySwitch Only used for container-serialized policy
 boolean tooManyInstances()
           
 void unregisterContext(javax.transaction.Transaction tx, JEntityContext ec)
          Unregister a Context/Instance in the transaction.
 
Methods inherited from class org.objectweb.jonas_ejb.container.JFactory
checkJonasVersion, checkSecurity, checkTransactionContainer, getContainer, getDeploymentDescriptor, getEjb10Environment, getEJBName, getEjbTimeoutSignature, getEnv, getInitialContext, getMaxCacheSize, getMinPoolSize, getPassivationDir, getTimerTxAttribute, getTransactionManager, getWorkManager, init, isClassAvailable, isStopped, isTxBeanManaged, myClassLoader, postInvoke, postInvokeRemote, preInvoke, preInvokeRemote, resetComponentContext, restartTimers, setComponentContext, stopContainer
 
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


lockPolicy

protected int lockPolicy
lock policy


prefetch

protected boolean prefetch
enable the prefetch for CMP2 bean


cmp2

protected boolean cmp2
True if CMP2 container


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.


reentrant

protected boolean reentrant
reentrant if instance can be used concurrently


readTimeout

protected int readTimeout
Number of seconds before reading again instances for read-only.


datasource

protected java.lang.Object datasource
Datasource in case of CMP


bctxlist

protected java.util.List bctxlist
freelist of JEntityContext's


instanceCount

protected int instanceCount
Current number of instances in memory


hardLimit

protected boolean hardLimit
True if cannot overtake max-cache-size


txlist

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

Constructor Detail

JEntityFactory

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

Method Detail

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

getLockPolicy

public int getLockPolicy()
Returns:
lockPolicy

isPrefetch

public boolean isPrefetch()
Returns:
true if prefetch enable

isCMP2

public boolean isCMP2()
Returns:
true if CMP2 container

isShared

public boolean isShared()
Returns:
true if shared

isReentrant

public boolean isReentrant()
Returns:
true if reentrant

getSelectForUpdate

public boolean getSelectForUpdate()
Returns:
true if the bean is shared

getReadTimeout

public int getReadTimeout()
Returns:
read timeout in sec.

getInactivityTimeout

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

setInactivityTimeout

public void setInactivityTimeout(int i)
Set the inactivity timeout (jonas admin) Not used today


getDeadlockTimeout

public int getDeadlockTimeout()
Returns:
deadlock timeout in sec.

setDeadlockTimeout

public void setDeadlockTimeout(int i)
Set the deadlock timeout (jonas admin) Not used today


getPassivationTimeout

public int getPassivationTimeout()
Returns:
passivation timeout in sec.

setPassivationTimeout

public void setPassivationTimeout(int i)
Set the passivation timeout (jonas admin) Not used today


getDataSource

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

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

init

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

Parameters:
dd - the deployment descriptor
cont - the Container

stop

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

Specified by:
stop in interface BeanFactory

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

getCacheSize

public int getCacheSize()
Overrides:
getCacheSize in class JFactory
Returns:
current cache size

tooManyInstances

public boolean tooManyInstances()
Returns:
true if max-cache-size has been reached

isHardLimit

public boolean isHardLimit()
Returns:
true if hard limit for max-cache-size

getCurrentWaiters

public int getCurrentWaiters()
Returns:
current number of instance waiters

getMaxWaitTime

public int getMaxWaitTime()
Returns:
waiter timeout in seconds

setMaxWaitTime

public void setMaxWaitTime(int sec)
Parameters:
sec - max time to wait for a connection, in seconds

initInstancePool

public void initInstancePool()
Init the pool of instances

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

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,
                            int poolaction)
Release a Context

Parameters:
ctx - - The JContext to release.
poolaction - 0=never pool, 1=pool if < MinPoolSize, 2=pool if < MaxCacheSize

releaseJContext

public void releaseJContext(JContext ctx)
Release a Context [deprecated]

Parameters:
ctx - - The JContext to release.

createNewContext

protected JEntityContext createNewContext(javax.ejb.EntityBean bean)

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

syncDirty

public void syncDirty(boolean alwaysStore)
Synchronize all dirty instances Does nothing if not CS policy.

Specified by:
syncDirty in interface BeanFactory
Parameters:
alwaysStore - True if we want to store modify instances even if passivation timeout is not reached.

reduceCache

public void reduceCache()
Reduce number of instances in memory passivate all instances that are not busy and not used very recently

Specified by:
reduceCache in interface BeanFactory

dirtyInstances

public boolean dirtyInstances()
Returns:
true if dirty list is not empty Only used for CS policy (non transacted modifying methods)

calculateAutomaticPk

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

Returns:
int (unique pk)

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

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 Only used for container-serialized policy


isDeadLocked

public boolean isDeadLocked(javax.transaction.Transaction suspect)
Search if this transaction is blocked in a deadlock

Parameters:
suspect - Transaction suspected to block everybody
Returns:
True if a deadlock has been detected.

getEntityCounters

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

Returns:
EntityCounters

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

syncForSelect

public void syncForSelect()
synchronize data modified in the current transaction. this is necessary in case of select method, because ejb-ql looks for on disk.


storeInstances

public void storeInstances(javax.transaction.Transaction tx)
For storing entities modified in tx

Specified by:
storeInstances in interface BeanFactory
Parameters:
tx - current transaction
See Also:
BeanFactory.storeInstances(javax.transaction.Transaction)

removeTxListener

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

Parameters:
tx - the Transaction to remove

unregisterContext

public void unregisterContext(javax.transaction.Transaction tx,
                              JEntityContext ec)
                       throws java.lang.IllegalStateException
Unregister a Context/Instance in the transaction.

Parameters:
tx - current Transaction
ctx - JEntityContext to be registered
Throws:
java.lang.IllegalStateException

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
ctx - JEntityContext to be registered
Returns:
true if instance has been registered.
Throws:
java.lang.IllegalStateException

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

getTimerService

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

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

createTimer

public javax.ejb.Timer createTimer(long arg0,
                                   java.io.Serializable arg1)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Illegal operation: timers are associated with a PK

Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException
See Also:
TimerService.createTimer(long, java.io.Serializable)

createTimer

public javax.ejb.Timer createTimer(long arg0,
                                   long arg1,
                                   java.io.Serializable arg2)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Illegal operation: timers are associated with a PK /* (non-Javadoc)

Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException
See Also:
TimerService.createTimer(long, long, java.io.Serializable)

createTimer

public javax.ejb.Timer createTimer(java.util.Date arg0,
                                   java.io.Serializable arg1)
                            throws java.lang.IllegalArgumentException,
                                   java.lang.IllegalStateException,
                                   javax.ejb.EJBException
Illegal operation: timers are associated with a PK

Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException
See Also:
TimerService.createTimer(java.util.Date, java.io.Serializable)

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
Illegal operation: timers are associated with a PK

Specified by:
createTimer in interface javax.ejb.TimerService
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalStateException
javax.ejb.EJBException
See Also:
TimerService.createTimer(java.util.Date, long, java.io.Serializable)

getTimers

public java.util.Collection getTimers()
                               throws java.lang.IllegalStateException,
                                      javax.ejb.EJBException
Illegal operation: timers are associated with a PK

Specified by:
getTimers in interface javax.ejb.TimerService
Throws:
java.lang.IllegalStateException
javax.ejb.EJBException
See Also:
TimerService.getTimers()