1 package jfun.yan.monitoring; 2 3 4 import jfun.yan.YanException; 5 import jfun.yan.function.Function; 6 7 final class MonitoredFunction implements Function { 8 private final Function fwd; 9 private final FunctionMonitor mon; 10 MonitoredFunction(Function f, FunctionMonitor mon) { 11 this.fwd = f; 12 this.mon = mon; 13 } 14 public boolean isConcrete() { 15 return fwd.isConcrete(); 16 } 17 public Class getReturnType() { 18 return fwd.getReturnType(); 19 } 20 public Class [] getParameterTypes() { 21 return fwd.getParameterTypes(); 22 } 23 public Object call(Object [] args) throws Throwable { 24 mon.calling(fwd, args); 25 final long from = System.currentTimeMillis(); 26 Object result; 27 try{ 28 result = fwd.call(args); 29 } 30 catch(Throwable e){ 31 mon.callFailed(fwd, args, unwrap(e), System.currentTimeMillis()-from); 32 throw e; 33 } 34 mon.called(fwd, args, result, System.currentTimeMillis()-from); 35 return result; 36 } 37 public String getName() { 38 return fwd.getName(); 39 } 40 public boolean equals(Object obj) { 41 if(obj instanceof MonitoredFunction){ 42 final MonitoredFunction other = (MonitoredFunction)obj; 43 return mon==other.mon && fwd.equals(other.fwd); 44 } 45 else{ 46 return false; 47 } 48 } 49 public int hashCode() { 50 return fwd.hashCode()*31+System.identityHashCode(mon); 51 } 52 public String toString() { 53 return fwd.toString(); 54 } 55 private Throwable unwrap(Throwable e){ 56 if(e instanceof YanException){ 57 final Throwable cause = ((YanException)e).getCause(); 58 if(cause != null) return cause; 59 } 60 return e; 61 } 62 } 63 | Popular Tags |