| Method from org.jboss.ejb.plugins.AbstractInstanceCache Detail: |
abstract protected EnterpriseContext acquireContext() throws Exception
Acquires an EnterpriseContext from the pool |
abstract protected void activate(EnterpriseContext ctx) throws RemoteException
Activates the given EnterpriseContext |
abstract protected boolean canPassivate(EnterpriseContext ctx)
Returns whether the given context can be passivated or not |
public void create() throws Exception {
getCache().create();
}
|
public void destroy() {
synchronized (getCacheLock())
{
getCache().destroy();
}
this.m_cache = null;
}
|
protected boolean doActivate(EnterpriseContext ctx) throws RemoteException {
activate(ctx);
return true;
}
Activate the given EnterpriseContext |
public void flush() {
if( m_cache != null )
m_cache.flush();
}
|
abstract protected void freeContext(EnterpriseContext ctx)
Frees the given EnterpriseContext to the pool |
public EnterpriseContext get(Object id) throws NoSuchObjectException, RemoteException {
if (id == null) throw new IllegalArgumentException("Can't get an object with a null key");
EnterpriseContext ctx;
synchronized (getCacheLock())
{
CachePolicy cache = getCache();
ctx = (EnterpriseContext)cache.get(id);
if (ctx == null)
{
try
{
ctx = acquireContext();
setKey(id, ctx);
if (doActivate(ctx) == false)
// This is a recursive activation
return ctx;
logActivation(id);
// the cache will throw an IllegalStateException if we try to insert
// something that is in the cache already, so we don't check here
cache.insert(id, ctx);
}
catch (Throwable x)
{
log.debug("Activation failure", x);
throw new NoSuchObjectException(x.getMessage());
}
}
}
return ctx;
}
|
protected CachePolicy getCache() {
return m_cache;
}
Returns the cache policy used for this cache. |
public Object getCacheLock() {
return m_cacheLock;
}
Returns the mutex used to sync access to the cache policy object |
public String getCachePolicyString() {
return m_cache.toString();
}
Display the cache policy. |
public long getCacheSize() {
int cacheSize = m_cache != null ? m_cache.size() : 0;
return cacheSize;
}
Get the current cache size |
abstract protected Container getContainer()
Returns the container for this cache. |
abstract protected Object getKey(EnterpriseContext ctx)
Returns the key used by the cache to map the given context |
public long getPassivatedCount() {
return 0;
}
Get the passivated count. |
public void importXml(Element element) throws DeploymentException {
// This one is mandatory
String p = MetaData.getElementContent(MetaData.getUniqueChild(element, "cache-policy"));
try
{
Class cls = SecurityActions.getContextClassLoader().loadClass(p);
Constructor ctor = cls.getConstructor(new Class[] {AbstractInstanceCache.class});
m_cache = (CachePolicy)ctor.newInstance(new Object[] {this});
}
catch (Exception x)
{
throw new DeploymentException("Can't create cache policy", x);
}
Element policyConf = MetaData.getOptionalChild(element, "cache-policy-conf");
if (policyConf != null)
{
if (m_cache instanceof XmlLoadable)
{
try
{
((XmlLoadable)m_cache).importXml(policyConf);
}
catch (Exception x)
{
throw new DeploymentException("Can't import policy configuration", x);
}
}
}
}
|
public void insert(EnterpriseContext ctx) {
if (ctx == null) throw new IllegalArgumentException("Can't insert a null object in the cache");
Object key = getKey(ctx);
synchronized (getCacheLock())
{
// the cache will throw an IllegalStateException if we try to insert
// something that is in the cache already, so we don't check here
getCache().insert(key, ctx);
}
}
|
public boolean isActive(Object id) {
// Check whether an object with the given id is available in the cache
synchronized (getCacheLock())
{
return getCache().peek(id) != null;
}
}
|
protected void logActivation(Object id) {
if( log.isTraceEnabled() )
{
StringBuffer m_buffer=new StringBuffer(100);
m_buffer.append("Activated bean ");
m_buffer.append(getContainer().getBeanMetaData().getEjbName());
m_buffer.append(" with id = ");
m_buffer.append(id);
log.trace(m_buffer.toString());
}
}
|
protected void logPassivation(Object id) {
if( log.isTraceEnabled() )
{
StringBuffer m_buffer=new StringBuffer(100);
m_buffer.append("Passivated bean ");
m_buffer.append(getContainer().getBeanMetaData().getEjbName());
m_buffer.append(" with id = ");
m_buffer.append(id);
log.trace(m_buffer.toString());
}
}
|
abstract protected void passivate(EnterpriseContext ctx) throws RemoteException
Passivates the given EnterpriseContext |
public void release(EnterpriseContext ctx) {
if (ctx == null) throw new IllegalArgumentException("Can't release a null object");
// Here I remove the bean; call to remove(id) is wrong
// cause will remove also the cache lock that is needed
// by the passivation, that eventually will remove it.
/* the removal should only be done if the instance is not in use.
this is taken care of in tryToPassivate
Object id = getKey(ctx);
synchronized (getCacheLock())
{
if (getCache().peek(id) != null)
getCache().remove(id);
}
*/
tryToPassivate(ctx, true);
}
Passivates and removes the instance from the cache.
If the instance is in use then removal and passivation will be scheduled until
after transaction ends |
public void remove(Object id) {
if (id == null) throw new IllegalArgumentException("Can't remove an object using a null key");
synchronized (getCacheLock())
{
if (getCache().peek(id) != null)
{
getCache().remove(id);
}
}
}
From InstanceCache interface |
public void resetStatistic() {
}
|
public Map retrieveStatistic() {
return null;
}
|
public void sample(Object s) {
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Monitorable implementation ------------------------------------
if( m_cache == null )
return;
synchronized (getCacheLock())
{
BeanCacheSnapshot snapshot = (BeanCacheSnapshot)s;
snapshot.m_passivatingBeans = 0;
CachePolicy policy = getCache();
if (policy instanceof Monitorable)
{
((Monitorable)policy).sample(s);
}
}
}
|
abstract protected void setKey(Object id,
EnterpriseContext ctx)
Sets the given id as key for the given context |
public void start() throws Exception {
getCache().start();
}
|
public void stop() {
// Empty the cache
synchronized (getCacheLock())
{
getCache().stop();
}
}
|
protected void tryToPassivate(EnterpriseContext ctx) {
tryToPassivate(ctx, false);
}
Tries to passivate the instance. If the instance is in use then the instance
will be passivated later according to the container's commit option and max age. |
protected void tryToPassivate(EnterpriseContext ctx,
boolean passivateAfterCommit) {
Object id = ctx.getId();
if (id == null) return;
BeanLock lock = getContainer().getLockManager().getLock(id);
boolean lockedBean = false;
try
{
/* If this is a BeanLockExt only attempt the lock as the call to
remove is going to have to acquire the cache lock, but this may already
be held since this method is called by passivation policies without
the cache lock. This can lead to a deadlock as in the case of a size based
eviction during a cache get attempts to lock the bean that has been
locked by an age based background thread as seen in bug 987389 on
sourceforge.
*/
if( lock instanceof BeanLockExt )
{
BeanLockExt lock2 = (BeanLockExt) lock;
lockedBean = lock2.attemptSync();
if( lockedBean == false )
{
unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
return;
}
}
else
{
// Use the blocking sync
lock.sync();
lockedBean = true;
}
if (canPassivate(ctx))
{
try
{
remove(id);
passivate(ctx);
freeContext(ctx);
}
catch (Exception ignored)
{
log.warn("failed to passivate, id="+id, ignored);
}
}
else
{
// Touch the entry to make it MRU
synchronized (getCacheLock())
{
getCache().get(id);
}
unableToPassivateDueToCtxLock(ctx, passivateAfterCommit);
}
}
finally
{
if( lockedBean )
lock.releaseSync();
getContainer().getLockManager().removeLockRef(id);
}
}
Tries to passivate the instance. If the instance is in use and passivateAfterCommit
parameter is true then the instance will passivated after the transaction commits.
Otherwise, the instance will be passivated later according to the container's
commit option and max age. |
protected void unableToPassivateDueToCtxLock(EnterpriseContext ctx,
boolean passivateAfterCommit) {
log.warn("Unable to passivate due to ctx lock, id="+ctx.getId());
}
|