View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
15   */
16  package org.jmonit.events;
17  
18  import java.util.List;
19  import java.util.concurrent.CopyOnWriteArrayList;
20  
21  /**
22   * A synchronous implementation of the MonitoringEventBus.
23   * <p>
24   * "Synchronous" means the event is dispatched to and consumed by all listeners
25   * before the emitter can resume processing.
26   * 
27   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
28   */
29  public class DefaultMonitoringEventBus
30      implements MonitoringEventBus
31  {
32      /**
33       * Registered listeners
34       */
35      private List<MonitoringEventListener> listeners =
36          new CopyOnWriteArrayList<MonitoringEventListener>();
37  
38      /**
39       * {@inheritDoc}
40       * 
41       * @see org.jmonit.events.MonitoringEventBus#addListener(org.jmonit.events.MonitoringEventListener)
42       */
43      public void addListener( final MonitoringEventListener listener )
44      {
45          if ( listener != null )
46          {
47              listeners.add( listener );
48          }
49      }
50  
51      /**
52       * {@inheritDoc}
53       * 
54       * @see org.jmonit.events.MonitoringEventBus#removeListener(MonitoringEventListener)
55       */
56      public void removeListener( final MonitoringEventListener listener )
57      {
58          listeners.remove( listener );
59      }
60  
61      /**
62       * {@inheritDoc}
63       * 
64       * @see org.jmonit.events.MonitoringEventBus#hasListener(MonitoringEventListener)
65       */
66      public boolean hasListener( final MonitoringEventListener listener )
67      {
68          return listeners.contains( listener );
69      }
70  
71      /**
72       * {@inheritDoc}
73       * 
74       * @see org.jmonit.events.MonitoringEventBus#fireMonitoringEvent(org.jmonit.events.MonitoringEvent)
75       */
76      public void fireMonitoringEvent( final MonitoringEvent event )
77      {
78          for ( MonitoringEventListener listener : listeners )
79          {
80              listener.onMonitoringEvent( event );
81          }
82      }
83  }