package org.jboss.varia.counter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.naming.NonSerializableFactory;
public class CounterService
extends ServiceMBeanSupport
implements CounterServiceMBean
{
public static final String JNDI_NAME = "java:/CounterService";
private HashMap counterMap = new HashMap();
public void accumulate(String counterName, long add)
{
Counter counter = null;
synchronized (counterMap)
{
counter = (Counter)counterMap.get(counterName);
if (counter == null)
{
counter = new Counter(counterName);
counterMap.put(counterName, counter);
}
}
counter.addToCount(add);
}
protected void startService() throws Exception
{
InitialContext ctx = new InitialContext();
NonSerializableFactory.bind(JNDI_NAME, this);
StringRefAddr addr = new StringRefAddr("nns", JNDI_NAME);
Reference ref = new Reference(this.getClass().getName(), addr, NonSerializableFactory.class.getName(), null);
ctx.bind(JNDI_NAME, ref);
}
protected void stopService() throws Exception
{
InitialContext ctx = new InitialContext();
ctx.unbind(JNDI_NAME);
NonSerializableFactory.unbind(JNDI_NAME);
}
public String list()
{
DecimalFormat format = new DecimalFormat("####0.0000");
String retVal = "";
Iterator keys = counterMap.keySet().iterator();
while (keys.hasNext())
{
String key = (String)keys.next();
Counter counter = (Counter)counterMap.get(key);
long total = 0;
int entries = 0;
synchronized (counter)
{ total = counter.getCount();
entries = counter.getEntries();
}
double avg = ((double)total)/((double)entries);
String descrip = key+": total="+total+" on "+entries+"entries for "+
"an average of "+format.format(avg)+"<br>\n";
retVal += descrip;
}
return retVal;
}
private static class Counter
{
private String name;
private long count=0;
private int entries=0;
public Counter(String n)
{
name = n;
}
public String getName()
{
return name;
}
public synchronized long getCount()
{
return count;
}
public synchronized int getEntries()
{
return entries;
}
public synchronized void addToCount(long add)
{
count += add;
entries++;
}
}
}