1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.messaging.config;
24  
25  import com.liferay.portal.kernel.messaging.Destination;
26  import com.liferay.portal.kernel.messaging.DestinationEventListener;
27  import com.liferay.portal.kernel.messaging.MessageBus;
28  import com.liferay.portal.kernel.messaging.MessageListener;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * <a href="AbstractMessagingConfigurator.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Michael C. Han
40   */
41  public abstract class AbstractMessagingConfigurator
42      implements MessagingConfigurator {
43  
44      public void destroy() {
45          MessageBus messageBus = getMessageBus();
46  
47          for (Map.Entry<String, List<MessageListener>> messageListeners :
48                  _messageListeners.entrySet()) {
49  
50              String destinationName = messageListeners.getKey();
51  
52              for (MessageListener messageListener :
53                      messageListeners.getValue()) {
54  
55                  messageBus.unregisterMessageListener(
56                      destinationName, messageListener);
57              }
58          }
59  
60          for (Destination destination : _destinations) {
61              messageBus.removeDestination(destination.getName());
62          }
63  
64          for (DestinationEventListener destinationEventListener :
65                  _globalDestinationEventListeners) {
66  
67              messageBus.removeDestinationEventListener(destinationEventListener);
68          }
69      }
70  
71      public void init() {
72          MessageBus messageBus = getMessageBus();
73  
74          for (DestinationEventListener destinationEventListener :
75                  _globalDestinationEventListeners) {
76  
77              messageBus.addDestinationEventListener(destinationEventListener);
78          }
79  
80          for (Destination destination : _destinations) {
81              messageBus.addDestination(destination);
82          }
83  
84          for (Map.Entry<String, List<DestinationEventListener>>
85                  destinationEventListeners :
86                      _specificDestinationEventListeners.entrySet()) {
87  
88              String destinationName = destinationEventListeners.getKey();
89  
90              for (DestinationEventListener destinationEventListener :
91                      destinationEventListeners.getValue()) {
92  
93                  messageBus.addDestinationEventListener(
94                      destinationName, destinationEventListener);
95              }
96          }
97  
98          for (Destination destination : _replacementDestinations) {
99              messageBus.replace(destination);
100         }
101 
102         ClassLoader contextClassLoader =
103             Thread.currentThread().getContextClassLoader();
104 
105         try {
106             ClassLoader operatingClassLoader = getOperatingClassloader();
107 
108             Thread.currentThread().setContextClassLoader(operatingClassLoader);
109 
110             for (Map.Entry<String, List<MessageListener>> messageListeners :
111                     _messageListeners.entrySet()) {
112 
113                 String destinationName = messageListeners.getKey();
114 
115                 for (MessageListener messageListener :
116                         messageListeners.getValue()) {
117 
118                     messageBus.registerMessageListener(
119                         destinationName, messageListener);
120                 }
121             }
122         }
123         finally {
124             Thread.currentThread().setContextClassLoader(contextClassLoader);
125         }
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 
144     public void setReplacementDestinations(
145         List<Destination> replacementDestinations) {
146 
147         _replacementDestinations = replacementDestinations;
148     }
149 
150     public void setSpecificDestinationEventListener(
151         Map<String, List<DestinationEventListener>>
152             specificDestinationEventListeners) {
153 
154         _specificDestinationEventListeners = specificDestinationEventListeners;
155     }
156 
157     protected abstract MessageBus getMessageBus();
158 
159     protected abstract ClassLoader getOperatingClassloader();
160 
161     private List<Destination> _destinations = new ArrayList<Destination>();
162     private List<DestinationEventListener> _globalDestinationEventListeners =
163         new ArrayList<DestinationEventListener>();
164     private Map<String, List<MessageListener>> _messageListeners  =
165         new HashMap<String, List<MessageListener>>();
166     private List<Destination> _replacementDestinations =
167         new ArrayList<Destination>();
168     private Map<String, List<DestinationEventListener>>
169         _specificDestinationEventListeners =
170             new HashMap<String, List<DestinationEventListener>>();
171 
172 }