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