1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.messaging.jmx;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.BaseDestinationEventListener;
20  import com.liferay.portal.kernel.messaging.Destination;
21  import com.liferay.portal.kernel.messaging.MessageBus;
22  
23  import java.util.Collection;
24  
25  import javax.management.InstanceAlreadyExistsException;
26  import javax.management.MBeanServer;
27  import javax.management.ObjectName;
28  
29  /**
30   * <a href="JMXMessageListener.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Michael C. Han
33   * @author Brian Wing Shun Chan
34   */
35  public class JMXMessageListener extends BaseDestinationEventListener {
36  
37      public void afterPropertiesSet() throws Exception {
38          if ((_mBeanServer == null) || (_messageBus == null)) {
39              throw new IllegalStateException(
40                  "MBean server and message bus are not configured");
41          }
42  
43          try {
44              _replaceMBeanRegistration(
45                  new MessageBusManager(_messageBus),
46                  MessageBusManager.createObjectName());
47          }
48          catch (Exception e) {
49              if (log.isWarnEnabled()) {
50                  log.warn("Unable to register message bus manager", e);
51              }
52          }
53  
54          Collection<Destination> destinations = _messageBus.getDestinations();
55  
56          for (Destination destination : destinations) {
57              try {
58                  registerDestination(destination);
59              }
60              catch (Exception e) {
61                  if (log.isWarnEnabled()) {
62                      log.warn(
63                          "Unable to register destination " +
64                              destination.getName(),
65                      e);
66                  }
67              }
68          }
69      }
70  
71      public void destinationAdded(Destination destination) {
72          try {
73              registerDestination(destination);
74          }
75          catch (Exception e) {
76              log.error(
77                  "Unable to register destination " + destination.getName(), e);
78          }
79      }
80  
81      public void destinationRemoved(Destination destination) {
82          try {
83              unregisterDestination(destination);
84          }
85          catch (Exception e) {
86              log.error(
87                  "Unable to unregister destination " + destination.getName(), e);
88          }
89      }
90  
91      public void destroy() throws Exception {
92          Collection<Destination> destinations = _messageBus.getDestinations();
93  
94          for (Destination destination : destinations) {
95              try {
96                  unregisterDestination(destination);
97              }
98              catch (Exception e) {
99                  if (log.isWarnEnabled()) {
100                     log.warn(
101                         "Unable to unregister destination " +
102                             destination.getName(),
103                         e);
104                 }
105             }
106         }
107 
108         try {
109             _mBeanServer.unregisterMBean(
110                 MessageBusManager.createObjectName());
111         }
112         catch (Exception e) {
113             if (log.isWarnEnabled()) {
114                 log.warn("Unable to unregister message bus manager", e);
115             }
116         }
117     }
118 
119     /**
120      * @deprecated {@link #afterPropertiesSet}
121      */
122     public void init() throws Exception {
123         afterPropertiesSet();
124     }
125 
126     public void setMBeanServer(MBeanServer mBeanServer) {
127         _mBeanServer = mBeanServer;
128     }
129 
130     public void setMessageBus(MessageBus messageBus) {
131         _messageBus = messageBus;
132     }
133 
134     protected void registerDestination(Destination destination)
135         throws Exception {
136 
137         String destinationName = destination.getName();
138 
139         _replaceMBeanRegistration(
140             new DestinationManager(destination),
141             DestinationManager.createObjectName(destinationName));
142 
143         _replaceMBeanRegistration(
144             new DestinationStatisticsManager(destination),
145             DestinationStatisticsManager.createObjectName(destinationName));
146     }
147 
148     protected void unregisterDestination(Destination destination)
149         throws Exception {
150 
151         String destinationName = destination.getName();
152 
153         _mBeanServer.unregisterMBean(
154             DestinationManager.createObjectName(destinationName));
155 
156         _mBeanServer.unregisterMBean(
157             DestinationStatisticsManager.createObjectName(destinationName));
158     }
159 
160     private void _replaceMBeanRegistration(Object object, ObjectName objectName)
161         throws Exception {
162 
163         try {
164             _mBeanServer.registerMBean(object, objectName);
165         }
166         catch (InstanceAlreadyExistsException iaee) {
167             _mBeanServer.unregisterMBean(objectName);
168 
169             _mBeanServer.registerMBean(object, objectName);
170         }
171     }
172 
173     private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
174 
175     private MBeanServer _mBeanServer;
176     private MessageBus _messageBus;
177 
178 }