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.CollectionStatistics;
35  
36  /***
37   * CollectionStats
38   * @author gkeim
39   */
40  public class CollectionStats implements Serializable {
41    private static final String        COMPOSITE_TYPE_NAME        = "CollectionsStats";
42    private static final String        COMPOSITE_TYPE_DESCRIPTION = "Statistics per Collections";
43    private static final String[]      ITEM_NAMES                 = new String[] {"roleName", "shortName", "loadCount",
44        "fetchCount", "updateCount", "removeCount", "recreateCount", };
45    private static final String[]      ITEM_DESCRIPTIONS          = new String[] {"roleName", "shortName", "loadCount",
46        "fetchCount", "updateCount", "removeCount", "recreateCount", };
47    private static final OpenType[]    ITEM_TYPES                 = new OpenType[] {SimpleType.STRING,
48        SimpleType.STRING, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, };
49    private static final CompositeType COMPOSITE_TYPE;
50    private static final String        TABULAR_TYPE_NAME          = "Statistics by Collection";
51    private static final String        TABULAR_TYPE_DESCRIPTION   = "All Collection Statistics";
52    private static final String[]      INDEX_NAMES                = new String[] {"roleName", };
53    private static final TabularType   TABULAR_TYPE;
54  
55    static {
56      try {
57        COMPOSITE_TYPE = new CompositeType(COMPOSITE_TYPE_NAME, COMPOSITE_TYPE_DESCRIPTION, ITEM_NAMES,
58                                           ITEM_DESCRIPTIONS, ITEM_TYPES);
59        TABULAR_TYPE = new TabularType(TABULAR_TYPE_NAME, TABULAR_TYPE_DESCRIPTION, COMPOSITE_TYPE, INDEX_NAMES);
60      } catch (OpenDataException e) {
61        throw new RuntimeException(e);
62      }
63    }
64  
65    /***
66     * roleName
67     */
68    protected final String roleName;
69    
70    /***
71     * shortName
72     */
73    protected final String shortName;
74    
75    /***
76     * loadCount
77     */
78    protected long loadCount;
79    
80    /***
81     * fetchCount
82     */
83    protected long fetchCount;
84    
85    /***
86     * updateCount
87     */
88    protected long updateCount;
89    
90    /***
91     * removeCount
92     */
93    protected long removeCount;
94    
95    /***
96     * recreateCount
97     */
98    protected long recreateCount;
99    
100 
101   /***
102    * @param roleName
103    */
104   public CollectionStats(String roleName) {
105     this.roleName = roleName;
106     this.shortName = CacheRegionUtils.determineShortName(roleName);
107   }
108 
109   /***
110    * @param name
111    * @param src
112    */
113   public CollectionStats(String name, CollectionStatistics src) {
114     this(name);
115 
116     try {
117       this.loadCount = BeanUtils.getLongBeanProperty(src, "loadCount");
118       this.fetchCount = BeanUtils.getLongBeanProperty(src, "fetchCount");
119       this.updateCount = BeanUtils.getLongBeanProperty(src, "updateCount");
120       this.removeCount = BeanUtils.getLongBeanProperty(src, "removeCount");
121       this.recreateCount = BeanUtils.getLongBeanProperty(src, "recreateCount");
122     } catch (Exception e) {
123       e.printStackTrace();
124       throw new RuntimeException("Exception retrieving statistics", e);
125     }
126   }
127 
128   /***
129    * @param cData
130    */
131   public CollectionStats(final CompositeData cData) {
132     int i = 0;
133     roleName = (String) cData.get(ITEM_NAMES[i++]);
134     shortName = (String) cData.get(ITEM_NAMES[i++]);
135     loadCount = (Long) cData.get(ITEM_NAMES[i++]);
136     fetchCount = (Long) cData.get(ITEM_NAMES[i++]);
137     updateCount = (Long) cData.get(ITEM_NAMES[i++]);
138     removeCount = (Long) cData.get(ITEM_NAMES[i++]);
139     recreateCount = (Long) cData.get(ITEM_NAMES[i++]);
140   }
141 
142   private static int safeParseInt(String s) {
143       try {
144           return Integer.parseInt(s);
145       } catch (Exception e) {
146           return -1;
147       }
148   }
149   
150   /***
151    * @param stats
152    */
153   public void add(CollectionStats stats) {
154     loadCount += stats.getLoadCount();
155     fetchCount += stats.getFetchCount();
156     updateCount += stats.getUpdateCount();
157     removeCount += stats.getRemoveCount();
158     recreateCount += stats.getRecreateCount();
159   }
160 
161   /***
162    * toString
163    */
164   @Override
165   public String toString() {
166     return "roleName=" + roleName + "shortName=" + shortName + ", loadCount=" + loadCount + ", fetchCount="
167            + fetchCount + ", updateCount=" + updateCount + ", removeCount=" + removeCount + ", recreateCount"
168            + recreateCount;
169   }
170 
171   /***
172    * getRoleName
173    */
174   public String getRoleName() {
175     return roleName;
176   }
177 
178   /***
179    * getShortName
180    */
181   public String getShortName() {
182     return shortName;
183   }
184 
185   /***
186    * getLoadCount
187    */
188   public long getLoadCount() {
189     return loadCount;
190   }
191 
192   /***
193    * getFetchCount
194    */
195   public long getFetchCount() {
196     return fetchCount;
197   }
198 
199   /***
200    * getUpdateCount
201    */
202   public long getUpdateCount() {
203     return updateCount;
204   }
205 
206   /***
207    * getRemoveCount
208    */
209   public long getRemoveCount() {
210     return removeCount;
211   }
212 
213   /***
214    * getRecreateCount
215    */
216   public long getRecreateCount() {
217     return recreateCount;
218   }
219 
220   /***
221    * toCompositeData
222    */
223   public CompositeData toCompositeData() {
224     try {
225       return new CompositeDataSupport(COMPOSITE_TYPE, ITEM_NAMES, new Object[] {roleName, shortName, loadCount,
226           fetchCount, updateCount, removeCount, recreateCount, });
227     } catch (OpenDataException e) {
228       throw new RuntimeException(e);
229     }
230   }
231 
232   /***
233    * newTabularDataInstance
234    */
235   public static TabularData newTabularDataInstance() {
236     return new TabularDataSupport(TABULAR_TYPE);
237   }
238 
239   /***
240    * fromTabularData
241    */
242   public static CollectionStats[] fromTabularData(final TabularData tabularData) {
243     final List<CollectionStats> countList = new ArrayList(tabularData.size());
244     for (final Iterator pos = tabularData.values().iterator(); pos.hasNext();) {
245       countList.add(new CollectionStats((CompositeData) pos.next()));
246     }
247     return countList.toArray(new CollectionStats[countList.size()]);
248   }
249 
250 }