001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.jmx;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.management.InstanceAlreadyExistsException;
024    import javax.management.InstanceNotFoundException;
025    import javax.management.MBeanRegistrationException;
026    import javax.management.MBeanServer;
027    import javax.management.NotCompliantMBeanException;
028    import javax.management.ObjectInstance;
029    import javax.management.ObjectName;
030    
031    /**
032     * @author Michael C. Han
033     */
034    public class MBeanRegistry {
035    
036            public void destroy() throws Exception {
037                    synchronized (_objectNameCache) {
038                            for (ObjectName objectName : _objectNameCache.values()) {
039                                    try {
040                                            _mBeanServer.unregisterMBean(objectName);
041                                    }
042                                    catch (Exception e) {
043                                            if (_log.isWarnEnabled()) {
044                                                    _log.warn(
045                                                            "Unable to unregister MBean" +
046                                                                    objectName.getCanonicalName(),
047                                                            e);
048                                            }
049                                    }
050                            }
051    
052                            _objectNameCache.clear();
053                    }
054            }
055    
056            public ObjectName getObjectName(String objectNameCacheKey) {
057                    return _objectNameCache.get(objectNameCacheKey);
058            }
059    
060            public ObjectInstance register(
061                            String objectNameCacheKey, Object object, ObjectName objectName)
062                    throws InstanceAlreadyExistsException, MBeanRegistrationException,
063                               NotCompliantMBeanException {
064    
065                    ObjectInstance objectInstance = _mBeanServer.registerMBean(
066                            object, objectName);
067    
068                    synchronized (_objectNameCache) {
069                            _objectNameCache.put(
070                                    objectNameCacheKey, objectInstance.getObjectName());
071                    }
072    
073                    return objectInstance;
074            }
075    
076            public void replace(
077                            String objectCacheKey, Object object, ObjectName objectName)
078                    throws Exception {
079    
080                    try {
081                            register(objectCacheKey, object, objectName);
082                    }
083                    catch (InstanceAlreadyExistsException iaee) {
084                            unregister(objectCacheKey, objectName);
085    
086                            register(objectCacheKey, object, objectName);
087                    }
088            }
089    
090            public void setMBeanServer(MBeanServer mBeanServer) {
091                    _mBeanServer = mBeanServer;
092            }
093    
094            public void unregister(
095                            String objectNameCacheKey, ObjectName defaultObjectName)
096                    throws InstanceNotFoundException, MBeanRegistrationException {
097    
098                    synchronized (_objectNameCache) {
099                            ObjectName objectName = _objectNameCache.get(objectNameCacheKey);
100    
101                            if (objectName == null) {
102                                    try {
103                                            _mBeanServer.unregisterMBean(defaultObjectName);
104                                    }
105                                    catch (InstanceNotFoundException infe) {
106                                            if (_log.isDebugEnabled()) {
107                                                    _log.debug(
108                                                            "Unable to unregister " + defaultObjectName, infe);
109                                            }
110                                    }
111                            }
112                            else {
113                                    _objectNameCache.remove(objectNameCacheKey);
114    
115                                    _mBeanServer.unregisterMBean(objectName);
116                            }
117                    }
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(MBeanRegistry.class);
121    
122            private MBeanServer _mBeanServer;
123            private Map<String, ObjectName> _objectNameCache =
124                    new ConcurrentHashMap<String, ObjectName>();
125    
126    }