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.util.Map;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import javax.management.MBeanNotificationInfo;
23 import javax.management.NotCompliantMBeanException;
24 import javax.management.Notification;
25 import javax.management.openmbean.TabularData;
26
27 import net.sf.ehcache.CacheManager;
28 import net.sf.ehcache.hibernate.management.api.EhcacheHibernateMBean;
29 import net.sf.ehcache.hibernate.management.api.EhcacheStats;
30 import net.sf.ehcache.hibernate.management.api.HibernateStats;
31
32 import org.hibernate.SessionFactory;
33
34 /***
35 * Implementation of the {@link EhcacheHibernateMBean}
36 *
37 * <p />
38 *
39 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
40 *
41 */
42 public class EhcacheHibernate extends BaseEmitterBean implements EhcacheHibernateMBean {
43 private static final MBeanNotificationInfo NOTIFICATION_INFO;
44
45 private final AtomicBoolean statsEnabled = new AtomicBoolean(true);
46 private EhcacheStats ehcacheStats;
47 private volatile HibernateStats hibernateStats = NullHibernateStats.INSTANCE;
48
49 static {
50 final String[] notifTypes = new String[] {};
51 final String name = Notification.class.getName();
52 final String description = "Ehcache Hibernate Statistics Event";
53 NOTIFICATION_INFO = new MBeanNotificationInfo(notifTypes, name, description);
54 }
55
56 /***
57 * Constructor accepting the backing {@link CacheManager}
58 *
59 * @param manager
60 * the backing {@link CacheManager}
61 * @throws NotCompliantMBeanException
62 */
63 public EhcacheHibernate(CacheManager manager) throws NotCompliantMBeanException {
64 super(EhcacheHibernateMBean.class);
65 ehcacheStats = new EhcacheStatsImpl(manager);
66 }
67
68 /***
69 * Enable hibernate statistics with the input session factory
70 *
71 */
72 public void enableHibernateStatistics(SessionFactory sessionFactory) {
73 try {
74 hibernateStats = new HibernateStatsImpl(sessionFactory);
75 } catch (Exception e) {
76 throw new RuntimeException(e);
77 }
78 }
79
80 /***
81 * {@inheritDoc}
82 *
83 */
84 public boolean isHibernateStatisticsSupported() {
85 return hibernateStats instanceof HibernateStatsImpl;
86 }
87
88 /***
89 * {@inheritDoc}
90 */
91 public void setStatisticsEnabled(boolean flag) {
92 if (flag) {
93 ehcacheStats.enableStats();
94 hibernateStats.enableStats();
95 } else {
96 ehcacheStats.disableStats();
97 hibernateStats.disableStats();
98 }
99 statsEnabled.set(flag);
100 sendNotification(HibernateStats.CACHE_STATISTICS_ENABLED, flag);
101 }
102
103 /***
104 * {@inheritDoc}
105 */
106 public boolean isStatisticsEnabled() {
107 return statsEnabled.get();
108 }
109
110 /***
111 * {@inheritDoc}
112 */
113 public void clearStats() {
114 ehcacheStats.clearStats();
115 hibernateStats.clearStats();
116 }
117
118 /***
119 * {@inheritDoc}
120 */
121 public void disableStats() {
122 setStatisticsEnabled(false);
123 }
124
125 /***
126 * {@inheritDoc}
127 */
128 public void enableStats() {
129 setStatisticsEnabled(true);
130 }
131
132 /***
133 * {@inheritDoc}
134 */
135 public void flushRegionCache(String region) {
136 ehcacheStats.flushRegionCache(region);
137 sendNotification(HibernateStats.CACHE_REGION_FLUSHED, region);
138 }
139
140 /***
141 * {@inheritDoc}
142 */
143 public void flushRegionCaches() {
144 ehcacheStats.flushRegionCaches();
145 sendNotification(HibernateStats.CACHE_FLUSHED);
146 }
147
148 /***
149 * {@inheritDoc}
150 */
151 public String generateActiveConfigDeclaration() {
152 return ehcacheStats.generateActiveConfigDeclaration();
153 }
154
155 /***
156 * {@inheritDoc}
157 */
158 public String generateActiveConfigDeclaration(String region) {
159 return ehcacheStats.generateActiveConfigDeclaration(region);
160 }
161
162 /***
163 * {@inheritDoc}
164 */
165 public long getCacheHitCount() {
166 return ehcacheStats.getCacheHitCount();
167 }
168
169 /***
170 * {@inheritDoc}
171 */
172 public double getCacheHitRate() {
173 return ehcacheStats.getCacheHitRate();
174 }
175
176 /***
177 * {@inheritDoc}
178 */
179 public long getCacheHitSample() {
180 return ehcacheStats.getCacheHitSample();
181 }
182
183 /***
184 * {@inheritDoc}
185 */
186 public long getCacheMissCount() {
187 return ehcacheStats.getCacheMissCount();
188 }
189
190 /***
191 * {@inheritDoc}
192 */
193 public double getCacheMissRate() {
194 return ehcacheStats.getCacheMissRate();
195 }
196
197 /***
198 * {@inheritDoc}
199 */
200 public long getCacheMissSample() {
201 return ehcacheStats.getCacheMissSample();
202 }
203
204 /***
205 * {@inheritDoc}
206 */
207 public long getCachePutCount() {
208 return ehcacheStats.getCachePutCount();
209 }
210
211 /***
212 * {@inheritDoc}
213 */
214 public double getCachePutRate() {
215 return ehcacheStats.getCachePutRate();
216 }
217
218 /***
219 * {@inheritDoc}
220 */
221 public long getCachePutSample() {
222 return ehcacheStats.getCachePutSample();
223 }
224
225 /***
226 * {@inheritDoc}
227 */
228 public TabularData getCacheRegionStats() {
229 return hibernateStats.getCacheRegionStats();
230 }
231
232 /***
233 * {@inheritDoc}
234 */
235 public long getCloseStatementCount() {
236 return hibernateStats.getCloseStatementCount();
237 }
238
239 /***
240 * {@inheritDoc}
241 */
242 public TabularData getCollectionStats() {
243 return hibernateStats.getCollectionStats();
244 }
245
246 /***
247 * {@inheritDoc}
248 */
249 public long getConnectCount() {
250 return hibernateStats.getConnectCount();
251 }
252
253 /***
254 * {@inheritDoc}
255 */
256 public TabularData getEntityStats() {
257 return hibernateStats.getEntityStats();
258 }
259
260 /***
261 * {@inheritDoc}
262 */
263 public long getFlushCount() {
264 return hibernateStats.getFlushCount();
265 }
266
267 /***
268 * {@inheritDoc}
269 */
270 public long getOptimisticFailureCount() {
271 return hibernateStats.getOptimisticFailureCount();
272 }
273
274 /***
275 * {@inheritDoc}
276 */
277 public String getOriginalConfigDeclaration() {
278 return ehcacheStats.getOriginalConfigDeclaration();
279 }
280
281 /***
282 * {@inheritDoc}
283 */
284 public String getOriginalConfigDeclaration(String region) {
285 return ehcacheStats.getOriginalConfigDeclaration(region);
286 }
287
288 /***
289 * {@inheritDoc}
290 */
291 public long getPrepareStatementCount() {
292 return hibernateStats.getPrepareStatementCount();
293 }
294
295 /***
296 * {@inheritDoc}
297 */
298 public long getQueryExecutionCount() {
299 return hibernateStats.getQueryExecutionCount();
300 }
301
302 /***
303 * {@inheritDoc}
304 */
305 public double getQueryExecutionRate() {
306 return hibernateStats.getQueryExecutionRate();
307 }
308
309 /***
310 * {@inheritDoc}
311 */
312 public long getQueryExecutionSample() {
313 return hibernateStats.getQueryExecutionSample();
314 }
315
316 /***
317 * {@inheritDoc}
318 */
319 public TabularData getQueryStats() {
320 return hibernateStats.getQueryStats();
321 }
322
323 /***
324 * {@inheritDoc}
325 */
326 public Map<String, Map<String, Object>> getRegionCacheAttributes() {
327 return ehcacheStats.getRegionCacheAttributes();
328 }
329
330 /***
331 * {@inheritDoc}
332 */
333 public Map<String, Object> getRegionCacheAttributes(String regionName) {
334 return ehcacheStats.getRegionCacheAttributes(regionName);
335 }
336
337 /***
338 * {@inheritDoc}
339 */
340 public int getRegionCacheMaxTTISeconds(String region) {
341 return ehcacheStats.getRegionCacheMaxTTISeconds(region);
342 }
343
344 /***
345 * {@inheritDoc}
346 */
347 public int getRegionCacheMaxTTLSeconds(String region) {
348 return ehcacheStats.getRegionCacheMaxTTLSeconds(region);
349 }
350
351 /***
352 * {@inheritDoc}
353 */
354 public int getRegionCacheOrphanEvictionPeriod(String region) {
355 return ehcacheStats.getRegionCacheOrphanEvictionPeriod(region);
356 }
357
358 /***
359 * {@inheritDoc}
360 */
361 public Map<String, int[]> getRegionCacheSamples() {
362 return ehcacheStats.getRegionCacheSamples();
363 }
364
365 /***
366 * {@inheritDoc}
367 */
368 public int getRegionCacheTargetMaxInMemoryCount(String region) {
369 return ehcacheStats.getRegionCacheTargetMaxInMemoryCount(region);
370 }
371
372 /***
373 * {@inheritDoc}
374 */
375 public int getRegionCacheTargetMaxTotalCount(String region) {
376 return ehcacheStats.getRegionCacheTargetMaxTotalCount(region);
377 }
378
379 /***
380 * {@inheritDoc}
381 */
382 public long getSessionCloseCount() {
383 return hibernateStats.getSessionCloseCount();
384 }
385
386 /***
387 * {@inheritDoc}
388 */
389 public long getSessionOpenCount() {
390 return hibernateStats.getSessionOpenCount();
391 }
392
393 /***
394 * {@inheritDoc}
395 */
396 public long getSuccessfulTransactionCount() {
397 return hibernateStats.getSuccessfulTransactionCount();
398 }
399
400 /***
401 * {@inheritDoc}
402 */
403 public String[] getTerracottaHibernateCacheRegionNames() {
404 return ehcacheStats.getTerracottaHibernateCacheRegionNames();
405 }
406
407 /***
408 * {@inheritDoc}
409 */
410 public long getTransactionCount() {
411 return hibernateStats.getTransactionCount();
412 }
413
414 /***
415 * {@inheritDoc}
416 */
417 public boolean isRegionCacheEnabled(String region) {
418 return ehcacheStats.isRegionCacheEnabled(region);
419 }
420
421 /***
422 * {@inheritDoc}
423 */
424 public void setRegionCachesEnabled(boolean enabled) {
425 ehcacheStats.setRegionCachesEnabled(enabled);
426 sendNotification(HibernateStats.CACHE_ENABLED, enabled);
427 }
428
429 /***
430 * {@inheritDoc}
431 */
432 public void setRegionCacheEnabled(String region, boolean enabled) {
433 ehcacheStats.setRegionCacheEnabled(region, enabled);
434 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
435 }
436
437 /***
438 * {@inheritDoc}
439 */
440 public boolean isRegionCacheLoggingEnabled(String region) {
441 return ehcacheStats.isRegionCacheLoggingEnabled(region);
442 }
443
444 /***
445 * {@inheritDoc}
446 */
447 public boolean isRegionCacheOrphanEvictionEnabled(String region) {
448 return ehcacheStats.isRegionCacheOrphanEvictionEnabled(region);
449 }
450
451 /***
452 * {@inheritDoc}
453 */
454 public boolean isRegionCachesEnabled() {
455 return ehcacheStats.isRegionCachesEnabled();
456 }
457
458 /***
459 * {@inheritDoc}
460 */
461 public boolean isTerracottaHibernateCache(String region) {
462 return ehcacheStats.isTerracottaHibernateCache(region);
463 }
464
465 /***
466 * {@inheritDoc}
467 */
468 public void setRegionCacheLoggingEnabled(String region, boolean loggingEnabled) {
469 ehcacheStats.setRegionCacheLoggingEnabled(region, loggingEnabled);
470 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
471 }
472
473 /***
474 * {@inheritDoc}
475 */
476 public void setRegionCacheMaxTTISeconds(String region, int maxTTISeconds) {
477 ehcacheStats.setRegionCacheMaxTTISeconds(region, maxTTISeconds);
478 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
479 }
480
481 /***
482 * {@inheritDoc}
483 */
484 public void setRegionCacheMaxTTLSeconds(String region, int maxTTLSeconds) {
485 ehcacheStats.setRegionCacheMaxTTLSeconds(region, maxTTLSeconds);
486 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
487 }
488
489 /***
490 * {@inheritDoc}
491 */
492 public void setRegionCacheTargetMaxInMemoryCount(String region, int targetMaxInMemoryCount) {
493 ehcacheStats.setRegionCacheTargetMaxInMemoryCount(region, targetMaxInMemoryCount);
494 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
495 }
496
497 /***
498 * {@inheritDoc}
499 */
500 public void setRegionCacheTargetMaxTotalCount(String region, int targetMaxTotalCount) {
501 ehcacheStats.setRegionCacheTargetMaxTotalCount(region, targetMaxTotalCount);
502 sendNotification(HibernateStats.CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region);
503 }
504
505 /***
506 * {@inheritDoc}
507 *
508 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getNumberOfElementsInMemory(java.lang.String)
509 */
510 public int getNumberOfElementsInMemory(String region) {
511 return ehcacheStats.getNumberOfElementsInMemory(region);
512 }
513
514 /***
515 * {@inheritDoc}
516 *
517 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getNumberOfElementsInMemory(java.lang.String)
518 */
519 public int getNumberOfElementsOffHeap(String region) {
520 return ehcacheStats.getNumberOfElementsOffHeap(region);
521 }
522
523 /***
524 * {@inheritDoc}
525 *
526 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getNumberOfElementsOnDisk(java.lang.String)
527 */
528 public int getNumberOfElementsOnDisk(String region) {
529 return ehcacheStats.getNumberOfElementsOnDisk(region);
530 }
531
532 /***
533 * {@inheritDoc}
534 *
535 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getMaxGetTimeMillis()
536 */
537 public long getMaxGetTimeMillis() {
538 return ehcacheStats.getMaxGetTimeMillis();
539 }
540
541 /***
542 * {@inheritDoc}
543 *
544 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getMaxGetTimeMillis(java.lang.String)
545 */
546 public long getMaxGetTimeMillis(String cacheName) {
547 return ehcacheStats.getMaxGetTimeMillis(cacheName);
548 }
549
550 /***
551 * {@inheritDoc}
552 *
553 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getMinGetTimeMillis()
554 */
555 public long getMinGetTimeMillis() {
556 return ehcacheStats.getMinGetTimeMillis();
557 }
558
559 /***
560 * {@inheritDoc}
561 *
562 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getMinGetTimeMillis(java.lang.String)
563 */
564 public long getMinGetTimeMillis(String cacheName) {
565 return ehcacheStats.getMinGetTimeMillis(cacheName);
566 }
567
568 /***
569 * {@inheritDoc}
570 *
571 * @see net.sf.ehcache.hibernate.management.api.EhcacheStats#getAverageGetTimeMillis(java.lang.String)
572 */
573 public float getAverageGetTimeMillis(String region) {
574 return ehcacheStats.getAverageGetTimeMillis(region);
575 }
576
577 /***
578 * {@inheritDoc}
579 */
580 @Override
581 protected void doDispose() {
582
583 }
584
585 /***
586 * @see BaseEmitterBean#getNotificationInfo()
587 */
588 @Override
589 public MBeanNotificationInfo[] getNotificationInfo() {
590 return new MBeanNotificationInfo[] {NOTIFICATION_INFO};
591 }
592 }