1 16 package net.sf.dozer.util.mapping.interceptor; 17 18 import java.lang.reflect.InvocationHandler ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import net.sf.dozer.util.mapping.stats.StatisticTypeConstants; 23 import net.sf.dozer.util.mapping.stats.StatisticsManagerIF; 24 import net.sf.dozer.util.mapping.util.MappingUtils; 25 26 29 public class StatisticsInterceptor implements InvocationHandler { 30 private final Object delegate; 31 private final StatisticsManagerIF statsMgr; 32 private final MappingUtils mappingUtils = new MappingUtils(); 33 34 public StatisticsInterceptor(Object delegate, StatisticsManagerIF statsMgr) { 35 this.delegate = delegate; 36 this.statsMgr = statsMgr; 37 } 38 39 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 40 long start = System.currentTimeMillis(); 41 42 try { 43 Object result = method.invoke(delegate, args); 44 45 long stop = System.currentTimeMillis(); 46 statsMgr.increment(StatisticTypeConstants.MAPPING_SUCCESS_COUNT); 47 statsMgr.increment(StatisticTypeConstants.MAPPING_TIME, (stop - start)); 48 49 return result; 50 } catch (InvocationTargetException e) { 51 Throwable ex = e.getTargetException(); 52 53 statsMgr.increment(StatisticTypeConstants.MAPPING_FAILURE_COUNT); 54 Throwable rootCause = mappingUtils.getRootCause(ex); 55 statsMgr.increment(StatisticTypeConstants.MAPPING_FAILURE_EX_TYPE_COUNT, rootCause.getClass()); 56 incrementClassMappingFailureTypeStat(args); 57 throw ex; 58 } 59 } 60 61 private void incrementClassMappingFailureTypeStat(Object [] args) { 62 String srcClassName = null; 65 if (args[0] != null) { 66 srcClassName = args[0].getClass().getName(); 67 } 68 String destClassName = null; 69 if (args[1] != null) { 70 if (args[1] instanceof Class ) { 71 destClassName = ((Class ) args[1]).getName(); 72 } else { 73 destClassName = args[1].getClass().getName(); 74 } 75 } 76 statsMgr.increment(StatisticTypeConstants.MAPPING_FAILURE_TYPE_COUNT, srcClassName + "-->" + destClassName); 77 } 78 } 79 80 | Popular Tags |