1 /**
2  *  Copyright 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 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 /**
27  * Implementation of {@link MBeanRegistrationProvider}
28  * <p />
29  *
30  * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
31  * @since 1.7.1
32  */

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     /**
41      * Constructor accepting the {@link Configuration}
42      *
43      * @param configuration
44      */

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     /**
57      * {@inheritDoc}
58      */

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     /**
72      * {@inheritDoc}
73      */

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         // do not use a static final to store this in a class.
99         // If unclustered cacheManager's are created before creating
100         // clustered ones, mbeans will never get registered (in standalone context)
101         return Boolean.getBoolean("tc.active");
102     }
103
104     /**
105      * {@inheritDoc}
106      */

107     public boolean isInitialized() {
108         return initialized.get();
109     }
110
111 }
112