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