001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 (DestinationEventListener destinationEventListener :
122                                    _globalDestinationEventListeners) {
123    
124                            messageBus.removeDestinationEventListener(destinationEventListener);
125                    }
126            }
127    
128            /**
129             * @deprecated {@link #afterPropertiesSet}
130             */
131            public void init() {
132                    afterPropertiesSet();
133            }
134    
135            public void setDestinations(List<Destination> destinations) {
136                    for (Destination destination : destinations) {
137                            SecurityManager securityManager = System.getSecurityManager();
138    
139                            if (securityManager != null) {
140                                    Permission permission = new PortalMessageBusPermission(
141                                            PACLConstants.PORTAL_MESSAGE_BUS_PERMISSION_LISTEN,
142                                            destination.getName());
143    
144                                    try {
145                                            securityManager.checkPermission(permission);
146                                    }
147                                    catch (SecurityException se) {
148                                            if (_log.isInfoEnabled()) {
149                                                    _log.info(
150                                                            "Rejecting destination " + destination.getName());
151                                            }
152    
153                                            continue;
154                                    }
155                            }
156    
157                            _destinations.add(destination);
158                    }
159            }
160    
161            public void setGlobalDestinationEventListeners(
162                    List<DestinationEventListener> globalDestinationEventListeners) {
163    
164                    _globalDestinationEventListeners = globalDestinationEventListeners;
165            }
166    
167            public void setMessageListeners(
168                    Map<String, List<MessageListener>> messageListeners) {
169    
170                    _messageListeners = messageListeners;
171    
172                    for (List<MessageListener> messageListenersList :
173                                    _messageListeners.values()) {
174    
175                            for (MessageListener messageListener : messageListenersList) {
176                                    Class<?> messageListenerClass = messageListener.getClass();
177    
178                                    try {
179                                            Method setMessageBusMethod = messageListenerClass.getMethod(
180                                                    "setMessageBus", MessageBus.class);
181    
182                                            setMessageBusMethod.setAccessible(true);
183    
184                                            setMessageBusMethod.invoke(
185                                                    messageListener, getMessageBus());
186    
187                                            continue;
188                                    }
189                                    catch (Exception e) {
190                                    }
191    
192                                    try {
193                                            Method setMessageBusMethod =
194                                                    messageListenerClass.getDeclaredMethod(
195                                                            "setMessageBus", MessageBus.class);
196    
197                                            setMessageBusMethod.setAccessible(true);
198    
199                                            setMessageBusMethod.invoke(
200                                                    messageListener, getMessageBus());
201                                    }
202                                    catch (Exception e) {
203                                    }
204                            }
205                    }
206            }
207    
208            public void setReplacementDestinations(
209                    List<Destination> replacementDestinations) {
210    
211                    _replacementDestinations = replacementDestinations;
212            }
213    
214            public void setSpecificDestinationEventListener(
215                    Map<String, List<DestinationEventListener>>
216                            specificDestinationEventListeners) {
217    
218                    _specificDestinationEventListeners = specificDestinationEventListeners;
219            }
220    
221            protected abstract MessageBus getMessageBus();
222    
223            protected abstract ClassLoader getOperatingClassloader();
224    
225            private static Log _log = LogFactoryUtil.getLog(
226                    AbstractMessagingConfigurator.class);
227    
228            private List<Destination> _destinations = new ArrayList<Destination>();
229            private List<DestinationEventListener> _globalDestinationEventListeners =
230                    new ArrayList<DestinationEventListener>();
231            private Map<String, List<MessageListener>> _messageListeners =
232                    new HashMap<String, List<MessageListener>>();
233            private List<Destination> _replacementDestinations =
234                    new ArrayList<Destination>();
235            private Map<String, List<DestinationEventListener>>
236                    _specificDestinationEventListeners =
237                            new HashMap<String, List<DestinationEventListener>>();
238    
239    }