KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > interceptor > StatisticsInterceptor


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.interceptor;
17
18 import java.lang.reflect.InvocationHandler JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
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  /*
27  * @author tierney.matt
28  */

29 public class StatisticsInterceptor implements InvocationHandler JavaDoc {
30   private final Object JavaDoc delegate;
31   private final StatisticsManagerIF statsMgr;
32   private final MappingUtils mappingUtils = new MappingUtils();
33
34   public StatisticsInterceptor(Object JavaDoc delegate, StatisticsManagerIF statsMgr) {
35     this.delegate = delegate;
36     this.statsMgr = statsMgr;
37   }
38
39   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
40     long start = System.currentTimeMillis();
41
42     try {
43       Object JavaDoc 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 JavaDoc e) {
51       Throwable JavaDoc ex = e.getTargetException();
52
53       statsMgr.increment(StatisticTypeConstants.MAPPING_FAILURE_COUNT);
54       Throwable JavaDoc 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 JavaDoc[] args) {
62     // Determine src and dest class name. The combination of src and dest
63
// class name will be used for the statistic entry key.
64
String JavaDoc srcClassName = null;
65     if (args[0] != null) {
66       srcClassName = args[0].getClass().getName();
67     }
68     String JavaDoc destClassName = null;
69     if (args[1] != null) {
70       if (args[1] instanceof Class JavaDoc) {
71         destClassName = ((Class JavaDoc) 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