1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.messaging.jmx;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.messaging.Destination;
28  import com.liferay.portal.kernel.messaging.DestinationEventListener;
29  import com.liferay.portal.kernel.messaging.MessageBus;
30  
31  import java.util.Collection;
32  
33  import javax.management.MBeanServer;
34  
35  /**
36   * <a href="JMXMessageListener.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Michael C. Han
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class JMXMessageListener implements DestinationEventListener {
43  
44      public void destinationAdded(Destination destination) {
45          try {
46              registerDestination(destination);
47          }
48          catch (Exception e) {
49              log.error(
50                  "Unable to register destination " + destination.getName(), e);
51          }
52      }
53  
54      public void destinationRemoved(Destination destination) {
55          try {
56              unregisterDestination(destination);
57          }
58          catch (Exception e) {
59              log.error(
60                  "Unable to unregister destination " + destination.getName(), e);
61          }
62      }
63  
64      public void destroy() throws Exception {
65          _mBeanServer.unregisterMBean(
66              MessageBusManager.createObjectName());
67      }
68  
69      public void init() throws Exception {
70          if ((_mBeanServer == null) || (_messageBus == null)) {
71              throw new IllegalStateException(
72                  "MBean server and message bus are not configured");
73          }
74  
75          _mBeanServer.registerMBean(
76              new MessageBusManager(_messageBus),
77              MessageBusManager.createObjectName());
78  
79          Collection<Destination> destinations = _messageBus.getDestinations();
80  
81          for (Destination destination : destinations) {
82              registerDestination(destination);
83          }
84      }
85  
86      public void setMBeanServer(MBeanServer mBeanServer) {
87          _mBeanServer = mBeanServer;
88      }
89  
90      public void setMessageBus(MessageBus messageBus) {
91          _messageBus = messageBus;
92      }
93  
94      protected void registerDestination(Destination destination)
95          throws Exception {
96  
97          String destinationName = destination.getName();
98  
99          _mBeanServer.registerMBean(
100             new DestinationManager(destination),
101             DestinationManager.createObjectName(destinationName));
102 
103         _mBeanServer.registerMBean(
104             new DestinationStatisticsManager(destination),
105             DestinationStatisticsManager.createObjectName(destinationName));
106     }
107 
108     protected void unregisterDestination(Destination destination)
109         throws Exception {
110 
111         String destinationName = destination.getName();
112 
113         _mBeanServer.unregisterMBean(
114             DestinationManager.createObjectName(destinationName));
115 
116         _mBeanServer.unregisterMBean(
117             DestinationStatisticsManager.createObjectName(destinationName));
118     }
119 
120     private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
121 
122     private MBeanServer _mBeanServer;
123     private MessageBus _messageBus;
124 
125 }