KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > jmx > DozerStatisticsController


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.jmx;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import net.sf.dozer.util.mapping.config.GlobalSettings;
23 import net.sf.dozer.util.mapping.config.Settings;
24 import net.sf.dozer.util.mapping.stats.GlobalStatistics;
25 import net.sf.dozer.util.mapping.stats.StatisticEntry;
26 import net.sf.dozer.util.mapping.stats.StatisticTypeConstants;
27 import net.sf.dozer.util.mapping.stats.StatisticsManagerIF;
28
29
30 /**
31  * @author tierney.matt
32  */

33 public class DozerStatisticsController implements DozerStatisticsControllerMBean {
34   private final StatisticsManagerIF statsMgr = GlobalStatistics.getInstance().getStatsMgr();
35   private final Settings globalSettings = GlobalSettings.getInstance().getSettings();
36   
37   public void clearAll() {
38     statsMgr.clearAll();
39   }
40   
41   public boolean isStatisticsEnabled() {
42     return globalSettings.isStatisticsEnabled();
43   }
44   
45   public void setStatisticsEnabled(boolean statisticsEnabled) {
46     globalSettings.setStatisticsEnabled(statisticsEnabled);
47   }
48   
49   public long getMappingSuccessCount() {
50     return getStatisticValue(StatisticTypeConstants.MAPPING_SUCCESS_COUNT);
51   }
52   
53   public long getMappingFailureCount() {
54     return getStatisticValue(StatisticTypeConstants.MAPPING_FAILURE_COUNT);
55   }
56   
57   public long getMapperInstancesCount() {
58     return getStatisticValue(StatisticTypeConstants.MAPPER_INSTANCES_COUNT);
59   }
60   
61   public long getMappingOverallTime() {
62     return getStatisticValue(StatisticTypeConstants.MAPPING_TIME);
63   }
64   
65   public Set JavaDoc getMappingFailureExceptionTypes() {
66     return getStatisticEntries(StatisticTypeConstants.MAPPING_FAILURE_EX_TYPE_COUNT);
67   }
68   
69   public Set JavaDoc getMappingFailureTypes() {
70     return getStatisticEntries(StatisticTypeConstants.MAPPING_FAILURE_TYPE_COUNT);
71   }
72   
73   public Set JavaDoc getCacheHitCount() {
74     return getStatisticEntries(StatisticTypeConstants.CACHE_HIT_COUNT);
75   }
76   
77   public Set JavaDoc getCacheMissCount() {
78     return getStatisticEntries(StatisticTypeConstants.CACHE_MISS_COUNT);
79   }
80
81   public long getFieldMappingSuccessCount() {
82     return getStatisticValue(StatisticTypeConstants.FIELD_MAPPING_SUCCESS_COUNT);
83   }
84   
85   public long getFieldMappingFailureCount() {
86     return getStatisticValue(StatisticTypeConstants.FIELD_MAPPING_FAILURE_COUNT);
87   }
88   
89   public long getFieldMappingFailureIgnoredCount() {
90     return getStatisticValue(StatisticTypeConstants.FIELD_MAPPING_FAILURE_IGNORED_COUNT);
91   }
92   
93   public Set JavaDoc getStatisticTypes() {
94     return statsMgr.getStatisticTypes();
95   }
96   
97   public double getMappingAverageTime() {
98     double totalTime = getStatisticValue(StatisticTypeConstants.MAPPING_TIME);
99     double totalCount = getStatisticValue(StatisticTypeConstants.MAPPING_SUCCESS_COUNT);
100     return totalTime/totalCount;
101   }
102   
103   public Set JavaDoc getStatisticEntries(String JavaDoc statisticType) {
104     Set JavaDoc result = new TreeSet JavaDoc();
105     if (statsMgr.statisticExists(statisticType)) {
106       Set JavaDoc entries = statsMgr.getStatisticEntries(statisticType);
107       Iterator JavaDoc iter = entries.iterator();
108       while(iter.hasNext()) {
109         StatisticEntry entry = (StatisticEntry) iter.next();
110         result.add(entry.getKey().toString() + ":Count " + entry.getValue());
111       }
112     }
113     return result;
114   }
115   
116   public void logStatistics() {
117     statsMgr.logStatistics();
118   }
119   
120   public String JavaDoc dumpStatistics() {
121     return statsMgr.getStatistics().toString();
122   }
123   
124   protected long getStatisticValue(String JavaDoc statisticType) {
125     long result = 0;
126     if(statsMgr.statisticExists(statisticType)) {
127       result = statsMgr.getStatisticValue(statisticType);
128     }
129     return result;
130   }
131 }
132
Popular Tags