001    /**
002     * Copyright (c) 2000-2010 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.HashMap;
021    import java.util.Map;
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                    for (ObjectName objectName : _objectNameCache.values()) {
038                            try {
039                                    _mBeanServer.unregisterMBean(objectName);
040                            }
041                            catch (Exception e) {
042                                    if (_log.isWarnEnabled()) {
043                                            _log.warn(
044                                                    "Unable to unregister MBean" +
045                                                            objectName.getCanonicalName(),
046                                                    e);
047                                    }
048                            }
049                    }
050    
051                    _objectNameCache.clear();
052            }
053    
054            public ObjectName getObjectName(String objectNameCacheKey) {
055                    return _objectNameCache.get(objectNameCacheKey);
056            }
057    
058            public ObjectInstance register(
059                            String objectNameCacheKey, Object object, ObjectName objectName)
060                    throws InstanceAlreadyExistsException, MBeanRegistrationException,
061                               NotCompliantMBeanException {
062    
063                    ObjectInstance objectInstance = _mBeanServer.registerMBean(
064                            object, objectName);
065    
066                    _objectNameCache.put(
067                            objectNameCacheKey, objectInstance.getObjectName());
068    
069                    return objectInstance;
070            }
071    
072            public void replace(
073                            String objectCacheKey, Object object, ObjectName objectName)
074                    throws Exception {
075    
076                    try {
077                            register(objectCacheKey, object, objectName);
078                    }
079                    catch (InstanceAlreadyExistsException iaee) {
080                            unregister(objectCacheKey, objectName);
081    
082                            register(objectCacheKey, object, objectName);
083                    }
084            }
085    
086            public void setMBeanServer(MBeanServer mBeanServer) {
087                    _mBeanServer = mBeanServer;
088            }
089    
090            public void unregister(
091                            String objectNameCacheKey, ObjectName defaultObjectName)
092                    throws InstanceNotFoundException, MBeanRegistrationException {
093    
094                    ObjectName objectName = _objectNameCache.get(objectNameCacheKey);
095    
096                    if (objectName == null) {
097                            _mBeanServer.unregisterMBean(defaultObjectName);
098                    }
099                    else {
100                            _objectNameCache.remove(objectNameCacheKey);
101    
102                            _mBeanServer.unregisterMBean(objectName);
103                    }
104            }
105    
106            private static final Log _log = LogFactoryUtil.getLog(MBeanRegistry.class);
107    
108            private MBeanServer _mBeanServer;
109            private Map<String, ObjectName> _objectNameCache =
110                    new HashMap<String, ObjectName>();
111    
112    }