View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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  
17  package net.sf.ehcache.hibernate.management.impl;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.management.openmbean.CompositeData;
25  import javax.management.openmbean.CompositeDataSupport;
26  import javax.management.openmbean.CompositeType;
27  import javax.management.openmbean.OpenDataException;
28  import javax.management.openmbean.OpenType;
29  import javax.management.openmbean.SimpleType;
30  import javax.management.openmbean.TabularData;
31  import javax.management.openmbean.TabularDataSupport;
32  import javax.management.openmbean.TabularType;
33  
34  import org.hibernate.stat.EntityStatistics;
35  
36  /***
37   * When we only support Java 6, all of this OpenMBean scaffolding can be removed in favor or MXBeans.
38   * 
39   * @author gkeim
40   */
41  public class EntityStats implements Serializable {
42    private static final String        COMPOSITE_TYPE_NAME        = "EntityStats";
43    private static final String        COMPOSITE_TYPE_DESCRIPTION = "Statistics per Entity";
44    private static final String[]      ITEM_NAMES                 = new String[] {"name", "shortName", "loadCount",
45        "updateCount", "insertCount", "deleteCount", "fetchCount", "optimisticFailureCount", };
46    private static final String[]      ITEM_DESCRIPTIONS          = new String[] {"name", "shortName", "loadCount",
47        "updateCount", "insertCount", "deleteCount", "fetchCount", "optimisticFailureCount", };
48    private static final OpenType[]    ITEM_TYPES                 = new OpenType[] {SimpleType.STRING,
49        SimpleType.STRING, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
50        SimpleType.LONG, };
51    private static final CompositeType COMPOSITE_TYPE;
52    private static final String        TABULAR_TYPE_NAME          = "Statistics by Entity";
53    private static final String        TABULAR_TYPE_DESCRIPTION   = "All Entity Statistics";
54    private static final String[]      INDEX_NAMES                = new String[] {"name", };
55    private static final TabularType   TABULAR_TYPE;
56  
57    static {
58      try {
59        COMPOSITE_TYPE = new CompositeType(COMPOSITE_TYPE_NAME, COMPOSITE_TYPE_DESCRIPTION, ITEM_NAMES,
60                                           ITEM_DESCRIPTIONS, ITEM_TYPES);
61        TABULAR_TYPE = new TabularType(TABULAR_TYPE_NAME, TABULAR_TYPE_DESCRIPTION, COMPOSITE_TYPE, INDEX_NAMES);
62      } catch (OpenDataException e) {
63        throw new RuntimeException(e);
64      }
65    }
66  
67    /***
68     * name
69     */
70    protected final String name;
71    
72    /***
73     * shortName
74     */
75    protected final String shortName;
76    
77    /***
78     * loadCount
79     */
80    protected long loadCount;
81    
82    /***
83     * updateCount
84     */
85    protected long updateCount;
86    
87    /***
88     * insertCount
89     */
90    protected long insertCount;
91    
92    /***
93     * deleteCount
94     */
95    protected long deleteCount;
96    
97    /***
98     * fetchCount
99     */
100   protected long fetchCount;
101   
102   /***
103    * optimisticFailureCount
104    */
105   protected long optimisticFailureCount;
106 
107   /***
108    * @param name
109    */
110   public EntityStats(String name) {
111     this.name = name;
112     this.shortName = CacheRegionUtils.determineShortName(name);
113   }
114 
115   /***
116    * @param name
117    * @param src
118    */
119   public EntityStats(String name, EntityStatistics src) {
120     this(name);
121     
122     try {
123       this.loadCount = BeanUtils.getLongBeanProperty(src, "loadCount");
124       this.updateCount = BeanUtils.getLongBeanProperty(src, "updateCount");
125       this.insertCount = BeanUtils.getLongBeanProperty(src, "insertCount");
126       this.deleteCount = BeanUtils.getLongBeanProperty(src, "deleteCount");
127       this.fetchCount = BeanUtils.getLongBeanProperty(src, "fetchCount");
128       this.optimisticFailureCount = BeanUtils.getLongBeanProperty(src, "optimisticFailureCount");
129     } catch (Exception e) {
130       e.printStackTrace();
131       throw new RuntimeException("Exception retrieving statistics", e);
132     }
133   }
134 
135   /***
136    * @param cData
137    */
138   public EntityStats(final CompositeData cData) {
139     int i = 0;
140     name = (String) cData.get(ITEM_NAMES[i++]);
141     shortName = (String) cData.get(ITEM_NAMES[i++]);
142     loadCount = (Long) cData.get(ITEM_NAMES[i++]);
143     updateCount = (Long) cData.get(ITEM_NAMES[i++]);
144     insertCount = (Long) cData.get(ITEM_NAMES[i++]);
145     deleteCount = (Long) cData.get(ITEM_NAMES[i++]);
146     fetchCount = (Long) cData.get(ITEM_NAMES[i++]);
147     optimisticFailureCount = (Long) cData.get(ITEM_NAMES[i++]);
148   }
149 
150   private static int safeParseInt(String s) {
151       try {
152           return Integer.parseInt(s);
153       } catch (Exception e) {
154           return -1;
155       }
156   }
157   
158   /***
159    * @param stats
160    */
161   public void add(EntityStats stats) {
162     loadCount += stats.getLoadCount();
163     updateCount += stats.getUpdateCount();
164     insertCount += stats.getInsertCount();
165     deleteCount += stats.getDeleteCount();
166     fetchCount += stats.getFetchCount();
167     optimisticFailureCount += stats.getOptimisticFailureCount();
168   }
169 
170   /***
171    * toString
172    */
173   @Override
174   public String toString() {
175     return "name=" + name + ", shortName=" + shortName + ",loadCount=" + loadCount + ", updateCount=" + updateCount
176            + ", insertCount=" + insertCount + ", deleteCount=" + deleteCount + ", fetchCount=" + fetchCount
177            + ", optimisticFailureCount" + optimisticFailureCount;
178   }
179 
180   /***
181    * getName
182    */
183   public String getName() {
184     return name;
185   }
186 
187   /***
188    * getShortName
189    */
190   public String getShortName() {
191     return shortName;
192   }
193 
194   /***
195    * getLoadCount
196    */
197   public long getLoadCount() {
198     return loadCount;
199   }
200 
201   /***
202    * getUpdateCount
203    */
204   public long getUpdateCount() {
205     return updateCount;
206   }
207 
208   /***
209    * getInsertCount
210    */
211   public long getInsertCount() {
212     return insertCount;
213   }
214 
215   /***
216    * getDeleteCount
217    */
218   public long getDeleteCount() {
219     return deleteCount;
220   }
221 
222   /***
223    * getFetchCount
224    */
225   public long getFetchCount() {
226     return fetchCount;
227   }
228 
229   /***
230    * getOptimisticFailureCount
231    */
232   public long getOptimisticFailureCount() {
233     return optimisticFailureCount;
234   }
235 
236   /***
237    * toCompositeData
238    */
239   public CompositeData toCompositeData() {
240     try {
241       return new CompositeDataSupport(COMPOSITE_TYPE, ITEM_NAMES, new Object[] {name, shortName, loadCount,
242           updateCount, insertCount, deleteCount, fetchCount, optimisticFailureCount, });
243     } catch (OpenDataException e) {
244       throw new RuntimeException(e);
245     }
246   }
247 
248   /***
249    * newTabularDataInstance
250    */
251   public static TabularData newTabularDataInstance() {
252     return new TabularDataSupport(TABULAR_TYPE);
253   }
254 
255   /***
256    * fromTabularData
257    */
258   public static EntityStats[] fromTabularData(final TabularData tabularData) {
259     final List<EntityStats> countList = new ArrayList(tabularData.size());
260     for (final Iterator pos = tabularData.values().iterator(); pos.hasNext();) {
261       countList.add(new EntityStats((CompositeData) pos.next()));
262     }
263     return countList.toArray(new EntityStats[countList.size()]);
264   }
265 
266 }