2626import javax .ejb .Local ;
2727import javax .naming .ConfigurationException ;
2828
29- import com .cloud .utils .Pair ;
30- import org .apache .log4j .Logger ;
31-
3229import org .apache .cloudstack .framework .events .Event ;
3330import org .apache .cloudstack .framework .events .EventBus ;
3431import org .apache .cloudstack .framework .events .EventBusException ;
3532import org .apache .cloudstack .framework .events .EventSubscriber ;
3633import org .apache .cloudstack .framework .events .EventTopic ;
34+ import org .apache .log4j .Logger ;
3735
36+ import com .cloud .utils .Pair ;
3837import com .cloud .utils .component .ManagerBase ;
3938
4039@ Local (value = EventBus .class )
4140public class InMemoryEventBus extends ManagerBase implements EventBus {
4241
43- private String name ;
4442 private static final Logger s_logger = Logger .getLogger (InMemoryEventBus .class );
45- private static ConcurrentHashMap <UUID , Pair <EventTopic , EventSubscriber >> s_subscribers ;
43+
44+ private final static ConcurrentHashMap <UUID , Pair <EventTopic , EventSubscriber >> subscribers ;
45+
46+ static {
47+ subscribers = new ConcurrentHashMap <UUID , Pair <EventTopic , EventSubscriber >>();
48+ }
4649
4750 @ Override
4851 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
49- s_subscribers = new ConcurrentHashMap < UUID , Pair < EventTopic , EventSubscriber >>() ;
52+ _name = name ;
5053 return true ;
5154 }
5255
5356 @ Override
5457 public void setName (String name ) {
55- this . name = name ;
58+ _name = name ;
5659 }
5760
5861 @ Override
@@ -62,29 +65,35 @@ public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws Event
6265 }
6366 UUID subscriberId = UUID .randomUUID ();
6467
65- s_subscribers .put (subscriberId , new Pair (topic , subscriber ));
68+ subscribers .put (subscriberId , new Pair < EventTopic , EventSubscriber > (topic , subscriber ));
6669 return subscriberId ;
6770 }
6871
6972 @ Override
7073 public void unsubscribe (UUID subscriberId , EventSubscriber subscriber ) throws EventBusException {
71- if (s_subscribers != null && s_subscribers .isEmpty ()) {
74+ if (subscriberId == null ) {
75+ throw new EventBusException ("Cannot unregister a null subscriberId." );
76+ }
77+
78+ if (subscribers .isEmpty ()) {
7279 throw new EventBusException ("There are no registered subscribers to unregister." );
7380 }
74- if (s_subscribers .get (subscriberId ) == null ) {
81+
82+ if (!subscribers .containsKey (subscriberId )) {
7583 throw new EventBusException ("No subscriber found with subscriber id " + subscriberId );
84+ } else {
85+ subscribers .remove (subscriberId );
7686 }
77- s_subscribers .remove (subscriberId );
7887 }
7988
8089 @ Override
8190 public void publish (Event event ) throws EventBusException {
82- if (s_subscribers == null || s_subscribers .isEmpty ()) {
91+ if (subscribers == null || subscribers .isEmpty ()) {
8392 return ; // no subscriber to publish to, so just return
8493 }
8594
86- for (UUID subscriberId : s_subscribers .keySet ()) {
87- Pair <EventTopic , EventSubscriber > subscriberDetails = s_subscribers .get (subscriberId );
95+ for (UUID subscriberId : subscribers .keySet ()) {
96+ Pair <EventTopic , EventSubscriber > subscriberDetails = subscribers .get (subscriberId );
8897 // if the event matches subscribers interested event topic then call back the subscriber with the event
8998 if (isEventMatchesTopic (event , subscriberDetails .first ())) {
9099 EventSubscriber subscriber = subscriberDetails .second ();
@@ -108,6 +117,10 @@ public boolean stop() {
108117 return true ;
109118 }
110119
120+ public int totalSubscribers () {
121+ return subscribers .size ();
122+ }
123+
111124 private String replaceNullWithWildcard (String key ) {
112125 if (key == null || key .isEmpty ()) {
113126 return "*" ;
@@ -122,42 +135,42 @@ private boolean isEventMatchesTopic(Event event, EventTopic topic) {
122135 eventTopicSource = eventTopicSource .replace ("." , "-" );
123136 String eventSource = replaceNullWithWildcard (event .getEventSource ());
124137 eventSource = eventSource .replace ("." , "-" );
125- if (eventTopicSource != "*" && eventSource != "*" && !eventTopicSource .equalsIgnoreCase (eventSource )) {
138+ if (! eventTopicSource . equals ( "*" ) && ! eventSource . equals ( "*" ) && !eventTopicSource .equalsIgnoreCase (eventSource )) {
126139 return false ;
127140 }
128141
129142 String eventTopicCategory = replaceNullWithWildcard (topic .getEventCategory ());
130143 eventTopicCategory = eventTopicCategory .replace ("." , "-" );
131144 String eventCategory = replaceNullWithWildcard (event .getEventCategory ());
132145 eventCategory = eventCategory .replace ("." , "-" );
133- if (eventTopicCategory != "*" && eventCategory != "*" && !eventTopicCategory .equalsIgnoreCase (eventCategory )) {
146+ if (! eventTopicCategory . equals ( "*" ) && ! eventCategory . equals ( "*" ) && !eventTopicCategory .equalsIgnoreCase (eventCategory )) {
134147 return false ;
135148 }
136149
137150 String eventTopicType = replaceNullWithWildcard (topic .getEventType ());
138151 eventTopicType = eventTopicType .replace ("." , "-" );
139152 String eventType = replaceNullWithWildcard (event .getEventType ());
140153 eventType = eventType .replace ("." , "-" );
141- if (eventTopicType != "*" && eventType != "*" && !eventTopicType .equalsIgnoreCase (eventType )) {
154+ if (! eventTopicType . equals ( "*" ) && ! eventType . equals ( "*" ) && !eventTopicType .equalsIgnoreCase (eventType )) {
142155 return false ;
143156 }
144157
145158 String eventTopicResourceType = replaceNullWithWildcard (topic .getResourceType ());
146159 eventTopicResourceType = eventTopicResourceType .replace ("." , "-" );
147160 String resourceType = replaceNullWithWildcard (event .getResourceType ());
148161 resourceType = resourceType .replace ("." , "-" );
149- if (eventTopicResourceType != "*" && resourceType != "*" && !eventTopicResourceType .equalsIgnoreCase (resourceType )) {
162+ if (! eventTopicResourceType . equals ( "*" ) && ! resourceType . equals ( "*" ) && !eventTopicResourceType .equalsIgnoreCase (resourceType )) {
150163 return false ;
151164 }
152165
153166 String resourceUuid = replaceNullWithWildcard (event .getResourceUUID ());
154167 resourceUuid = resourceUuid .replace ("." , "-" );
155168 String eventTopicresourceUuid = replaceNullWithWildcard (topic .getResourceUUID ());
156169 eventTopicresourceUuid = eventTopicresourceUuid .replace ("." , "-" );
157- if (resourceUuid != "*" && eventTopicresourceUuid != "*" && !resourceUuid .equalsIgnoreCase (eventTopicresourceUuid )) {
170+ if (! resourceUuid . equals ( "*" ) && ! eventTopicresourceUuid . equals ( "*" ) && !resourceUuid .equalsIgnoreCase (eventTopicresourceUuid )) {
158171 return false ;
159172 }
160173
161174 return true ;
162175 }
163- }
176+ }
0 commit comments