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.messaging.jmx;
16  
17  import com.liferay.portal.kernel.jmx.MBeanRegistry;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.messaging.BaseDestinationEventListener;
21  import com.liferay.portal.kernel.messaging.Destination;
22  import com.liferay.portal.kernel.messaging.MessageBus;
23  
24  import java.util.Collection;
25  
26  /**
27   * <a href="JMXMessageListener.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Michael C. Han
30   * @author Brian Wing Shun Chan
31   */
32  public class JMXMessageListener extends BaseDestinationEventListener {
33  
34      public void afterPropertiesSet() throws Exception {
35          if ((_mBeanRegistry == null) || (_messageBus == null)) {
36              throw new IllegalStateException(
37                  "MBean server and message bus are not configured");
38          }
39  
40          try {
41              _mBeanRegistry.replace(
42                  _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
43                  new MessageBusManager(_messageBus),
44                  MessageBusManager.createObjectName());
45          }
46          catch (Exception e) {
47              if (log.isWarnEnabled()) {
48                  log.warn("Unable to register message bus manager", e);
49              }
50          }
51  
52          Collection<Destination> destinations = _messageBus.getDestinations();
53  
54          for (Destination destination : destinations) {
55              try {
56                  registerDestination(destination);
57              }
58              catch (Exception e) {
59                  if (log.isWarnEnabled()) {
60                      log.warn(
61                          "Unable to register destination " +
62                              destination.getName(),
63                      e);
64                  }
65              }
66          }
67      }
68  
69      public void destinationAdded(Destination destination) {
70          try {
71              registerDestination(destination);
72          }
73          catch (Exception e) {
74              log.error(
75                  "Unable to register destination " + destination.getName(), e);
76          }
77      }
78  
79      public void destinationRemoved(Destination destination) {
80          try {
81              unregisterDestination(destination);
82          }
83          catch (Exception e) {
84              log.error(
85                  "Unable to unregister destination " + destination.getName(), e);
86          }
87      }
88  
89      public void destroy() throws Exception {
90          Collection<Destination> destinations = _messageBus.getDestinations();
91  
92          for (Destination destination : destinations) {
93              try {
94                  unregisterDestination(destination);
95              }
96              catch (Exception e) {
97                  if (log.isWarnEnabled()) {
98                      log.warn(
99                          "Unable to unregister destination " +
100                             destination.getName(),
101                         e);
102                 }
103             }
104         }
105 
106         try {
107             _mBeanRegistry.unregister(
108                 _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY,
109                 MessageBusManager.createObjectName());
110         }
111         catch (Exception e) {
112             if (log.isWarnEnabled()) {
113                 log.warn("Unable to unregister message bus manager", e);
114             }
115         }
116     }
117 
118     /**
119      * @deprecated {@link #afterPropertiesSet}
120      */
121     public void init() throws Exception {
122         afterPropertiesSet();
123     }
124 
125     public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
126         _mBeanRegistry = mBeanRegistry;
127     }
128 
129     public void setMessageBus(MessageBus messageBus) {
130         _messageBus = messageBus;
131     }
132 
133     protected void unregisterDestination(Destination destination)
134         throws Exception {
135 
136         String destinationName = destination.getName();
137 
138         _mBeanRegistry.unregister(
139             destinationName,
140             DestinationManager.createObjectName(destinationName));
141 
142         _mBeanRegistry.unregister(
143             _getStatisticsObjectNameCacheKey(destinationName),
144             DestinationStatisticsManager.createObjectName(destinationName));
145     }
146 
147     protected void registerDestination(Destination destination)
148         throws Exception {
149 
150         String destinationName = destination.getName();
151 
152         _mBeanRegistry.replace(
153             destinationName, new DestinationManager(destination),
154             DestinationManager.createObjectName(destinationName));
155 
156         _mBeanRegistry.replace(
157             _getStatisticsObjectNameCacheKey(destinationName),
158             new DestinationStatisticsManager(destination),
159             DestinationStatisticsManager.createObjectName(destinationName));
160     }
161 
162     private String _getStatisticsObjectNameCacheKey(String destinationName) {
163         return destinationName + "statistics";
164     }
165 
166     private static final String _MESSAGE_BUS_MANAGER_OBJECT_NAME_CACHE_KEY =
167         "messageBusManager";
168 
169     private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
170 
171     private MBeanRegistry _mBeanRegistry;
172     private MessageBus _messageBus;
173 
174 }