1
16 package net.sf.ehcache.management.provider;
17
18 import java.util.concurrent.atomic.AtomicBoolean;
19
20 import net.sf.ehcache.CacheManager;
21 import net.sf.ehcache.config.Configuration;
22 import net.sf.ehcache.config.Configuration.Monitoring;
23 import net.sf.ehcache.management.sampled.SampledMBeanRegistrationProvider;
24 import net.sf.ehcache.terracotta.ClusteredInstanceFactory;
25
26
33 public class MBeanRegistrationProviderImpl implements MBeanRegistrationProvider {
34
35 private final Monitoring monitoring;
36 private final AtomicBoolean initialized = new AtomicBoolean(false);
37 private SampledMBeanRegistrationProvider sampledProvider;
38 private CacheManager cachedCacheManager;
39
40
45 public MBeanRegistrationProviderImpl(Configuration configuration) {
46 this.monitoring = configuration.getMonitoring();
47 }
48
49 private synchronized SampledMBeanRegistrationProvider getSampledMBeanRegistrationProvider() {
50 if (sampledProvider == null) {
51 sampledProvider = new SampledMBeanRegistrationProvider();
52 }
53 return sampledProvider;
54 }
55
56
59 public void initialize(CacheManager cacheManager, ClusteredInstanceFactory clusteredInstanceFactory)
60 throws MBeanRegistrationProviderException {
61 if (!initialized.getAndSet(true)) {
62 if (shouldRegisterMBeans()) {
63 getSampledMBeanRegistrationProvider().initialize(cacheManager, clusteredInstanceFactory);
64 }
65 this.cachedCacheManager = cacheManager;
66 } else {
67 throw new IllegalStateException("MBeanRegistrationProvider is already initialized");
68 }
69 }
70
71
74 public void reinitialize(ClusteredInstanceFactory clusteredInstanceFactory) throws MBeanRegistrationProviderException {
75 if (shouldRegisterMBeans()) {
76 if (getSampledMBeanRegistrationProvider().isAlive()) {
77 getSampledMBeanRegistrationProvider().reinitialize(clusteredInstanceFactory);
78 } else {
79 getSampledMBeanRegistrationProvider().initialize(cachedCacheManager, clusteredInstanceFactory);
80 }
81 }
82 }
83
84 private boolean shouldRegisterMBeans() {
85 switch (monitoring) {
86 case AUTODETECT:
87 return isTcActive();
88 case ON:
89 return true;
90 case OFF:
91 return false;
92 default:
93 throw new IllegalArgumentException("Unknown type of monitoring specified in config: " + monitoring);
94 }
95 }
96
97 private boolean isTcActive() {
98
99
100
101 return Boolean.getBoolean("tc.active");
102 }
103
104
107 public boolean isInitialized() {
108 return initialized.get();
109 }
110
111 }
112