package org.jboss.management.j2ee;
import org.jboss.invocation.InvocationStatistics;
import org.jboss.logging.Logger;
import org.jboss.management.j2ee.statistics.CountStatisticImpl;
import org.jboss.management.j2ee.statistics.EJBStatsImpl;
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.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public abstract class EJB
extends J2EEManagedObject
implements EJBMBean
{
public static final int ENTITY_BEAN = 0;
public static final int STATEFUL_SESSION_BEAN = 1;
public static final int STATELESS_SESSION_BEAN = 2;
public static final int MESSAGE_DRIVEN_BEAN = 3;
private static Logger log = Logger.getLogger(EJB.class);
protected ObjectName ejbContainerName;
public static ObjectName create(MBeanServer mbeanServer, ObjectName ejbModuleName,
ObjectName ejbContainerName, int ejbType, String jndiName)
{
try
{
EJB ejb = null;
switch (ejbType)
{
case ENTITY_BEAN:
ejb = new EntityBean(jndiName, ejbModuleName, ejbContainerName);
break;
case STATEFUL_SESSION_BEAN:
ejb = new StatefulSessionBean(jndiName, ejbModuleName, ejbContainerName);
break;
case STATELESS_SESSION_BEAN:
ejb = new StatelessSessionBean(jndiName, ejbModuleName, ejbContainerName);
break;
case MESSAGE_DRIVEN_BEAN:
ejb = new MessageDrivenBean(jndiName, ejbModuleName, ejbContainerName);
break;
}
ObjectName jsr77Name = ejb.getObjectName();
mbeanServer.registerMBean(ejb, jsr77Name);
log.debug("Created JSR-77 EJB: " + jsr77Name);
return jsr77Name;
}
catch (Exception e)
{
log.debug("Could not create JSR-77 EJB: " + jndiName, e);
return null;
}
}
public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name)
{
try
{
mbeanServer.unregisterMBean(jsr77Name);
log.debug("Destroyed JSR-77 EJB: " + jsr77Name);
}
catch (javax.management.InstanceNotFoundException ignore)
{
}
catch (Exception e)
{
log.debug("Could not destroy JSR-77 EJB: " + jsr77Name, e);
}
}
public EJB(String ejbType, String ejbName, ObjectName ejbModuleName,
ObjectName ejbContainerName)
throws MalformedObjectNameException,
InvalidParentException
{
super(ejbType, ejbName, ejbModuleName);
this.ejbContainerName = ejbContainerName;
}
public abstract Stats getstats();
public abstract void resetStats();
public String toString()
{
return "EJB { " + super.toString() + " } []";
}
protected void updateCommonStats(EJBStatsImpl stats)
{
try
{
ObjectName containerName = getContainerName();
CountStatisticImpl createCount = (CountStatisticImpl) stats.getCreateCount();
Long creates = (Long) server.getAttribute(containerName, "CreateCount");
createCount.set(creates.longValue());
CountStatisticImpl removeCount = (CountStatisticImpl) stats.getRemoveCount();
Long removes = (Long) server.getAttribute(containerName, "RemoveCount");
removeCount.set(removes.longValue());
InvocationStatistics times = (InvocationStatistics) server.getAttribute(containerName, "InvokeStats");
HashMap timesMap = new HashMap(times.getStats());
Iterator iter = timesMap.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry) iter.next();
Method m = (Method) entry.getKey();
InvocationStatistics.TimeStatistic stat = (InvocationStatistics.TimeStatistic) entry.getValue();
TimeStatisticImpl tstat = new TimeStatisticImpl(m.getName(), StatisticsConstants.MILLISECOND,
"The timing information for the given method");
tstat.set(stat.count, stat.minTime, stat.maxTime, stat.totalTime);
stats.addStatistic(m.getName(), tstat);
}
}
catch (Exception e)
{
log.debug("Failed to retrieve stats", e);
}
}
protected ObjectName getContainerName()
{
return this.ejbContainerName;
}
protected ObjectName getContainerCacheName()
{
ObjectName cacheName = null;
try
{
Hashtable props = ejbContainerName.getKeyPropertyList();
props.put("plugin", "cache");
cacheName = new ObjectName(ejbContainerName.getDomain(), props);
}
catch (MalformedObjectNameException e)
{
}
return cacheName;
}
protected ObjectName getContainerPoolName()
{
ObjectName poolName = null;
try
{
Hashtable props = ejbContainerName.getKeyPropertyList();
props.put("plugin", "pool");
poolName = new ObjectName(ejbContainerName.getDomain(), props);
}
catch (MalformedObjectNameException e)
{
}
return poolName;
}
protected Hashtable getParentKeys(ObjectName pParent)
{
Hashtable lReturn = new Hashtable();
Hashtable lProperties = pParent.getKeyPropertyList();
lReturn.put(J2EETypeConstants.EJBModule, lProperties.get("name"));
lReturn.put(J2EETypeConstants.J2EEApplication, lProperties.get(J2EETypeConstants.J2EEApplication));
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
return lReturn;
}
}