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