1
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
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 }