1
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
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 Thread currentThread = Thread.currentThread();
70
71 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
72
73 try {
74 ClassLoader operatingClassLoader = getOperatingClassloader();
75
76 currentThread.setContextClassLoader(operatingClassLoader);
77
78 for (Map.Entry<String, List<MessageListener>> messageListeners :
79 _messageListeners.entrySet()) {
80
81 String destinationName = messageListeners.getKey();
82
83 for (MessageListener messageListener :
84 messageListeners.getValue()) {
85
86 messageBus.registerMessageListener(
87 destinationName, messageListener);
88 }
89 }
90 }
91 finally {
92 currentThread.setContextClassLoader(contextClassLoader);
93 }
94 }
95
96 public void destroy() {
97 MessageBus messageBus = getMessageBus();
98
99 for (Map.Entry<String, List<MessageListener>> messageListeners :
100 _messageListeners.entrySet()) {
101
102 String destinationName = messageListeners.getKey();
103
104 for (MessageListener messageListener :
105 messageListeners.getValue()) {
106
107 messageBus.unregisterMessageListener(
108 destinationName, messageListener);
109 }
110 }
111
112 for (Destination destination : _destinations) {
113 messageBus.removeDestination(destination.getName());
114
115 destination.close();
116 }
117
118 for (DestinationEventListener destinationEventListener :
119 _globalDestinationEventListeners) {
120
121 messageBus.removeDestinationEventListener(destinationEventListener);
122 }
123 }
124
125
128 public void init() {
129 afterPropertiesSet();
130 }
131
132 public void setDestinations(List<Destination> destinations) {
133 _destinations = destinations;
134 }
135
136 public void setGlobalDestinationEventListeners(
137 List<DestinationEventListener> globalDestinationEventListeners) {
138
139 _globalDestinationEventListeners = globalDestinationEventListeners;
140 }
141
142 public void setMessageListeners(
143 Map<String, List<MessageListener>> messageListeners) {
144
145 _messageListeners = messageListeners;
146
147 for (List<MessageListener> messageListenersList :
148 _messageListeners.values()) {
149
150 for (MessageListener messageListener : messageListenersList) {
151 Class<?> messageListenerClass = messageListener.getClass();
152
153 try {
154 Method setMessageBusMethod =
155 messageListenerClass.getMethod(
156 "setMessageBus", MessageBus.class);
157
158 setMessageBusMethod.setAccessible(true);
159
160 setMessageBusMethod.invoke(
161 messageListener, getMessageBus());
162
163 continue;
164 }
165 catch (Exception e) {
166 }
167
168 try{
169 Method setMessageBusMethod =
170 messageListenerClass.getDeclaredMethod(
171 "setMessageBus", MessageBus.class);
172
173 setMessageBusMethod.setAccessible(true);
174
175 setMessageBusMethod.invoke(
176 messageListener, getMessageBus());
177 }
178 catch (Exception e) {
179 }
180 }
181 }
182 }
183
184 public void setReplacementDestinations(
185 List<Destination> replacementDestinations) {
186
187 _replacementDestinations = replacementDestinations;
188 }
189
190 public void setSpecificDestinationEventListener(
191 Map<String, List<DestinationEventListener>>
192 specificDestinationEventListeners) {
193
194 _specificDestinationEventListeners = specificDestinationEventListeners;
195 }
196
197 protected abstract MessageBus getMessageBus();
198
199 protected abstract ClassLoader getOperatingClassloader();
200
201 private List<Destination> _destinations = new ArrayList<Destination>();
202 private List<DestinationEventListener> _globalDestinationEventListeners =
203 new ArrayList<DestinationEventListener>();
204 private Map<String, List<MessageListener>> _messageListeners =
205 new HashMap<String, List<MessageListener>>();
206 private List<Destination> _replacementDestinations =
207 new ArrayList<Destination>();
208 private Map<String, List<DestinationEventListener>>
209 _specificDestinationEventListeners =
210 new HashMap<String, List<DestinationEventListener>>();
211
212 }