1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.jmx;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.management.InstanceAlreadyExistsException;
24  import javax.management.InstanceNotFoundException;
25  import javax.management.MBeanRegistrationException;
26  import javax.management.MBeanServer;
27  import javax.management.NotCompliantMBeanException;
28  import javax.management.ObjectInstance;
29  import javax.management.ObjectName;
30  
31  /**
32   * <a href="MBeanRegistry.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Michael C. Han
35   */
36  public class MBeanRegistry {
37  
38      public void destroy() throws Exception {
39          for (ObjectName objectName : _objectNameCache.values()) {
40              try {
41                  _mBeanServer.unregisterMBean(objectName);
42              }
43              catch (Exception e) {
44                  if (_log.isWarnEnabled()) {
45                      _log.warn(
46                          "Unable to unregister MBean" +
47                              objectName.getCanonicalName(),
48                          e);
49                  }
50              }
51          }
52  
53          _objectNameCache.clear();
54      }
55  
56      public ObjectName getObjectName(String objectNameCacheKey) {
57          return _objectNameCache.get(objectNameCacheKey);
58      }
59  
60      public ObjectInstance register(
61              String objectNameCacheKey, Object object, ObjectName objectName)
62          throws InstanceAlreadyExistsException, MBeanRegistrationException,
63                 NotCompliantMBeanException {
64  
65          ObjectInstance objectInstance = _mBeanServer.registerMBean(
66              object, objectName);
67  
68          _objectNameCache.put(
69              objectNameCacheKey, objectInstance.getObjectName());
70  
71          return objectInstance;
72      }
73  
74      public void replace(
75              String objectCacheKey, Object object, ObjectName objectName)
76          throws Exception {
77  
78          try {
79              register(objectCacheKey, object, objectName);
80          }
81          catch (InstanceAlreadyExistsException iaee) {
82              unregister(objectCacheKey, objectName);
83  
84              register(objectCacheKey, object, objectName);
85          }
86      }
87  
88      public void setMBeanServer(MBeanServer mBeanServer) {
89          _mBeanServer = mBeanServer;
90      }
91  
92      public void unregister(
93              String objectNameCacheKey, ObjectName defaultObjectName)
94          throws InstanceNotFoundException, MBeanRegistrationException {
95  
96          ObjectName objectName = _objectNameCache.get(objectNameCacheKey);
97  
98          if (objectName == null) {
99              _mBeanServer.unregisterMBean(defaultObjectName);
100         }
101         else {
102             _objectNameCache.remove(objectNameCacheKey);
103 
104             _mBeanServer.unregisterMBean(objectName);
105         }
106     }
107 
108     private static final Log _log = LogFactoryUtil.getLog(MBeanRegistry.class);
109 
110     private MBeanServer _mBeanServer;
111     private Map<String, ObjectName> _objectNameCache =
112         new HashMap<String, ObjectName>();
113 
114 }