001    /**
002     * Copyright (c) 2000-2012 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.PACLConstants;
024    import com.liferay.portal.kernel.security.pacl.permission.PortalMessageBusPermission;
025    
026    import java.lang.reflect.Method;
027    
028    import java.security.Permission;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Michael C. Han
037     */
038    public abstract class AbstractMessagingConfigurator
039            implements MessagingConfigurator {
040    
041            public void afterPropertiesSet() {
042                    MessageBus messageBus = getMessageBus();
043    
044                    for (DestinationEventListener destinationEventListener :
045                                    _globalDestinationEventListeners) {
046    
047                            messageBus.addDestinationEventListener(destinationEventListener);
048                    }
049    
050                    for (Destination destination : _destinations) {
051                            messageBus.addDestination(destination);
052                    }
053    
054                    for (Map.Entry<String, List<DestinationEventListener>>
055                                    destinationEventListeners :
056                                            _specificDestinationEventListeners.entrySet()) {
057    
058                            String destinationName = destinationEventListeners.getKey();
059    
060                            for (DestinationEventListener destinationEventListener :
061                                            destinationEventListeners.getValue()) {
062    
063                                    messageBus.addDestinationEventListener(
064                                            destinationName, destinationEventListener);
065                            }
066                    }
067    
068                    for (Destination destination : _replacementDestinations) {
069                            messageBus.replace(destination);
070                    }
071    
072                    Thread currentThread = Thread.currentThread();
073    
074                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
075    
076                    try {
077                            ClassLoader operatingClassLoader = getOperatingClassloader();
078    
079                            currentThread.setContextClassLoader(operatingClassLoader);
080    
081                            for (Map.Entry<String, List<MessageListener>> messageListeners :
082                                            _messageListeners.entrySet()) {
083    
084                                    String destinationName = messageListeners.getKey();
085    
086                                    for (MessageListener messageListener :
087                                                    messageListeners.getValue()) {
088    
089                                            messageBus.registerMessageListener(
090                                                    destinationName, messageListener);
091                                    }
092                            }
093                    }
094                    finally {
095                            currentThread.setContextClassLoader(contextClassLoader);
096                    }
097            }
098    
099            public void destroy() {
100                    MessageBus messageBus = getMessageBus();
101    
102                    for (Map.Entry<String, List<MessageListener>> messageListeners :
103                                    _messageListeners.entrySet()) {
104    
105                            String destinationName = messageListeners.getKey();
106    
107                            for (MessageListener messageListener :
108                                            messageListeners.getValue()) {
109    
110                                    messageBus.unregisterMessageListener(
111                                            destinationName, messageListener);
112                            }
113                    }
114    
115                    for (Destination destination : _destinations) {
116                            messageBus.removeDestination(destination.getName());
117    
118                            destination.close();
119                    }
120    
121                    for (Map.Entry<String, List<DestinationEventListener>>
122                                    destinationEventListeners :
123                                            _specificDestinationEventListeners.entrySet()) {
124    
125                            String destinationName = destinationEventListeners.getKey();
126    
127                            for (DestinationEventListener destinationEventListener :
128                                            destinationEventListeners.getValue()) {
129    
130                                    messageBus.removeDestinationEventListener(
131                                            destinationName, destinationEventListener);
132                            }
133                    }
134    
135                    for (DestinationEventListener destinationEventListener :
136                                    _globalDestinationEventListeners) {
137    
138                            messageBus.removeDestinationEventListener(destinationEventListener);
139                    }
140            }
141    
142            /**
143             * @deprecated {@link #afterPropertiesSet}
144             */
145            public void init() {
146                    afterPropertiesSet();
147            }
148    
149            public void setDestinations(List<Destination> destinations) {
150                    for (Destination destination : destinations) {
151                            SecurityManager securityManager = System.getSecurityManager();
152    
153                            if (securityManager != null) {
154                                    Permission permission = new PortalMessageBusPermission(
155                                            PACLConstants.PORTAL_MESSAGE_BUS_PERMISSION_LISTEN,
156                                            destination.getName());
157    
158                                    try {
159                                            securityManager.checkPermission(permission);
160                                    }
161                                    catch (SecurityException se) {
162                                            if (_log.isInfoEnabled()) {
163                                                    _log.info(
164                                                            "Rejecting destination " + destination.getName());
165                                            }
166    
167                                            continue;
168                                    }
169                            }
170    
171                            _destinations.add(destination);
172                    }
173            }
174    
175            public void setGlobalDestinationEventListeners(
176                    List<DestinationEventListener> globalDestinationEventListeners) {
177    
178                    _globalDestinationEventListeners = globalDestinationEventListeners;
179            }
180    
181            public void setMessageListeners(
182                    Map<String, List<MessageListener>> messageListeners) {
183    
184                    _messageListeners = messageListeners;
185    
186                    for (List<MessageListener> messageListenersList :
187                                    _messageListeners.values()) {
188    
189                            for (MessageListener messageListener : messageListenersList) {
190                                    Class<?> messageListenerClass = messageListener.getClass();
191    
192                                    try {
193                                            Method setMessageBusMethod = messageListenerClass.getMethod(
194                                                    "setMessageBus", MessageBus.class);
195    
196                                            setMessageBusMethod.setAccessible(true);
197    
198                                            setMessageBusMethod.invoke(
199                                                    messageListener, getMessageBus());
200    
201                                            continue;
202                                    }
203                                    catch (Exception e) {
204                                    }
205    
206                                    try {
207                                            Method setMessageBusMethod =
208                                                    messageListenerClass.getDeclaredMethod(
209                                                            "setMessageBus", MessageBus.class);
210    
211                                            setMessageBusMethod.setAccessible(true);
212    
213                                            setMessageBusMethod.invoke(
214                                                    messageListener, getMessageBus());
215                                    }
216                                    catch (Exception e) {
217                                    }
218                            }
219                    }
220            }
221    
222            public void setReplacementDestinations(
223                    List<Destination> replacementDestinations) {
224    
225                    _replacementDestinations = replacementDestinations;
226            }
227    
228            public void setSpecificDestinationEventListener(
229                    Map<String, List<DestinationEventListener>>
230                            specificDestinationEventListeners) {
231    
232                    _specificDestinationEventListeners = specificDestinationEventListeners;
233            }
234    
235            protected abstract MessageBus getMessageBus();
236    
237            protected abstract ClassLoader getOperatingClassloader();
238    
239            private static Log _log = LogFactoryUtil.getLog(
240                    AbstractMessagingConfigurator.class);
241    
242            private List<Destination> _destinations = new ArrayList<Destination>();
243            private List<DestinationEventListener> _globalDestinationEventListeners =
244                    new ArrayList<DestinationEventListener>();
245            private Map<String, List<MessageListener>> _messageListeners =
246                    new HashMap<String, List<MessageListener>>();
247            private List<Destination> _replacementDestinations =
248                    new ArrayList<Destination>();
249            private Map<String, List<DestinationEventListener>>
250                    _specificDestinationEventListeners =
251                            new HashMap<String, List<DestinationEventListener>>();
252    
253    }