package org.jboss.varia.counter;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.invocation.Invocation;
import org.jboss.ejb.Container;
import org.jboss.ejb.plugins.*;
import org.jboss.logging.Logger;
import org.jboss.varia.counter.CounterService;
public class CounterInterceptor
extends AbstractInterceptor
{
Container container = null;
CounterService counter = null;
boolean loggedNoCounter = false;
StringBuffer baseCounterName = null;
int baseNameLength = 0;
public CounterInterceptor() {
}
public void setContainer(Container container) {
baseCounterName = new StringBuffer(container.getBeanClass().getName());
baseNameLength = baseCounterName.length();
this.container = container;
}
public Container getContainer() {
return container;
}
public Object invokeHome(Invocation mi) throws Exception {
long startTime=System.currentTimeMillis();
try {
return super.invokeHome(mi);
} finally {
if (getCounter() != null) {
long endTime=System.currentTimeMillis();
baseCounterName.append("Home.");
baseCounterName.append(mi.getMethod().getName());
counter.accumulate(baseCounterName.toString(), endTime-startTime);
baseCounterName.setLength(baseNameLength);
}
}
}
public Object invoke(Invocation mi) throws Exception {
long startTime=System.currentTimeMillis();
try {
return super.invoke(mi);
} finally {
if (getCounter() != null) {
long endTime=System.currentTimeMillis();
baseCounterName.append('.');
baseCounterName.append(mi.getMethod().getName());
counter.accumulate(baseCounterName.toString(), endTime-startTime);
baseCounterName.setLength(baseNameLength);
}
}
}
public void create() throws java.lang.Exception {
log.debug("CounterInterceptor initializing");
}
private CounterService getCounter() {
if (counter == null) {
try {
InitialContext ctx = new InitialContext();
counter = (CounterService)ctx.lookup(CounterService.JNDI_NAME);
} catch (NamingException ne) {
if (!loggedNoCounter) {
log.warn("CounterInterceptor can't get counter service ", ne);
loggedNoCounter = true;
}
}
}
return counter;
}
public void sample(Object s)
{
}
public Map retrieveStatistic()
{
return null;
}
public void resetStatistic()
{
}
}