package org.jboss.management.j2ee;
import org.jboss.logging.Logger;
import org.jboss.management.j2ee.statistics.ServletStatsImpl;
import org.jboss.management.j2ee.statistics.TimeStatisticImpl;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.j2ee.statistics.Stats;
import java.util.Hashtable;
public class Servlet
extends J2EEManagedObject
implements ServletMBean
{
private static Logger log = Logger.getLogger(Servlet.class);
private ObjectName servletServiceName;
private ServletStatsImpl stats;
public static ObjectName create(MBeanServer mbeanServer, ObjectName webModuleName,
ObjectName webContainerName, ObjectName servletServiceName)
{
try
{
Servlet servlet = new Servlet(servletServiceName, webModuleName, webContainerName);
ObjectName jsr77Name = servlet.getObjectName();
mbeanServer.registerMBean(servlet, jsr77Name);
log.debug("Created JSR-77 Servlet: " + jsr77Name);
return jsr77Name;
}
catch (Exception e)
{
log.debug("Could not create JSR-77 Servlet: " + servletServiceName, e);
return null;
}
}
public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name)
{
try
{
mbeanServer.unregisterMBean(jsr77Name);
log.debug("Destroyed JSR-77 Servlet: " + jsr77Name);
}
catch (javax.management.InstanceNotFoundException ignore)
{
}
catch (Exception e)
{
log.debug("Could not destroy JSR-77 Servlet: " + jsr77Name, e);
}
}
public Servlet(ObjectName servletServiceName, ObjectName webModuleName,
ObjectName webContainerName)
throws MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.Servlet, servletServiceName.getKeyProperty("name"), webModuleName);
this.servletServiceName = servletServiceName;
this.stats = new ServletStatsImpl();
}
public Stats getstats()
{
try
{
TimeStatisticImpl serviceTime = (TimeStatisticImpl) stats.getServiceTime();
Integer count = (Integer) server.getAttribute(servletServiceName, "requestCount");
Long totalTime = (Long) server.getAttribute(servletServiceName, "processingTime");
Long minTime = (Long) server.getAttribute(servletServiceName, "minTime");
Long maxTime = (Long) server.getAttribute(servletServiceName, "maxTime");
serviceTime.set(count.longValue(), minTime.longValue(),
maxTime.longValue(), totalTime.longValue());
}
catch (Exception e)
{
log.debug("Failed to retrieve stats", e);
}
return stats;
}
public void resetStats()
{
stats.reset();
}
public String toString()
{
return "Servlet { " + super.toString() + " } []";
}
protected Hashtable getParentKeys(ObjectName pParent)
{
Hashtable lReturn = new Hashtable();
Hashtable lProperties = pParent.getKeyPropertyList();
lReturn.put(J2EETypeConstants.WebModule, lProperties.get("name"));
lReturn.put(J2EETypeConstants.J2EEApplication, lProperties.get(J2EETypeConstants.J2EEApplication));
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
return lReturn;
}
}