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.config;
16  
17  import com.liferay.portal.kernel.messaging.Destination;
18  import com.liferay.portal.kernel.messaging.DestinationEventListener;
19  import com.liferay.portal.kernel.messaging.MessageBus;
20  import com.liferay.portal.kernel.messaging.MessageListener;
21  
22  import java.lang.reflect.Method;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * <a href="AbstractMessagingConfigurator.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Michael C. Han
34   */
35  public abstract class AbstractMessagingConfigurator
36      implements MessagingConfigurator {
37  
38      public void afterPropertiesSet() {
39          MessageBus messageBus = getMessageBus();
40  
41          for (DestinationEventListener destinationEventListener :
42                  _globalDestinationEventListeners) {
43  
44              messageBus.addDestinationEventListener(destinationEventListener);
45          }
46  
47          for (Destination destination : _destinations) {
48              messageBus.addDestination(destination);
49          }
50  
51          for (Map.Entry<String, List<DestinationEventListener>>
52                  destinationEventListeners :
53                      _specificDestinationEventListeners.entrySet()) {
54  
55              String destinationName = destinationEventListeners.getKey();
56  
57              for (DestinationEventListener destinationEventListener :
58                      destinationEventListeners.getValue()) {
59  
60                  messageBus.addDestinationEventListener(
61                      destinationName, destinationEventListener);
62              }
63          }
64  
65          for (Destination destination : _replacementDestinations) {
66              messageBus.replace(destination);
67          }
68  
69          ClassLoader contextClassLoader =
70              Thread.currentThread().getContextClassLoader();
71  
72          try {
73              ClassLoader operatingClassLoader = getOperatingClassloader();
74  
75              Thread.currentThread().setContextClassLoader(operatingClassLoader);
76  
77              for (Map.Entry<String, List<MessageListener>> messageListeners :
78                      _messageListeners.entrySet()) {
79  
80                  String destinationName = messageListeners.getKey();
81  
82                  for (MessageListener messageListener :
83                          messageListeners.getValue()) {
84  
85                      messageBus.registerMessageListener(
86                          destinationName, messageListener);
87                  }
88              }
89          }
90          finally {
91              Thread.currentThread().setContextClassLoader(contextClassLoader);
92          }
93      }
94  
95      public void destroy() {
96          MessageBus messageBus = getMessageBus();
97  
98          for (Map.Entry<String, List<MessageListener>> messageListeners :
99                  _messageListeners.entrySet()) {
100 
101             String destinationName = messageListeners.getKey();
102 
103             for (MessageListener messageListener :
104                     messageListeners.getValue()) {
105 
106                 messageBus.unregisterMessageListener(
107                     destinationName, messageListener);
108             }
109         }
110 
111         for (Destination destination : _destinations) {
112             messageBus.removeDestination(destination.getName());
113 
114             destination.close();
115         }
116 
117         for (DestinationEventListener destinationEventListener :
118                 _globalDestinationEventListeners) {
119 
120             messageBus.removeDestinationEventListener(destinationEventListener);
121         }
122     }
123 
124     /**
125      * @deprecated {@link #afterPropertiesSet}
126      */
127     public void init() {
128         afterPropertiesSet();
129     }
130 
131     public void setDestinations(List<Destination> destinations) {
132         _destinations = destinations;
133     }
134 
135     public void setGlobalDestinationEventListeners(
136         List<DestinationEventListener> globalDestinationEventListeners) {
137 
138         _globalDestinationEventListeners = globalDestinationEventListeners;
139     }
140 
141     public void setMessageListeners(
142         Map<String, List<MessageListener>> messageListeners) {
143 
144         _messageListeners = messageListeners;
145 
146         for (List<MessageListener> messageListenersList :
147                 _messageListeners.values()) {
148 
149             for (MessageListener messageListener : messageListenersList) {
150                 Class<?> messageListenerClass = messageListener.getClass();
151 
152                 try {
153                     Method setMessageBusMethod =
154                         messageListenerClass.getMethod(
155                             "setMessageBus", MessageBus.class);
156 
157                     setMessageBusMethod.setAccessible(true);
158 
159                     setMessageBusMethod.invoke(
160                         messageListener, getMessageBus());
161 
162                     continue;
163                 }
164                 catch (Exception e) {
165                 }
166 
167                 try{
168                     Method setMessageBusMethod =
169                         messageListenerClass.getDeclaredMethod(
170                             "setMessageBus", MessageBus.class);
171 
172                     setMessageBusMethod.setAccessible(true);
173 
174                     setMessageBusMethod.invoke(
175                         messageListener, getMessageBus());
176                 }
177                 catch (Exception e) {
178                 }
179             }
180         }
181     }
182 
183     public void setReplacementDestinations(
184         List<Destination> replacementDestinations) {
185 
186         _replacementDestinations = replacementDestinations;
187     }
188 
189     public void setSpecificDestinationEventListener(
190         Map<String, List<DestinationEventListener>>
191             specificDestinationEventListeners) {
192 
193         _specificDestinationEventListeners = specificDestinationEventListeners;
194     }
195 
196     protected abstract MessageBus getMessageBus();
197 
198     protected abstract ClassLoader getOperatingClassloader();
199 
200     private List<Destination> _destinations = new ArrayList<Destination>();
201     private List<DestinationEventListener> _globalDestinationEventListeners =
202         new ArrayList<DestinationEventListener>();
203     private Map<String, List<MessageListener>> _messageListeners  =
204         new HashMap<String, List<MessageListener>>();
205     private List<Destination> _replacementDestinations =
206         new ArrayList<Destination>();
207     private Map<String, List<DestinationEventListener>>
208         _specificDestinationEventListeners =
209             new HashMap<String, List<DestinationEventListener>>();
210 
211 }