001
014
015 package com.liferay.portal.kernel.messaging.config;
016
017 import com.liferay.portal.kernel.messaging.Destination;
018 import com.liferay.portal.kernel.messaging.DestinationEventListener;
019 import com.liferay.portal.kernel.messaging.MessageBus;
020 import com.liferay.portal.kernel.messaging.MessageListener;
021
022 import java.lang.reflect.Method;
023
024 import java.util.ArrayList;
025 import java.util.HashMap;
026 import java.util.List;
027 import java.util.Map;
028
029
032 public abstract class AbstractMessagingConfigurator
033 implements MessagingConfigurator {
034
035 public void afterPropertiesSet() {
036 MessageBus messageBus = getMessageBus();
037
038 for (DestinationEventListener destinationEventListener :
039 _globalDestinationEventListeners) {
040
041 messageBus.addDestinationEventListener(destinationEventListener);
042 }
043
044 for (Destination destination : _destinations) {
045 messageBus.addDestination(destination);
046 }
047
048 for (Map.Entry<String, List<DestinationEventListener>>
049 destinationEventListeners :
050 _specificDestinationEventListeners.entrySet()) {
051
052 String destinationName = destinationEventListeners.getKey();
053
054 for (DestinationEventListener destinationEventListener :
055 destinationEventListeners.getValue()) {
056
057 messageBus.addDestinationEventListener(
058 destinationName, destinationEventListener);
059 }
060 }
061
062 for (Destination destination : _replacementDestinations) {
063 messageBus.replace(destination);
064 }
065
066 Thread currentThread = Thread.currentThread();
067
068 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
069
070 try {
071 ClassLoader operatingClassLoader = getOperatingClassloader();
072
073 currentThread.setContextClassLoader(operatingClassLoader);
074
075 for (Map.Entry<String, List<MessageListener>> messageListeners :
076 _messageListeners.entrySet()) {
077
078 String destinationName = messageListeners.getKey();
079
080 for (MessageListener messageListener :
081 messageListeners.getValue()) {
082
083 messageBus.registerMessageListener(
084 destinationName, messageListener);
085 }
086 }
087 }
088 finally {
089 currentThread.setContextClassLoader(contextClassLoader);
090 }
091 }
092
093 public void destroy() {
094 MessageBus messageBus = getMessageBus();
095
096 for (Map.Entry<String, List<MessageListener>> messageListeners :
097 _messageListeners.entrySet()) {
098
099 String destinationName = messageListeners.getKey();
100
101 for (MessageListener messageListener :
102 messageListeners.getValue()) {
103
104 messageBus.unregisterMessageListener(
105 destinationName, messageListener);
106 }
107 }
108
109 for (Destination destination : _destinations) {
110 messageBus.removeDestination(destination.getName());
111
112 destination.close();
113 }
114
115 for (DestinationEventListener destinationEventListener :
116 _globalDestinationEventListeners) {
117
118 messageBus.removeDestinationEventListener(destinationEventListener);
119 }
120 }
121
122
125 public void init() {
126 afterPropertiesSet();
127 }
128
129 public void setDestinations(List<Destination> destinations) {
130 _destinations = destinations;
131 }
132
133 public void setGlobalDestinationEventListeners(
134 List<DestinationEventListener> globalDestinationEventListeners) {
135
136 _globalDestinationEventListeners = globalDestinationEventListeners;
137 }
138
139 public void setMessageListeners(
140 Map<String, List<MessageListener>> messageListeners) {
141
142 _messageListeners = messageListeners;
143
144 for (List<MessageListener> messageListenersList :
145 _messageListeners.values()) {
146
147 for (MessageListener messageListener : messageListenersList) {
148 Class<?> messageListenerClass = messageListener.getClass();
149
150 try {
151 Method setMessageBusMethod = messageListenerClass.getMethod(
152 "setMessageBus", MessageBus.class);
153
154 setMessageBusMethod.setAccessible(true);
155
156 setMessageBusMethod.invoke(
157 messageListener, getMessageBus());
158
159 continue;
160 }
161 catch (Exception e) {
162 }
163
164 try{
165 Method setMessageBusMethod =
166 messageListenerClass.getDeclaredMethod(
167 "setMessageBus", MessageBus.class);
168
169 setMessageBusMethod.setAccessible(true);
170
171 setMessageBusMethod.invoke(
172 messageListener, getMessageBus());
173 }
174 catch (Exception e) {
175 }
176 }
177 }
178 }
179
180 public void setReplacementDestinations(
181 List<Destination> replacementDestinations) {
182
183 _replacementDestinations = replacementDestinations;
184 }
185
186 public void setSpecificDestinationEventListener(
187 Map<String, List<DestinationEventListener>>
188 specificDestinationEventListeners) {
189
190 _specificDestinationEventListeners = specificDestinationEventListeners;
191 }
192
193 protected abstract MessageBus getMessageBus();
194
195 protected abstract ClassLoader getOperatingClassloader();
196
197 private List<Destination> _destinations = new ArrayList<Destination>();
198 private List<DestinationEventListener> _globalDestinationEventListeners =
199 new ArrayList<DestinationEventListener>();
200 private Map<String, List<MessageListener>> _messageListeners =
201 new HashMap<String, List<MessageListener>>();
202 private List<Destination> _replacementDestinations =
203 new ArrayList<Destination>();
204 private Map<String, List<DestinationEventListener>>
205 _specificDestinationEventListeners =
206 new HashMap<String, List<DestinationEventListener>>();
207
208 }