Coverage Report - org.dozer.stats.StatisticsInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
StatisticsInterceptor
96%
26/27
50%
3/6
3
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.dozer.stats;
 17  
 
 18  
 import org.dozer.util.MappingUtils;
 19  
 
 20  
 import java.lang.reflect.InvocationHandler;
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.lang.reflect.Method;
 23  
 
 24  
 
 25  
 /**
 26  
  * Internal dynamic proxy used for collecting mapping statisics. Only intended for internal use.
 27  
  * 
 28  
  * @author tierney.matt
 29  
  */
 30  
 public class StatisticsInterceptor implements InvocationHandler {
 31  
   private final Object delegate;
 32  
   private final StatisticsManager statsMgr;
 33  
 
 34  2
   public StatisticsInterceptor(Object delegate, StatisticsManager statsMgr) {
 35  2
     this.delegate = delegate;
 36  2
     this.statsMgr = statsMgr;
 37  2
   }
 38  
 
 39  
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 40  2
     long start = System.currentTimeMillis();
 41  
 
 42  
     try {
 43  2
       Object result = method.invoke(delegate, args);
 44  
 
 45  1
       long stop = System.currentTimeMillis();
 46  1
       statsMgr.increment(StatisticType.MAPPING_SUCCESS_COUNT);
 47  1
       statsMgr.increment(StatisticType.MAPPING_TIME, (stop - start));
 48  
 
 49  1
       return result;
 50  1
     } catch (InvocationTargetException e) {
 51  1
       Throwable ex = e.getTargetException();
 52  
 
 53  1
       statsMgr.increment(StatisticType.MAPPING_FAILURE_COUNT);
 54  1
       Throwable rootCause = MappingUtils.getRootCause(ex);
 55  1
       statsMgr.increment(StatisticType.MAPPING_FAILURE_EX_TYPE_COUNT, rootCause.getClass());
 56  1
       incrementClassMappingFailureTypeStat(args);
 57  1
       throw ex;
 58  
     }
 59  
   }
 60  
 
 61  
   private void incrementClassMappingFailureTypeStat(Object[] args) {
 62  
     // Determine src and dest class name. The combination of src and dest class name will be used for the statistic entry key.
 63  1
     String srcClassName = null;
 64  1
     if (args[0] != null) {
 65  1
       srcClassName = args[0].getClass().getName();
 66  
     }
 67  1
     String destClassName = null;
 68  1
     if (args[1] != null) {
 69  1
       if (args[1] instanceof Class) {
 70  0
         destClassName = ((Class<?>) args[1]).getName();
 71  
       } else {
 72  1
         destClassName = args[1].getClass().getName();
 73  
       }
 74  
     }
 75  1
     statsMgr.increment(StatisticType.MAPPING_FAILURE_TYPE_COUNT, srcClassName + "-->" + destClassName);
 76  1
   }
 77  
 }