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.InstanceAlreadyExistsException;
34  import javax.management.MBeanServer;
35  import javax.management.ObjectName;
36  
37  /**
38   * <a href="JMXMessageListener.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Michael C. Han
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class JMXMessageListener implements DestinationEventListener {
45  
46      public void destinationAdded(Destination destination) {
47          try {
48              registerDestination(destination);
49          }
50          catch (Exception e) {
51              log.error(
52                  "Unable to register destination " + destination.getName(), e);
53          }
54      }
55  
56      public void destinationRemoved(Destination destination) {
57          try {
58              unregisterDestination(destination);
59          }
60          catch (Exception e) {
61              log.error(
62                  "Unable to unregister destination " + destination.getName(), e);
63          }
64      }
65  
66      public void destroy() throws Exception {
67          Collection<Destination> destinations = _messageBus.getDestinations();
68  
69          for (Destination destination : destinations) {
70              try {
71                  unregisterDestination(destination);
72              }
73              catch (Exception e) {
74                  if (log.isWarnEnabled()) {
75                      log.warn(
76                          "Unable to unregister destination " +
77                              destination.getName(),
78                          e);
79                  }
80              }
81          }
82  
83          try {
84              _mBeanServer.unregisterMBean(
85                  MessageBusManager.createObjectName());
86          }
87          catch (Exception e) {
88              if (log.isWarnEnabled()) {
89                  log.warn("Unable to unregister message bus manager", e);
90              }
91          }
92      }
93  
94      public void init() throws Exception {
95          if ((_mBeanServer == null) || (_messageBus == null)) {
96              throw new IllegalStateException(
97                  "MBean server and message bus are not configured");
98          }
99  
100         try {
101             _replaceMBeanRegistration(
102                 new MessageBusManager(_messageBus),
103                 MessageBusManager.createObjectName());
104         }
105         catch (Exception e) {
106             if (log.isWarnEnabled()) {
107                 log.warn("Unable to register message bus manager", e);
108             }
109         }
110 
111         Collection<Destination> destinations = _messageBus.getDestinations();
112 
113         for (Destination destination : destinations) {
114             try {
115                 registerDestination(destination);
116             }
117             catch (Exception e) {
118                 if (log.isWarnEnabled()) {
119                     log.warn(
120                         "Unable to register destination " +
121                             destination.getName(),
122                     e);
123                 }
124             }
125         }
126     }
127 
128     public void setMBeanServer(MBeanServer mBeanServer) {
129         _mBeanServer = mBeanServer;
130     }
131 
132     public void setMessageBus(MessageBus messageBus) {
133         _messageBus = messageBus;
134     }
135 
136     protected void registerDestination(Destination destination)
137         throws Exception {
138 
139         String destinationName = destination.getName();
140 
141         _replaceMBeanRegistration(
142             new DestinationManager(destination),
143             DestinationManager.createObjectName(destinationName));
144 
145         _replaceMBeanRegistration(
146             new DestinationStatisticsManager(destination),
147             DestinationStatisticsManager.createObjectName(destinationName));
148     }
149 
150     protected void unregisterDestination(Destination destination)
151         throws Exception {
152 
153         String destinationName = destination.getName();
154 
155         _mBeanServer.unregisterMBean(
156             DestinationManager.createObjectName(destinationName));
157 
158         _mBeanServer.unregisterMBean(
159             DestinationStatisticsManager.createObjectName(destinationName));
160     }
161 
162     private void _replaceMBeanRegistration(Object object, ObjectName objectName)
163         throws Exception {
164 
165         try {
166             _mBeanServer.registerMBean(object, objectName);
167         }
168         catch (InstanceAlreadyExistsException iaee) {
169             _mBeanServer.unregisterMBean(objectName);
170 
171             _mBeanServer.registerMBean(object, objectName);
172         }
173     }
174 
175     private static Log log = LogFactoryUtil.getLog(JMXMessageListener.class);
176 
177     private MBeanServer _mBeanServer;
178     private MessageBus _messageBus;
179 
180 }