001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.messaging.jmx;
016    
017    import com.liferay.portal.kernel.jmx.MBeanRegistry;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.BaseDestinationEventListener;
021    import com.liferay.portal.kernel.messaging.Destination;
022    import com.liferay.portal.kernel.messaging.MessageBus;
023    
024    import java.util.Collection;
025    
026    /**
027     * @author Michael C. Han
028     * @author Brian Wing Shun Chan
029     */
030    public class JMXMessageListener extends BaseDestinationEventListener {
031    
032            public void afterPropertiesSet() throws Exception {
033                    if ((_mBeanRegistry == null) || (_messageBus == null)) {
034                            throw new IllegalStateException(
035                                    "MBean server and message bus are not configured");
036                    }
037    
038                    try {
039                            _mBeanRegistry.replace(
040                                    _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
041                                    new MessageBusManager(_messageBus),
042                                    MessageBusManager.createObjectName());
043                    }
044                    catch (Exception e) {
045                            if (log.isWarnEnabled()) {
046                                    log.warn("Unable to register message bus manager", e);
047                            }
048                    }
049    
050                    Collection<Destination> destinations = _messageBus.getDestinations();
051    
052                    for (Destination destination : destinations) {
053                            try {
054                                    registerDestination(destination);
055                            }
056                            catch (Exception e) {
057                                    if (log.isWarnEnabled()) {
058                                            log.warn(
059                                                    "Unable to register destination " +
060                                                            destination.getName(),
061                                            e);
062                                    }
063                            }
064                    }
065            }
066    
067            public void destinationAdded(Destination destination) {
068                    try {
069                            registerDestination(destination);
070                    }
071                    catch (Exception e) {
072                            log.error(
073                                    "Unable to register destination " + destination.getName(), e);
074                    }
075            }
076    
077            public void destinationRemoved(Destination destination) {
078                    try {
079                            unregisterDestination(destination);
080                    }
081                    catch (Exception e) {
082                            log.error(
083                                    "Unable to unregister destination " + destination.getName(), e);
084                    }
085            }
086    
087            public void destroy() throws Exception {
088                    Collection<Destination> destinations = _messageBus.getDestinations();
089    
090                    for (Destination destination : destinations) {
091                            try {
092                                    unregisterDestination(destination);
093                            }
094                            catch (Exception e) {
095                                    if (log.isWarnEnabled()) {
096                                            log.warn(
097                                                    "Unable to unregister destination " +
098                                                            destination.getName(),
099                                                    e);
100                                    }
101                            }
102                    }
103    
104                    try {
105                            _mBeanRegistry.unregister(
106                                    _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
107                                    MessageBusManager.createObjectName());
108                    }
109                    catch (Exception e) {
110                            if (log.isWarnEnabled()) {
111                                    log.warn("Unable to unregister message bus manager", e);
112                            }
113                    }
114            }
115    
116            /**
117             * @deprecated {@link #afterPropertiesSet}
118             */
119            public void init() throws Exception {
120                    afterPropertiesSet();
121            }
122    
123            public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
124                    _mBeanRegistry = mBeanRegistry;
125            }
126    
127            public void setMessageBus(MessageBus messageBus) {
128                    _messageBus = messageBus;
129            }
130    
131            protected void unregisterDestination(Destination destination)
132                    throws Exception {
133    
134                    String destinationName = destination.getName();
135    
136                    _mBeanRegistry.unregister(
137                            destinationName,
138                            DestinationManager.createObjectName(destinationName));
139    
140                    _mBeanRegistry.unregister(
141                            _getStatisticsObjectNameCacheKey(destinationName),
142                            DestinationStatisticsManager.createObjectName(destinationName));
143            }
144    
145            protected void registerDestination(Destination destination)
146                    throws Exception {
147    
148                    String destinationName = destination.getName();
149    
150                    _mBeanRegistry.replace(
151                            destinationName, new DestinationManager(destination),
152                            DestinationManager.createObjectName(destinationName));
153    
154                    _mBeanRegistry.replace(
155                            _getStatisticsObjectNameCacheKey(destinationName),
156                            new DestinationStatisticsManager(destination),
157                            DestinationStatisticsManager.createObjectName(destinationName));
158            }
159    
160            private String _getStatisticsObjectNameCacheKey(String destinationName) {
161                    return destinationName + "statistics";
162            }
163    
164            private static final String _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY =
165                    "messageBusManager";
166    
167            private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
168    
169            private MBeanRegistry _mBeanRegistry;
170            private MessageBus _messageBus;
171    
172    }