package org.jboss.mq.server.jmx;
import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.jboss.mq.MessageStatistics;
import org.jboss.mq.pm.PersistenceManager;
import org.jboss.mq.server.BasicQueueParameters;
import org.jboss.mq.server.JMSDestinationManager;
import org.jboss.mq.server.JMSServerInterceptor;
import org.jboss.mq.server.MessageCache;
import org.jboss.mq.server.MessageCounter;
import org.jboss.mq.server.Receivers;
import org.jboss.mq.sm.StateManager;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.system.ServiceControllerMBean;
public class DestinationManager extends InterceptorMBeanSupport implements DestinationManagerMBean
{
public String jndiBindLocation = "java:/JBossMQServer";
private ServiceControllerMBean serviceController;
private ObjectName mqService;
private JMSDestinationManager jmsServer;
private ObjectName persistenceManager;
private ObjectName stateManager;
private ObjectName messageCache;
private BasicQueueParameters tempParameters = new BasicQueueParameters();
public int getClientCount()
{
return jmsServer.getClientCount();
}
public Map getClients()
{
return jmsServer.getClients();
}
public ObjectName getPersistenceManager()
{
return persistenceManager;
}
public void setPersistenceManager(ObjectName objectName)
{
this.persistenceManager = objectName;
}
public ObjectName getStateManager()
{
return stateManager;
}
public void setStateManager(ObjectName objectName)
{
this.stateManager = objectName;
}
public ObjectName getMessageCache()
{
return messageCache;
}
public void setMessageCache(ObjectName objectName)
{
this.messageCache = objectName;
}
public int getTemporaryMaxDepth()
{
return tempParameters.maxDepth;
}
public void setTemporaryMaxDepth(int depth)
{
tempParameters.maxDepth = depth;
}
public boolean getTemporaryInMemory()
{
return tempParameters.inMemory;
}
public void setTemporaryInMemory(boolean mode)
{
tempParameters.inMemory = mode;
}
public Class getReceiversImpl()
{
return tempParameters.receiversImpl;
}
public void setReceiversImpl(Class clazz)
{
if (clazz != null && Receivers.class.isAssignableFrom(clazz) == false)
throw new IllegalArgumentException("Class " + clazz.getName() + " is not a Receivers implementation");
tempParameters.receiversImpl = clazz;
}
public void createQueue(String name) throws Exception
{
createDestination("org.jboss.mq.server.jmx.Queue", getQueueObjectName(name), null);
}
public void createTopic(String name) throws Exception
{
createDestination("org.jboss.mq.server.jmx.Topic", getTopicObjectName(name), null);
}
public void createQueue(String name, String jndiLocation) throws Exception
{
createDestination("org.jboss.mq.server.jmx.Queue", getQueueObjectName(name), jndiLocation);
}
public void createTopic(String name, String jndiLocation) throws Exception
{
createDestination("org.jboss.mq.server.jmx.Topic", getTopicObjectName(name), jndiLocation);
}
protected void createDestination(String type, ObjectName name, String jndiLocation) throws Exception
{
log.debug("Attempting to create destination: " + name + "; type=" + type);
server.createMBean(type, name);
server.setAttribute(name, new Attribute("DestinationManager", mqService));
if (jndiLocation != null)
server.setAttribute(name, new Attribute("JNDIName", jndiLocation));
ArrayList depends = new ArrayList();
depends.add(serviceName);
serviceController.create(name, depends);
serviceController.start(name);
}
public void destroyQueue(String name) throws Exception
{
destroyDestination(getQueueObjectName(name));
}
public void destroyTopic(String name) throws Exception
{
destroyDestination(getTopicObjectName(name));
}
protected void destroyDestination(ObjectName name) throws Exception
{
if (log.isDebugEnabled())
{
log.debug("Attempting to destroy destination: " + name);
}
serviceController.stop(name);
server.invoke(name, "removeAllMessages", new Object[] {
}, new String[] {
});
serviceController.destroy(name);
serviceController.remove(name);
}
protected ObjectName getObjectName(MBeanServer server, ObjectName name) throws MalformedObjectNameException
{
mqService = name;
return mqService;
}
protected void stopService()
{
jmsServer.stopServer();
}
protected ObjectName getTopicObjectName(String name) throws MalformedObjectNameException
{
return new ObjectName(mqService.getDomain() + ".destination:service=Topic,name=" + name);
}
protected ObjectName getQueueObjectName(String name) throws MalformedObjectNameException
{
return new ObjectName(mqService.getDomain() + ".destination:service=Queue,name=" + name);
}
protected ServiceControllerMBean getServiceController()
{
return serviceController;
}
public JMSServerInterceptor getInterceptor()
{
return jmsServer;
}
protected void createService() throws Exception
{
super.createService();
jmsServer = new JMSDestinationManager(tempParameters);
}
protected void startService() throws Exception
{
serviceController =
(ServiceControllerMBean) MBeanProxyExt.create(
ServiceControllerMBean.class,
ServiceControllerMBean.OBJECT_NAME,
server);
PersistenceManager pm = (PersistenceManager) server.getAttribute(persistenceManager, "Instance");
jmsServer.setPersistenceManager(pm);
StateManager sm = (StateManager) server.getAttribute(stateManager, "Instance");
jmsServer.setStateManager(sm);
MessageCache mc = null;
if (messageCache != null)
mc = (MessageCache) server.getAttribute(messageCache, "Instance");
else
mc = pm.getMessageCacheInstance();
jmsServer.setMessageCache(mc);
jmsServer.startServer();
super.startService();
}
protected void destroyService()
{
}
public void setMessageCounterHistoryDayLimit( int days )
{
if( days < -1 )
days = -1;
tempParameters.messageCounterHistoryDayLimit = days;
}
public int getMessageCounterHistoryDayLimit()
{
return tempParameters.messageCounterHistoryDayLimit;
}
public MessageCounter[] getMessageCounter() throws Exception
{
return jmsServer.getMessageCounter();
}
public MessageStatistics[] getMessageStatistics() throws Exception
{
return MessageCounter.getMessageStatistics(jmsServer.getMessageCounter());
}
public String listMessageCounter() throws Exception
{
MessageCounter[] counter = jmsServer.getMessageCounter();
String ret =
"<table width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">"
+ "<tr>"
+ "<th>Type</th>"
+ "<th>Name</th>"
+ "<th>Subscription</th>"
+ "<th>Durable</th>"
+ "<th>Count</th>"
+ "<th>CountDelta</th>"
+ "<th>Depth</th>"
+ "<th>DepthDelta</th>"
+ "<th>Last Add</th>"
+ "</tr>";
String strNameLast = null;
String strTypeLast = null;
String strDestLast = null;
String destData = "";
int destCount = 0;
int countTotal = 0;
int countDeltaTotal = 0;
int depthTotal = 0;
int depthDeltaTotal = 0;
int i = 0;
for (i = 0; i < counter.length; i++)
{
StringTokenizer tokens = new StringTokenizer(counter[i].getCounterAsString(), ",");
String strType = tokens.nextToken();
String strName = tokens.nextToken();
String strSub = tokens.nextToken();
String strDurable = tokens.nextToken();
String strDest = strType + "-" + strName;
String strCount = tokens.nextToken();
String strCountDelta = tokens.nextToken();
String strDepth = tokens.nextToken();
String strDepthDelta = tokens.nextToken();
String strDate = tokens.nextToken();
countTotal += Integer.parseInt(strCount);
depthTotal += Integer.parseInt(strDepth);
countDeltaTotal += Integer.parseInt(strCountDelta);
depthDeltaTotal += Integer.parseInt(strDepthDelta);
if (strCountDelta.equalsIgnoreCase("0"))
strCountDelta = "-";
if (strDepthDelta.equalsIgnoreCase("0"))
strDepthDelta = "-";
if (strDestLast != null && strDestLast.equals(strDest))
{
destData += "<tr bgcolor=\"#" + ((i % 2) == 0 ? "FFFFFF" : "F0F0F0") + "\">";
destCount += 1;
}
else
{
if (strDestLast != null)
{
ret += "<tr bgcolor=\"#"
+ ((i % 2) == 0 ? "FFFFFF" : "F0F0F0")
+ "\"><td rowspan=\""
+ destCount
+ "\">"
+ strTypeLast
+ "</td><td rowspan=\""
+ destCount
+ "\">"
+ strNameLast
+ "</td>"
+ destData;
destData = "";
}
destCount = 1;
}
destData += "<td>"
+ strSub
+ "</td>"
+ "<td>"
+ strDurable
+ "</td>"
+ "<td>"
+ strCount
+ "</td>"
+ "<td>"
+ strCountDelta
+ "</td>"
+ "<td>"
+ strDepth
+ "</td>"
+ "<td>"
+ strDepthDelta
+ "</td>"
+ "<td>"
+ strDate
+ "</td>";
strTypeLast = strType;
strNameLast = strName;
strDestLast = strDest;
}
if (strDestLast != null)
{
ret += "<tr bgcolor=\"#"
+ ((i % 2) == 0 ? "FFFFFF" : "F0F0F0")
+ "\"><td rowspan=\""
+ destCount
+ "\">"
+ strTypeLast
+ "</td><td rowspan=\""
+ destCount
+ "\">"
+ strNameLast
+ "</td>"
+ destData;
}
ret += "<tr>"
+ "<td><![CDATA[ ]]></td><td><![CDATA[ ]]></td>"
+ "<td><![CDATA[ ]]></td><td><![CDATA[ ]]></td><td>"
+ countTotal
+ "</td><td>"
+ (countDeltaTotal == 0 ? "-" : Integer.toString(countDeltaTotal))
+ "</td><td>"
+ depthTotal
+ "</td><td>"
+ (depthDeltaTotal == 0 ? "-" : Integer.toString(depthDeltaTotal))
+ "</td><td>Total</td></tr></table>";
return ret;
}
public void resetMessageCounter()
{
jmsServer.resetMessageCounter();
}
}