package org.jboss.mq.pm.none;
import javax.jms.JMSException;
import javax.management.ObjectName;
import org.jboss.mq.SpyDestination;
import org.jboss.mq.SpyMessage;
import org.jboss.mq.pm.CacheStore;
import org.jboss.mq.pm.Tx;
import org.jboss.mq.pm.TxManager;
import org.jboss.mq.server.JMSDestination;
import org.jboss.mq.server.MessageCache;
import org.jboss.mq.server.MessageReference;
import org.jboss.system.ServiceMBeanSupport;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
public class PersistenceManager
extends ServiceMBeanSupport
implements PersistenceManagerMBean, org.jboss.mq.pm.PersistenceManager, CacheStore
{
SynchronizedLong nextTransactionid = new SynchronizedLong(0l);
ObjectName delegateName;
org.jboss.mq.pm.PersistenceManager delegate;
TxManager txManager;
ConcurrentHashMap cache = new ConcurrentHashMap();
public ObjectName getDelegatePM()
{
return delegateName;
}
public void setDelegatePM(ObjectName delegateName)
{
this.delegateName = delegateName;
}
public void add(MessageReference message, Tx txId) throws JMSException
{
if (delegate != null && message.inMemory() == false)
delegate.add(message, txId);
}
public void closeQueue(JMSDestination jmsDest, SpyDestination dest) throws JMSException
{
if (delegate != null)
delegate.closeQueue(jmsDest, dest);
}
public void commitPersistentTx(Tx txId) throws JMSException
{
if (delegate != null)
delegate.commitPersistentTx(txId);
}
public Tx createPersistentTx() throws JMSException
{
if (delegate != null)
return delegate.createPersistentTx();
Tx tx = new Tx(nextTransactionid.increment());
return tx;
}
public MessageCache getMessageCacheInstance()
{
if (delegate != null)
return delegate.getMessageCacheInstance();
throw new UnsupportedOperationException("This is now set on the destination manager");
}
public TxManager getTxManager()
{
return txManager;
}
public void remove(MessageReference message, Tx txId) throws JMSException
{
if (delegate != null && message.inMemory() == false)
delegate.remove(message, txId);
}
public void restoreQueue(JMSDestination jmsDest, SpyDestination dest) throws JMSException
{
if (delegate != null)
delegate.restoreQueue(jmsDest, dest);
}
public void rollbackPersistentTx(Tx txId) throws JMSException
{
if (delegate != null)
delegate.rollbackPersistentTx(txId);
}
public void update(MessageReference message, Tx txId) throws JMSException
{
if (delegate != null && message.inMemory() == false)
delegate.update(message, txId);
}
public Object getInstance()
{
return this;
}
public ObjectName getMessageCache()
{
if (delegateName != null)
{
try
{
return (ObjectName) server.getAttribute(delegateName, "MessageCache");
}
catch (Exception e)
{
log.trace("Unable to retrieve message cache from delegate", e);
}
}
throw new UnsupportedOperationException("This is now set on the destination manager");
}
public void setMessageCache(ObjectName messageCache)
{
throw new UnsupportedOperationException("This is now set on the destination manager");
}
public SpyMessage loadFromStorage(MessageReference mh) throws JMSException
{
if (delegate == null || mh.inMemory())
return (SpyMessage) cache.get(mh);
else
return ((CacheStore) delegate).loadFromStorage(mh);
}
public void removeFromStorage(MessageReference mh) throws JMSException
{
if (delegate == null || mh.inMemory())
{
cache.remove(mh);
mh.setStored(MessageReference.NOT_STORED);
}
else
((CacheStore) delegate).removeFromStorage(mh);
}
public void saveToStorage(MessageReference mh, SpyMessage message) throws JMSException
{
if (delegate == null || mh.inMemory())
{
cache.put(mh, message);
mh.setStored(MessageReference.STORED);
}
else
((CacheStore) delegate).saveToStorage(mh, message);
}
protected void startService() throws Exception
{
if (delegateName != null)
{
delegate = (org.jboss.mq.pm.PersistenceManager) getServer().getAttribute(delegateName, "Instance");
if ((delegate instanceof CacheStore) == false)
throw new UnsupportedOperationException("The delegate persistence manager must also be a CacheStore");
txManager = delegate.getTxManager();
}
else
{
txManager = new TxManager(this);
}
}
}