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 destinationEventListener :
71                  _destinationEventListeners) {
72  
73              messageBus.addDestinationEventListener(destinationEventListener);
74          }
75  
76          for (Destination destination : _destinations) {
77              messageBus.addDestination(destination);
78          }
79  
80          for (Destination destination : _replacementDestinations) {
81              messageBus.replace(destination);
82          }
83  
84          ClassLoader contextClassLoader =
85              Thread.currentThread().getContextClassLoader();
86  
87          try {
88              ClassLoader operatingClassLoader = getOperatingClassloader();
89  
90              Thread.currentThread().setContextClassLoader(operatingClassLoader);
91  
92              for (Map.Entry<String, List<MessageListener>> listeners :
93                      _messageListeners.entrySet()) {
94  
95                  String destination = listeners.getKey();
96  
97                  for (MessageListener listener : listeners.getValue()) {
98                      messageBus.registerMessageListener(destination, listener);
99                  }
100             }
101         }
102         finally {
103             Thread.currentThread().setContextClassLoader(contextClassLoader);
104         }
105     }
106 
107     public void setDestinationEventListeners(
108         List<DestinationEventListener> destinationEventListeners) {
109 
110         _destinationEventListeners = destinationEventListeners;
111     }
112 
113     public void setDestinations(List<Destination> destinations) {
114         _destinations = destinations;
115     }
116 
117     public void setMessageListeners(
118         Map<String, List<MessageListener>> messageListeners) {
119 
120         _messageListeners = messageListeners;
121     }
122 
123     public void setReplacementDestinations(List<Destination> destinations) {
124         _replacementDestinations = destinations;
125     }
126 
127     protected abstract MessageBus getMessageBus();
128     protected abstract ClassLoader getOperatingClassloader();
129 
130     private List<DestinationEventListener> _destinationEventListeners =
131         new ArrayList<DestinationEventListener>();
132     private List<Destination> _destinations = new ArrayList<Destination>();
133     private Map<String, List<MessageListener>> _messageListeners  =
134         new HashMap<String, List<MessageListener>>();
135     private List<Destination> _replacementDestinations =
136         new ArrayList<Destination>();
137 
138 }