001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.messaging.config;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Destination;
020    import com.liferay.portal.kernel.messaging.DestinationEventListener;
021    import com.liferay.portal.kernel.messaging.MessageBus;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.kernel.security.pacl.permission.PortalMessageBusPermission;
024    
025    import java.lang.reflect.Method;
026    
027    import java.util.ArrayList;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Michael C. Han
034     */
035    public abstract class AbstractMessagingConfigurator
036            implements MessagingConfigurator {
037    
038            public void afterPropertiesSet() {
039                    MessageBus messageBus = getMessageBus();
040    
041                    for (DestinationEventListener destinationEventListener :
042                                    _globalDestinationEventListeners) {
043    
044                            messageBus.addDestinationEventListener(destinationEventListener);
045                    }
046    
047                    for (Destination destination : _destinations) {
048                            messageBus.addDestination(destination);
049                    }
050    
051                    for (Map.Entry<String, List<DestinationEventListener>>
052                                    destinationEventListeners :
053                                            _specificDestinationEventListeners.entrySet()) {
054    
055                            String destinationName = destinationEventListeners.getKey();
056    
057                            for (DestinationEventListener destinationEventListener :
058                                            destinationEventListeners.getValue()) {
059    
060                                    messageBus.addDestinationEventListener(
061                                            destinationName, destinationEventListener);
062                            }
063                    }
064    
065                    for (Destination destination : _replacementDestinations) {
066                            messageBus.replace(destination);
067                    }
068    
069                    Thread currentThread = Thread.currentThread();
070    
071                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
072    
073                    try {
074                            ClassLoader operatingClassLoader = getOperatingClassloader();
075    
076                            currentThread.setContextClassLoader(operatingClassLoader);
077    
078                            for (Map.Entry<String, List<MessageListener>> messageListeners :
079                                            _messageListeners.entrySet()) {
080    
081                                    String destinationName = messageListeners.getKey();
082    
083                                    for (MessageListener messageListener :
084                                                    messageListeners.getValue()) {
085    
086                                            messageBus.registerMessageListener(
087                                                    destinationName, messageListener);
088                                    }
089                            }
090                    }
091                    finally {
092                            currentThread.setContextClassLoader(contextClassLoader);
093                    }
094            }
095    
096            public void destroy() {
097                    MessageBus messageBus = getMessageBus();
098    
099                    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 (Map.Entry<String, List<DestinationEventListener>>
119                                    destinationEventListeners :
120                                            _specificDestinationEventListeners.entrySet()) {
121    
122                            String destinationName = destinationEventListeners.getKey();
123    
124                            for (DestinationEventListener destinationEventListener :
125                                            destinationEventListeners.getValue()) {
126    
127                                    messageBus.removeDestinationEventListener(
128                                            destinationName, destinationEventListener);
129                            }
130                    }
131    
132                    for (DestinationEventListener destinationEventListener :
133                                    _globalDestinationEventListeners) {
134    
135                            messageBus.removeDestinationEventListener(destinationEventListener);
136                    }
137            }
138    
139            /**
140             * @deprecated As of 6.1.0, replaced by {@link #afterPropertiesSet}
141             */
142            public void init() {
143                    afterPropertiesSet();
144            }
145    
146            public void setDestinations(List<Destination> destinations) {
147                    for (Destination destination : destinations) {
148                            try {
149                                    PortalMessageBusPermission.checkListen(destination.getName());
150                            }
151                            catch (SecurityException se) {
152                                    if (_log.isInfoEnabled()) {
153                                            _log.info("Rejecting destination " + destination.getName());
154                                    }
155    
156                                    continue;
157                            }
158    
159                            _destinations.add(destination);
160                    }
161            }
162    
163            public void setGlobalDestinationEventListeners(
164                    List<DestinationEventListener> globalDestinationEventListeners) {
165    
166                    _globalDestinationEventListeners = globalDestinationEventListeners;
167            }
168    
169            public void setMessageListeners(
170                    Map<String, List<MessageListener>> messageListeners) {
171    
172                    _messageListeners = messageListeners;
173    
174                    for (List<MessageListener> messageListenersList :
175                                    _messageListeners.values()) {
176    
177                            for (MessageListener messageListener : messageListenersList) {
178                                    Class<?> messageListenerClass = messageListener.getClass();
179    
180                                    try {
181                                            Method setMessageBusMethod = messageListenerClass.getMethod(
182                                                    "setMessageBus", MessageBus.class);
183    
184                                            setMessageBusMethod.setAccessible(true);
185    
186                                            setMessageBusMethod.invoke(
187                                                    messageListener, getMessageBus());
188    
189                                            continue;
190                                    }
191                                    catch (Exception e) {
192                                    }
193    
194                                    try {
195                                            Method setMessageBusMethod =
196                                                    messageListenerClass.getDeclaredMethod(
197                                                            "setMessageBus", MessageBus.class);
198    
199                                            setMessageBusMethod.setAccessible(true);
200    
201                                            setMessageBusMethod.invoke(
202                                                    messageListener, getMessageBus());
203                                    }
204                                    catch (Exception e) {
205                                    }
206                            }
207                    }
208            }
209    
210            public void setReplacementDestinations(
211                    List<Destination> replacementDestinations) {
212    
213                    _replacementDestinations = replacementDestinations;
214            }
215    
216            public void setSpecificDestinationEventListener(
217                    Map<String, List<DestinationEventListener>>
218                            specificDestinationEventListeners) {
219    
220                    _specificDestinationEventListeners = specificDestinationEventListeners;
221            }
222    
223            protected abstract MessageBus getMessageBus();
224    
225            protected abstract ClassLoader getOperatingClassloader();
226    
227            private static Log _log = LogFactoryUtil.getLog(
228                    AbstractMessagingConfigurator.class);
229    
230            private List<Destination> _destinations = new ArrayList<Destination>();
231            private List<DestinationEventListener> _globalDestinationEventListeners =
232                    new ArrayList<DestinationEventListener>();
233            private Map<String, List<MessageListener>> _messageListeners =
234                    new HashMap<String, List<MessageListener>>();
235            private List<Destination> _replacementDestinations =
236                    new ArrayList<Destination>();
237            private Map<String, List<DestinationEventListener>>
238                    _specificDestinationEventListeners =
239                            new HashMap<String, List<DestinationEventListener>>();
240    
241    }