1 22 package org.jboss.varia.counter; 23 24 import java.util.Map ; 25 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import org.jboss.invocation.Invocation; 29 import org.jboss.ejb.Container; 30 import org.jboss.ejb.plugins.*; 31 32 33 import org.jboss.logging.Logger; 34 import org.jboss.varia.counter.CounterService; 35 36 62 public class CounterInterceptor 63 extends AbstractInterceptor 64 { 65 Container container = null; 66 CounterService counter = null; 67 boolean loggedNoCounter = false; 68 StringBuffer baseCounterName = null; 69 int baseNameLength = 0; 70 71 public CounterInterceptor() { 72 } 73 74 public void setContainer(Container container) { 75 baseCounterName = new StringBuffer (container.getBeanClass().getName()); 76 baseNameLength = baseCounterName.length(); 77 this.container = container; 78 } 79 public Container getContainer() { 80 return container; 81 } 82 83 public Object invokeHome(Invocation mi) throws Exception { 84 long startTime=System.currentTimeMillis(); 85 try { 86 return super.invokeHome(mi); 87 } finally { 88 if (getCounter() != null) { 89 long endTime=System.currentTimeMillis(); 90 baseCounterName.append("Home."); 91 baseCounterName.append(mi.getMethod().getName()); 92 counter.accumulate(baseCounterName.toString(), endTime-startTime); 93 baseCounterName.setLength(baseNameLength); 94 } 95 } 96 } 97 98 public Object invoke(Invocation mi) throws Exception { 99 long startTime=System.currentTimeMillis(); 100 try { 101 return super.invoke(mi); 102 } finally { 103 if (getCounter() != null) { 104 long endTime=System.currentTimeMillis(); 105 baseCounterName.append('.'); 106 baseCounterName.append(mi.getMethod().getName()); 107 counter.accumulate(baseCounterName.toString(), endTime-startTime); 108 baseCounterName.setLength(baseNameLength); 109 } 110 } 111 } 112 113 public void create() throws java.lang.Exception { 114 log.debug("CounterInterceptor initializing"); 116 } 117 118 private CounterService getCounter() { 119 if (counter == null) { 120 try { 121 InitialContext ctx = new InitialContext (); 122 counter = (CounterService)ctx.lookup(CounterService.JNDI_NAME); 123 } catch (NamingException ne) { 124 if (!loggedNoCounter) { 125 log.warn("CounterInterceptor can't get counter service ", ne); 126 loggedNoCounter = true; 127 } 128 } 129 } 130 return counter; 131 } 132 133 135 public void sample(Object s) 136 { 137 } 139 140 public Map retrieveStatistic() 141 { 142 return null; 143 } 144 145 public void resetStatistic() 146 { 147 } 148 } 149 | Popular Tags |