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   */
42  public abstract class AbstractMessagingConfigurator
43      implements MessagingConfigurator {
44  
45      public void destroy() {
46          MessageBus messageBus = getMessageBus();
47  
48          for (Map.Entry<String, List<MessageListener>> listeners :
49                  _messageListeners.entrySet()) {
50  
51              String destination = listeners.getKey();
52  
53              for (MessageListener listener : listeners.getValue()) {
54                  messageBus.unregisterMessageListener(destination, listener);
55              }
56          }
57  
58          for (Destination destination : _destinations) {
59              messageBus.removeDestination(destination.getName());
60          }
61  
62          for (DestinationEventListener listener : _destinationEventListeners) {
63              messageBus.removeDestinationEventListener(listener);
64          }
65      }
66  
67      public void init() {
68          MessageBus messageBus = getMessageBus();
69  
70          for (DestinationEventListener listener : _destinationEventListeners) {
71              messageBus.addDestinationEventListener(listener);
72          }
73  
74          for (Destination destination : _destinations) {
75              messageBus.addDestination(destination);
76          }
77  
78          for (Map.Entry<String, List<MessageListener>> listeners :
79                  _messageListeners.entrySet()) {
80  
81              String destination = listeners.getKey();
82  
83              for (MessageListener listener : listeners.getValue()) {
84                  messageBus.registerMessageListener(destination, listener);
85              }
86          }
87      }
88  
89      public void setDestinationEventListeners(
90          List<DestinationEventListener> destinationEventListeners) {
91  
92          _destinationEventListeners = destinationEventListeners;
93      }
94  
95      public void setDestinations(List<Destination> destinations) {
96          _destinations = destinations;
97      }
98  
99      public void setMessageListeners(
100         Map<String, List<MessageListener>> messageListeners) {
101 
102         _messageListeners = messageListeners;
103     }
104 
105     protected abstract MessageBus getMessageBus();
106 
107     private List<DestinationEventListener> _destinationEventListeners =
108         new ArrayList<DestinationEventListener>();
109     private List<Destination> _destinations = new ArrayList<Destination>();
110     private Map<String, List<MessageListener>> _messageListeners  =
111         new HashMap<String, List<MessageListener>>();
112 
113 }