1   /**
2    * Copyright (c) 2000-2008 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.List;
31  import java.util.Map;
32  
33  /**
34   * <a href="AbstractMessagingConfigurator.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Michael C. Han
38   *
39   */
40  public abstract class AbstractMessagingConfigurator
41      implements MessagingConfigurator {
42  
43      public void destroy() {
44          MessageBus messageBus = getMessageBus();
45  
46          for (Map.Entry<String, List<MessageListener>> listeners :
47                  _messageListeners.entrySet()) {
48  
49              String destination = listeners.getKey();
50  
51              for (MessageListener listener : listeners.getValue()) {
52                  messageBus.unregisterMessageListener(destination, listener);
53              }
54          }
55  
56          for (Destination destination : _destinations) {
57              messageBus.removeDestination(destination.getName());
58          }
59  
60          for (DestinationEventListener listener : _destinationEventListeners) {
61              messageBus.removeDestinationEventListener(listener);
62          }
63      }
64  
65      public void init() {
66          MessageBus messageBus = getMessageBus();
67  
68          for (DestinationEventListener listener : _destinationEventListeners) {
69              messageBus.addDestinationEventListener(listener);
70          }
71  
72          for (Destination destination : _destinations) {
73              messageBus.addDestination(destination);
74          }
75  
76          for (Map.Entry<String, List<MessageListener>> listeners :
77                  _messageListeners.entrySet()) {
78  
79              String destination = listeners.getKey();
80  
81              for (MessageListener listener : listeners.getValue()) {
82                  messageBus.registerMessageListener(destination, listener);
83              }
84          }
85      }
86  
87      public void setDestinationEventListeners(
88          List<DestinationEventListener> destinationEventListeners) {
89  
90          _destinationEventListeners = destinationEventListeners;
91      }
92  
93      public void setDestinations(List<Destination> destinations) {
94          _destinations = destinations;
95      }
96  
97      public void setMessageListeners(
98          Map<String, List<MessageListener>> messageListeners) {
99  
100         _messageListeners = messageListeners;
101     }
102 
103     protected abstract MessageBus getMessageBus();
104 
105     private List<DestinationEventListener> _destinationEventListeners;
106     private List<Destination> _destinations;
107     private Map<String, List<MessageListener>> _messageListeners;
108 
109 }