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.lang.management.ManagementFactory;
20  import java.util.Properties;
21  
22  import javax.management.InstanceAlreadyExistsException;
23  import javax.management.MBeanServer;
24  import javax.management.ObjectName;
25  
26  import net.sf.ehcache.CacheException;
27  import net.sf.ehcache.CacheManager;
28  import net.sf.ehcache.Status;
29  import net.sf.ehcache.event.CacheManagerEventListener;
30  
31  import org.hibernate.SessionFactory;
32  import org.hibernate.cfg.Environment;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /***
37   * Implementation of {@link EhcacheHibernateMBeanRegistration}.
38   * Also implements {@link CacheManagerEventListener}. Deregisters mbeans when the associated cachemanager is shutdown.
39   * 
40   * <p />
41   * 
42   * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
43   * 
44   */
45  public class EhcacheHibernateMBeanRegistrationImpl implements EhcacheHibernateMBeanRegistration, CacheManagerEventListener {
46  
47      private static final Logger LOG = LoggerFactory.getLogger(EhcacheHibernateMBeanRegistrationImpl.class.getName());
48  
49      private static final int MAX_MBEAN_REGISTRATION_RETRIES = 50;
50      private String cacheManagerClusterUUID;
51      private String registeredCacheManagerName;
52      private Status status = Status.STATUS_UNINITIALISED;
53      private volatile EhcacheHibernate ehcacheHibernate;
54      private volatile ObjectName cacheManagerObjectName;
55  
56      /***
57       * {@inheritDoc}
58       */
59      public synchronized void registerMBeanForCacheManager(final CacheManager manager, final Properties properties) throws Exception {
60          String sessionFactoryName = properties.getProperty(Environment.SESSION_FACTORY_NAME);
61          String name = null;
62          if (sessionFactoryName == null) {
63              name = manager.getName();
64          } else {
65              name = "".equals(sessionFactoryName.trim()) ? manager.getName() : sessionFactoryName;
66          }
67          registerBean(name, manager);
68      }
69  
70      private void registerBean(String name, CacheManager manager) throws Exception {
71          ehcacheHibernate = new EhcacheHibernate(manager);
72          int tries = 0;
73          boolean success = false;
74          Exception exception = null;
75          cacheManagerClusterUUID = manager.getClusterUUID();
76          do {
77              this.registeredCacheManagerName = name;
78              if (tries != 0) {
79                  registeredCacheManagerName += "_" + tries;
80              }
81              try {
82                  // register the CacheManager MBean
83                  MBeanServer mBeanServer = getMBeanServer();
84                  cacheManagerObjectName = EhcacheHibernateMbeanNames.getCacheManagerObjectName(cacheManagerClusterUUID,
85                          registeredCacheManagerName);
86                  mBeanServer.registerMBean(ehcacheHibernate, cacheManagerObjectName);
87                  success = true;
88                  break;
89              } catch (InstanceAlreadyExistsException e) {
90                  success = false;
91                  exception = e;
92              }
93              tries++;
94          } while (tries < MAX_MBEAN_REGISTRATION_RETRIES);
95          if (!success) {
96              throw new Exception("Cannot register mbean for CacheManager with name" + manager.getName() + " after "
97                      + MAX_MBEAN_REGISTRATION_RETRIES + " retries. Last tried name=" + registeredCacheManagerName, exception);
98          }
99          status = status.STATUS_ALIVE;
100     }
101 
102     private MBeanServer getMBeanServer() {
103         return ManagementFactory.getPlatformMBeanServer();
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     public void enableHibernateStatisticsSupport(SessionFactory sessionFactory) {
110         ehcacheHibernate.enableHibernateStatistics(sessionFactory);
111     }
112 
113     /***
114      * {@inheritDoc}
115      */
116     public synchronized void dispose() throws CacheException {
117         if (status == Status.STATUS_SHUTDOWN) {
118             return;
119         }
120 
121         try {
122             getMBeanServer().unregisterMBean(cacheManagerObjectName);
123         } catch (Exception e) {
124             LOG.warn("Error unregistering object instance " + cacheManagerObjectName + " . Error was " + e.getMessage(), e);
125         }
126         ehcacheHibernate = null;
127         cacheManagerObjectName = null;
128         status = Status.STATUS_SHUTDOWN;
129     }
130 
131     /***
132      * {@inheritDoc}
133      */
134     public synchronized Status getStatus() {
135         return status;
136     }
137 
138     /***
139      * No-op in this case
140      */
141     public void init() throws CacheException {
142         // no-op
143     }
144 
145     /***
146      * No-op in this case
147      */
148     public void notifyCacheAdded(String cacheName) {
149         // no-op
150     }
151 
152     /***
153      * No-op in this case
154      */
155     public void notifyCacheRemoved(String cacheName) {
156         // no-op
157     }
158 
159 }