1
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.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
42 public abstract class AbstractMessagingConfigurator
43 implements MessagingConfigurator {
44
45 public void destroy() {
46 MessageBus messageBus = getMessageBus();
47
48 for (Map.Entry<String, List<MessageListener>> listeners :
49 _messageListeners.entrySet()) {
50
51 String destination = listeners.getKey();
52
53 for (MessageListener listener : listeners.getValue()) {
54 messageBus.unregisterMessageListener(destination, listener);
55 }
56 }
57
58 for (Destination destination : _destinations) {
59 messageBus.removeDestination(destination.getName());
60 }
61
62 for (DestinationEventListener listener : _destinationEventListeners) {
63 messageBus.removeDestinationEventListener(listener);
64 }
65 }
66
67 public void init() {
68 MessageBus messageBus = getMessageBus();
69
70 for (DestinationEventListener destinationEventListener :
71 _destinationEventListeners) {
72
73 messageBus.addDestinationEventListener(destinationEventListener);
74 }
75
76 for (Destination destination : _destinations) {
77 messageBus.addDestination(destination);
78 }
79
80 for (Destination destination : _replacementDestinations) {
81 messageBus.replace(destination);
82 }
83
84 ClassLoader contextClassLoader =
85 Thread.currentThread().getContextClassLoader();
86
87 try {
88 ClassLoader operatingClassLoader = getOperatingClassloader();
89
90 Thread.currentThread().setContextClassLoader(operatingClassLoader);
91
92 for (Map.Entry<String, List<MessageListener>> listeners :
93 _messageListeners.entrySet()) {
94
95 String destination = listeners.getKey();
96
97 for (MessageListener listener : listeners.getValue()) {
98 messageBus.registerMessageListener(destination, listener);
99 }
100 }
101 }
102 finally {
103 Thread.currentThread().setContextClassLoader(contextClassLoader);
104 }
105 }
106
107 public void setDestinationEventListeners(
108 List<DestinationEventListener> destinationEventListeners) {
109
110 _destinationEventListeners = destinationEventListeners;
111 }
112
113 public void setDestinations(List<Destination> destinations) {
114 _destinations = destinations;
115 }
116
117 public void setMessageListeners(
118 Map<String, List<MessageListener>> messageListeners) {
119
120 _messageListeners = messageListeners;
121 }
122
123 public void setReplacementDestinations(List<Destination> destinations) {
124 _replacementDestinations = destinations;
125 }
126
127 protected abstract MessageBus getMessageBus();
128 protected abstract ClassLoader getOperatingClassloader();
129
130 private List<DestinationEventListener> _destinationEventListeners =
131 new ArrayList<DestinationEventListener>();
132 private List<Destination> _destinations = new ArrayList<Destination>();
133 private Map<String, List<MessageListener>> _messageListeners =
134 new HashMap<String, List<MessageListener>>();
135 private List<Destination> _replacementDestinations =
136 new ArrayList<Destination>();
137
138 }