001    /**
002     * Copyright (c) 2000-2010 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.messaging.Destination;
018    import com.liferay.portal.kernel.messaging.DestinationEventListener;
019    import com.liferay.portal.kernel.messaging.MessageBus;
020    import com.liferay.portal.kernel.messaging.MessageListener;
021    
022    import java.lang.reflect.Method;
023    
024    import java.util.ArrayList;
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * @author Michael C. Han
031     */
032    public abstract class AbstractMessagingConfigurator
033            implements MessagingConfigurator {
034    
035            public void afterPropertiesSet() {
036                    MessageBus messageBus = getMessageBus();
037    
038                    for (DestinationEventListener destinationEventListener :
039                                    _globalDestinationEventListeners) {
040    
041                            messageBus.addDestinationEventListener(destinationEventListener);
042                    }
043    
044                    for (Destination destination : _destinations) {
045                            messageBus.addDestination(destination);
046                    }
047    
048                    for (Map.Entry<String, List<DestinationEventListener>>
049                                    destinationEventListeners :
050                                            _specificDestinationEventListeners.entrySet()) {
051    
052                            String destinationName = destinationEventListeners.getKey();
053    
054                            for (DestinationEventListener destinationEventListener :
055                                            destinationEventListeners.getValue()) {
056    
057                                    messageBus.addDestinationEventListener(
058                                            destinationName, destinationEventListener);
059                            }
060                    }
061    
062                    for (Destination destination : _replacementDestinations) {
063                            messageBus.replace(destination);
064                    }
065    
066                    ClassLoader contextClassLoader =
067                            Thread.currentThread().getContextClassLoader();
068    
069                    try {
070                            ClassLoader operatingClassLoader = getOperatingClassloader();
071    
072                            Thread.currentThread().setContextClassLoader(operatingClassLoader);
073    
074                            for (Map.Entry<String, List<MessageListener>> messageListeners :
075                                            _messageListeners.entrySet()) {
076    
077                                    String destinationName = messageListeners.getKey();
078    
079                                    for (MessageListener messageListener :
080                                                    messageListeners.getValue()) {
081    
082                                            messageBus.registerMessageListener(
083                                                    destinationName, messageListener);
084                                    }
085                            }
086                    }
087                    finally {
088                            Thread.currentThread().setContextClassLoader(contextClassLoader);
089                    }
090            }
091    
092            public void destroy() {
093                    MessageBus messageBus = getMessageBus();
094    
095                    for (Map.Entry<String, List<MessageListener>> messageListeners :
096                                    _messageListeners.entrySet()) {
097    
098                            String destinationName = messageListeners.getKey();
099    
100                            for (MessageListener messageListener :
101                                            messageListeners.getValue()) {
102    
103                                    messageBus.unregisterMessageListener(
104                                            destinationName, messageListener);
105                            }
106                    }
107    
108                    for (Destination destination : _destinations) {
109                            messageBus.removeDestination(destination.getName());
110    
111                            destination.close();
112                    }
113    
114                    for (DestinationEventListener destinationEventListener :
115                                    _globalDestinationEventListeners) {
116    
117                            messageBus.removeDestinationEventListener(destinationEventListener);
118                    }
119            }
120    
121            /**
122             * @deprecated {@link #afterPropertiesSet}
123             */
124            public void init() {
125                    afterPropertiesSet();
126            }
127    
128            public void setDestinations(List<Destination> destinations) {
129                    _destinations = destinations;
130            }
131    
132            public void setGlobalDestinationEventListeners(
133                    List<DestinationEventListener> globalDestinationEventListeners) {
134    
135                    _globalDestinationEventListeners = globalDestinationEventListeners;
136            }
137    
138            public void setMessageListeners(
139                    Map<String, List<MessageListener>> messageListeners) {
140    
141                    _messageListeners = messageListeners;
142    
143                    for (List<MessageListener> messageListenersList :
144                                    _messageListeners.values()) {
145    
146                            for (MessageListener messageListener : messageListenersList) {
147                                    Class<?> messageListenerClass = messageListener.getClass();
148    
149                                    try {
150                                            Method setMessageBusMethod =
151                                                    messageListenerClass.getMethod(
152                                                            "setMessageBus", MessageBus.class);
153    
154                                            setMessageBusMethod.setAccessible(true);
155    
156                                            setMessageBusMethod.invoke(
157                                                    messageListener, getMessageBus());
158    
159                                            continue;
160                                    }
161                                    catch (Exception e) {
162                                    }
163    
164                                    try{
165                                            Method setMessageBusMethod =
166                                                    messageListenerClass.getDeclaredMethod(
167                                                            "setMessageBus", MessageBus.class);
168    
169                                            setMessageBusMethod.setAccessible(true);
170    
171                                            setMessageBusMethod.invoke(
172                                                    messageListener, getMessageBus());
173                                    }
174                                    catch (Exception e) {
175                                    }
176                            }
177                    }
178            }
179    
180            public void setReplacementDestinations(
181                    List<Destination> replacementDestinations) {
182    
183                    _replacementDestinations = replacementDestinations;
184            }
185    
186            public void setSpecificDestinationEventListener(
187                    Map<String, List<DestinationEventListener>>
188                            specificDestinationEventListeners) {
189    
190                    _specificDestinationEventListeners = specificDestinationEventListeners;
191            }
192    
193            protected abstract MessageBus getMessageBus();
194    
195            protected abstract ClassLoader getOperatingClassloader();
196    
197            private List<Destination> _destinations = new ArrayList<Destination>();
198            private List<DestinationEventListener> _globalDestinationEventListeners =
199                    new ArrayList<DestinationEventListener>();
200            private Map<String, List<MessageListener>> _messageListeners  =
201                    new HashMap<String, List<MessageListener>>();
202            private List<Destination> _replacementDestinations =
203                    new ArrayList<Destination>();
204            private Map<String, List<DestinationEventListener>>
205                    _specificDestinationEventListeners =
206                            new HashMap<String, List<DestinationEventListener>>();
207    
208    }