001    /**
002     * Copyright (c) 2000-2010 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.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                    Thread currentThread = Thread.currentThread();
067    
068                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
069    
070                    try {
071                            ClassLoader operatingClassLoader = getOperatingClassloader();
072    
073                            currentThread.setContextClassLoader(operatingClassLoader);
074    
075                            for (Map.Entry<String, List<MessageListener>> messageListeners :
076                                            _messageListeners.entrySet()) {
077    
078                                    String destinationName = messageListeners.getKey();
079    
080                                    for (MessageListener messageListener :
081                                                    messageListeners.getValue()) {
082    
083                                            messageBus.registerMessageListener(
084                                                    destinationName, messageListener);
085                                    }
086                            }
087                    }
088                    finally {
089                            currentThread.setContextClassLoader(contextClassLoader);
090                    }
091            }
092    
093            public void destroy() {
094                    MessageBus messageBus = getMessageBus();
095    
096                    for (Map.Entry<String, List<MessageListener>> messageListeners :
097                                    _messageListeners.entrySet()) {
098    
099                            String destinationName = messageListeners.getKey();
100    
101                            for (MessageListener messageListener :
102                                            messageListeners.getValue()) {
103    
104                                    messageBus.unregisterMessageListener(
105                                            destinationName, messageListener);
106                            }
107                    }
108    
109                    for (Destination destination : _destinations) {
110                            messageBus.removeDestination(destination.getName());
111    
112                            destination.close();
113                    }
114    
115                    for (DestinationEventListener destinationEventListener :
116                                    _globalDestinationEventListeners) {
117    
118                            messageBus.removeDestinationEventListener(destinationEventListener);
119                    }
120            }
121    
122            /**
123             * @deprecated {@link #afterPropertiesSet}
124             */
125            public void init() {
126                    afterPropertiesSet();
127            }
128    
129            public void setDestinations(List<Destination> destinations) {
130                    _destinations = destinations;
131            }
132    
133            public void setGlobalDestinationEventListeners(
134                    List<DestinationEventListener> globalDestinationEventListeners) {
135    
136                    _globalDestinationEventListeners = globalDestinationEventListeners;
137            }
138    
139            public void setMessageListeners(
140                    Map<String, List<MessageListener>> messageListeners) {
141    
142                    _messageListeners = messageListeners;
143    
144                    for (List<MessageListener> messageListenersList :
145                                    _messageListeners.values()) {
146    
147                            for (MessageListener messageListener : messageListenersList) {
148                                    Class<?> messageListenerClass = messageListener.getClass();
149    
150                                    try {
151                                            Method setMessageBusMethod =
152                                                    messageListenerClass.getMethod(
153                                                            "setMessageBus", MessageBus.class);
154    
155                                            setMessageBusMethod.setAccessible(true);
156    
157                                            setMessageBusMethod.invoke(
158                                                    messageListener, getMessageBus());
159    
160                                            continue;
161                                    }
162                                    catch (Exception e) {
163                                    }
164    
165                                    try{
166                                            Method setMessageBusMethod =
167                                                    messageListenerClass.getDeclaredMethod(
168                                                            "setMessageBus", MessageBus.class);
169    
170                                            setMessageBusMethod.setAccessible(true);
171    
172                                            setMessageBusMethod.invoke(
173                                                    messageListener, getMessageBus());
174                                    }
175                                    catch (Exception e) {
176                                    }
177                            }
178                    }
179            }
180    
181            public void setReplacementDestinations(
182                    List<Destination> replacementDestinations) {
183    
184                    _replacementDestinations = replacementDestinations;
185            }
186    
187            public void setSpecificDestinationEventListener(
188                    Map<String, List<DestinationEventListener>>
189                            specificDestinationEventListeners) {
190    
191                    _specificDestinationEventListeners = specificDestinationEventListeners;
192            }
193    
194            protected abstract MessageBus getMessageBus();
195    
196            protected abstract ClassLoader getOperatingClassloader();
197    
198            private List<Destination> _destinations = new ArrayList<Destination>();
199            private List<DestinationEventListener> _globalDestinationEventListeners =
200                    new ArrayList<DestinationEventListener>();
201            private Map<String, List<MessageListener>> _messageListeners  =
202                    new HashMap<String, List<MessageListener>>();
203            private List<Destination> _replacementDestinations =
204                    new ArrayList<Destination>();
205            private Map<String, List<DestinationEventListener>>
206                    _specificDestinationEventListeners =
207                            new HashMap<String, List<DestinationEventListener>>();
208    
209    }