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;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * <a href="DefaultMessageBus.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Michael C. Han
38   *
39   */
40  public class DefaultMessageBus implements MessageBus {
41  
42      public synchronized void addDestination(Destination destination) {
43          _destinations.put(destination.getName(), destination);
44  
45          fireDestinationAddedEvent(destination);
46      }
47  
48      public void addDestinationEventListener(
49          DestinationEventListener destinationEventListener) {
50  
51          _destinationEventListeners.add(destinationEventListener);
52      }
53  
54      public void destroy() {
55          shutdown(true);
56      }
57  
58      public int getDestinationCount() {
59          return _destinations.size();
60      }
61  
62      public Collection<String> getDestinationNames() {
63          return _destinations.keySet();
64      }
65  
66      public Collection<Destination> getDestinations() {
67          return _destinations.values();
68      }
69  
70      public boolean hasDestination(String destinationName) {
71          return _destinations.containsKey(destinationName);
72      }
73  
74      public boolean hasMessageListener(String destinationName) {
75          Destination destination = _destinations.get(destinationName);
76  
77          if ((destination != null) && destination.isRegistered()) {
78              return true;
79          }
80          else {
81              return false;
82          }
83      }
84  
85      public synchronized void registerMessageListener(
86          String destinationName, MessageListener messageListener) {
87  
88          Destination destination = _destinations.get(destinationName);
89  
90          if (destination == null) {
91              throw new IllegalStateException(
92                  "Destination " + destinationName + " is not configured");
93          }
94  
95          destination.register(messageListener);
96      }
97  
98      public synchronized Destination removeDestination(String destinationName) {
99          Destination destinationModel = _destinations.remove(destinationName);
100 
101         fireDestinationRemovedEvent(destinationModel);
102 
103         return destinationModel;
104     }
105 
106     public void removeDestinationEventListener(
107         DestinationEventListener destinationEventListener) {
108 
109         _destinationEventListeners.remove(destinationEventListener);
110     }
111 
112     public void replace(Destination destination) {
113         Destination oldDestination = removeDestination(destination.getName());
114 
115         oldDestination.copyMessageListeners(destination);
116 
117         addDestination(destination);
118     }
119 
120     public void sendMessage(String destinationName, Message message) {
121         Destination destination = _destinations.get(destinationName);
122 
123         if (destination == null) {
124             if (_log.isWarnEnabled()) {
125                 _log.warn(
126                     "Destination " + destinationName + " is not configured");
127             }
128 
129             return;
130         }
131 
132         message.setDestination(destinationName);
133 
134         destination.send(message);
135     }
136 
137     public void shutdown() {
138         shutdown(false);
139     }
140 
141     public synchronized void shutdown(boolean force) {
142         for (Destination destination : _destinations.values()) {
143             destination.close(force);
144         }
145     }
146 
147     public synchronized boolean unregisterMessageListener(
148         String destinationName, MessageListener messageListener) {
149 
150         Destination destination = _destinations.get(destinationName);
151 
152         if (destination == null) {
153             return false;
154         }
155 
156         return destination.unregister(messageListener);
157     }
158 
159     protected void fireDestinationAddedEvent(Destination destination) {
160         for (DestinationEventListener listener : _destinationEventListeners) {
161             listener.destinationAdded(destination);
162         }
163     }
164 
165     protected void fireDestinationRemovedEvent(Destination destination) {
166         for (DestinationEventListener listener : _destinationEventListeners) {
167             listener.destinationRemoved(destination);
168         }
169     }
170 
171     private static Log _log = LogFactoryUtil.getLog(DefaultMessageBus.class);
172 
173     private List<DestinationEventListener> _destinationEventListeners =
174         new ArrayList<DestinationEventListener>();
175     private Map<String, Destination> _destinations =
176         new HashMap<String, Destination>();
177 
178 }