KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > jmx > StatisticsService


1 //$Id: StatisticsService.java,v 1.10 2005/04/25 07:33:12 oneovthafew Exp $
2
package org.hibernate.jmx;
3
4 import javax.naming.InitialContext JavaDoc;
5 import javax.naming.NameNotFoundException JavaDoc;
6 import javax.naming.NamingException JavaDoc;
7 import javax.naming.Reference JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.hibernate.SessionFactory;
12 import org.hibernate.impl.SessionFactoryObjectFactory;
13 import org.hibernate.stat.CollectionStatistics;
14 import org.hibernate.stat.EntityStatistics;
15 import org.hibernate.stat.QueryStatistics;
16 import org.hibernate.stat.SecondLevelCacheStatistics;
17 import org.hibernate.stat.Statistics;
18 import org.hibernate.stat.StatisticsImpl;
19
20 /**
21  * JMX service for Hibernate statistics<br>
22  * <br>
23  * Register this MBean in your JMX server for a specific session factory
24  * <pre>
25  * //build the ObjectName you want
26  * Hashtable tb = new Hashtable();
27  * tb.put("type", "statistics");
28  * tb.put("sessionFactory", "myFinancialApp");
29  * ObjectName on = new ObjectName("hibernate", tb);
30  * StatisticsService stats = new StatisticsService();
31  * stats.setSessionFactory(sessionFactory);
32  * server.registerMBean(stats, on);
33  * </pre>
34  * And call the MBean the way you want<br>
35  * <br>
36  * Register this MBean in your JMX server with no specific session factory
37  * <pre>
38  * //build the ObjectName you want
39  * Hashtable tb = new Hashtable();
40  * tb.put("type", "statistics");
41  * tb.put("sessionFactory", "myFinancialApp");
42  * ObjectName on = new ObjectName("hibernate", tb);
43  * StatisticsService stats = new StatisticsService();
44  * server.registerMBean(stats, on);
45  * </pre>
46  * And call the MBean by providing the <code>SessionFactoryJNDIName</code> first.
47  * Then the session factory will be retrieved from JNDI and the statistics
48  * loaded.
49  *
50  * @author Emmanuel Bernard
51  */

52 public class StatisticsService implements StatisticsServiceMBean {
53     
54     //TODO: We probably should have a StatisticsNotPublishedException, to make it clean
55

56     SessionFactory sf;
57     String JavaDoc sfJNDIName;
58     Log log = LogFactory.getLog(StatisticsService.class);
59     Statistics stats = new StatisticsImpl();
60
61     /**
62      * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String)
63      */

64     public void setSessionFactoryJNDIName(String JavaDoc sfJNDIName) {
65         this.sfJNDIName = sfJNDIName;
66         try {
67             Object JavaDoc obj = new InitialContext JavaDoc().lookup(sfJNDIName);
68             if (obj instanceof Reference JavaDoc) {
69                 Reference JavaDoc ref = (Reference JavaDoc) obj;
70                 setSessionFactory( (SessionFactory) SessionFactoryObjectFactory.getInstance( (String JavaDoc) ref.get(0).getContent() ) );
71             }
72             else {
73                 setSessionFactory( (SessionFactory) obj );
74             }
75         }
76         catch (NameNotFoundException JavaDoc e) {
77             log.error("No session factory with JNDI name " + sfJNDIName, e);
78             setSessionFactory(null);
79         }
80         catch (NamingException JavaDoc e) {
81             log.error("Error while accessing session factory with JNDI name " + sfJNDIName, e);
82             setSessionFactory(null);
83         }
84         catch (ClassCastException JavaDoc e) {
85             log.error("JNDI name " + sfJNDIName + " does not handle a session factory reference", e);
86             setSessionFactory(null);
87         }
88     }
89     
90     /**
91      * Useful to init this MBean wo a JNDI session factory name
92      *
93      * @param sf session factory to register
94      */

95     public void setSessionFactory(SessionFactory sf) {
96         this.sf = sf;
97         if (sf == null) {
98             stats = new StatisticsImpl();
99         }
100         else {
101             stats = sf.getStatistics();
102         }
103         
104     }
105     /**
106      * @see StatisticsServiceMBean#clear()
107      */

108     public void clear() {
109         stats.clear();
110     }
111     /**
112      * @see StatisticsServiceMBean#getEntityStatistics(java.lang.String)
113      */

114     public EntityStatistics getEntityStatistics(String JavaDoc entityName) {
115         return stats.getEntityStatistics(entityName);
116     }
117     /**
118      * @see StatisticsServiceMBean#getCollectionStatistics(java.lang.String)
119      */

120     public CollectionStatistics getCollectionStatistics(String JavaDoc role) {
121         return stats.getCollectionStatistics(role);
122     }
123     /**
124      * @see StatisticsServiceMBean#getSecondLevelCacheStatistics(java.lang.String)
125      */

126     public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String JavaDoc regionName) {
127         return stats.getSecondLevelCacheStatistics(regionName);
128     }
129     /**
130      * @see StatisticsServiceMBean#getQueryStatistics(java.lang.String)
131      */

132     public QueryStatistics getQueryStatistics(String JavaDoc hql) {
133         return stats.getQueryStatistics(hql);
134     }
135     /**
136      * @see StatisticsServiceMBean#getEntityDeleteCount()
137      */

138     public long getEntityDeleteCount() {
139         return stats.getEntityDeleteCount();
140     }
141     /**
142      * @see StatisticsServiceMBean#getEntityInsertCount()
143      */

144     public long getEntityInsertCount() {
145         return stats.getEntityInsertCount();
146     }
147     /**
148      * @see StatisticsServiceMBean#getEntityLoadCount()
149      */

150     public long getEntityLoadCount() {
151         return stats.getEntityLoadCount();
152     }
153     /**
154      * @see StatisticsServiceMBean#getEntityFetchCount()
155      */

156     public long getEntityFetchCount() {
157         return stats.getEntityFetchCount();
158     }
159     /**
160      * @see StatisticsServiceMBean#getEntityUpdateCount()
161      */

162     public long getEntityUpdateCount() {
163         return stats.getEntityUpdateCount();
164     }
165     /**
166      * @see StatisticsServiceMBean#getQueryExecutionCount()
167      */

168     public long getQueryExecutionCount() {
169         return stats.getQueryExecutionCount();
170     }
171     public long getQueryCacheHitCount() {
172         return stats.getQueryCacheHitCount();
173     }
174     public long getQueryExecutionMaxTime() {
175         return stats.getQueryExecutionMaxTime();
176     }
177     public long getQueryCacheMissCount() {
178         return stats.getQueryCacheMissCount();
179     }
180     public long getQueryCachePutCount() {
181         return stats.getQueryCachePutCount();
182     }
183     /**
184      * @see StatisticsServiceMBean#getFlushCount()
185      */

186     public long getFlushCount() {
187         return stats.getFlushCount();
188     }
189     /**
190      * @see StatisticsServiceMBean#getConnectCount()
191      */

192     public long getConnectCount() {
193         return stats.getConnectCount();
194     }
195     /**
196      * @see StatisticsServiceMBean#getSecondLevelCacheHitCount()
197      */

198     public long getSecondLevelCacheHitCount() {
199         return stats.getSecondLevelCacheHitCount();
200     }
201     /**
202      * @see StatisticsServiceMBean#getSecondLevelCacheMissCount()
203      */

204     public long getSecondLevelCacheMissCount() {
205         return stats.getSecondLevelCacheMissCount();
206     }
207     /**
208      * @see StatisticsServiceMBean#getSecondLevelCachePutCount()
209      */

210     public long getSecondLevelCachePutCount() {
211         return stats.getSecondLevelCachePutCount();
212     }
213     /**
214      * @see StatisticsServiceMBean#getSessionCloseCount()
215      */

216     public long getSessionCloseCount() {
217         return stats.getSessionCloseCount();
218     }
219     /**
220      * @see StatisticsServiceMBean#getSessionOpenCount()
221      */

222     public long getSessionOpenCount() {
223         return stats.getSessionOpenCount();
224     }
225     /**
226      * @see StatisticsServiceMBean#getCollectionLoadCount()
227      */

228     public long getCollectionLoadCount() {
229         return stats.getCollectionLoadCount();
230     }
231     /**
232      * @see StatisticsServiceMBean#getCollectionFetchCount()
233      */

234     public long getCollectionFetchCount() {
235         return stats.getCollectionFetchCount();
236     }
237     /**
238      * @see StatisticsServiceMBean#getCollectionUpdateCount()
239      */

240     public long getCollectionUpdateCount() {
241         return stats.getCollectionUpdateCount();
242     }
243     /**
244      * @see StatisticsServiceMBean#getCollectionRemoveCount()
245      */

246     public long getCollectionRemoveCount() {
247         return stats.getCollectionRemoveCount();
248     }
249     /**
250      * @see StatisticsServiceMBean#getCollectionRecreateCount()
251      */

252     public long getCollectionRecreateCount() {
253         return stats.getCollectionRecreateCount();
254     }
255     /**
256      * @see StatisticsServiceMBean#getStartTime()
257      */

258     public long getStartTime() {
259         return stats.getStartTime();
260     }
261
262     /**
263      * @see StatisticsServiceMBean#isStatisticsEnabled()
264      */

265     public boolean isStatisticsEnabled() {
266         return stats.isStatisticsEnabled();
267     }
268
269     /**
270      * @see StatisticsServiceMBean#setStatisticsEnabled(boolean)
271      */

272     public void setStatisticsEnabled(boolean enable) {
273         stats.setStatisticsEnabled(enable);
274     }
275     
276     public void logSummary() {
277         stats.logSummary();
278     }
279
280     public String JavaDoc[] getCollectionRoleNames() {
281         return stats.getCollectionRoleNames();
282     }
283
284     public String JavaDoc[] getEntityNames() {
285         return stats.getEntityNames();
286     }
287
288     public String JavaDoc[] getQueries() {
289         return stats.getQueries();
290     }
291
292     public String JavaDoc[] getSecondLevelCacheRegionNames() {
293         return stats.getSecondLevelCacheRegionNames();
294     }
295     
296     public long getSuccessfulTransactionCount() {
297         return stats.getSuccessfulTransactionCount();
298     }
299     public long getTransactionCount() {
300         return stats.getTransactionCount();
301     }
302
303     public long getCloseStatementCount() {
304         return stats.getCloseStatementCount();
305     }
306     public long getPrepareStatementCount() {
307         return stats.getPrepareStatementCount();
308     }
309 }
310
Popular Tags