View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.observe;
25  
26  import net.jcip.annotations.ThreadSafe;
27  import org.modeshape.common.util.Logger;
28  import org.modeshape.graph.GraphI18n;
29  
30  /**
31   * A simple {@link Observer} that is itself {@link Observable}. This class essentially multiplexes the events from a single
32   * Observable to disseminate each event to multiple Observers.
33   */
34  @ThreadSafe
35  public class ObservationBus implements Observable, Observer {
36      private final ChangeObservers observers = new ChangeObservers();
37      private static final Logger LOGGER = Logger.getLogger(ObservationBus.class);
38  
39      public ObservationBus() {
40      }
41  
42      /**
43       * {@inheritDoc}
44       * 
45       * @see org.modeshape.graph.observe.Observable#register(org.modeshape.graph.observe.Observer)
46       */
47      public boolean register( Observer observer ) {
48          if (observer == null) return false;
49          return observers.register(observer);
50      }
51  
52      /**
53       * {@inheritDoc}
54       * 
55       * @see org.modeshape.graph.observe.Observable#unregister(org.modeshape.graph.observe.Observer)
56       */
57      public boolean unregister( Observer observer ) {
58          return observers.unregister(observer);
59      }
60  
61      /**
62       * {@inheritDoc}
63       * 
64       * @see org.modeshape.graph.observe.Observer#notify(org.modeshape.graph.observe.Changes)
65       */
66      public void notify( Changes changes ) {
67          if (changes != null) {
68              // Broadcast the changes to the registered observers ...
69              try {
70                  observers.broadcast(changes);
71              } catch (RuntimeException t) {
72                  LOGGER.error(t, GraphI18n.errorNotifyingObserver, t.getLocalizedMessage());
73              }
74          }
75      }
76  
77      /**
78       * Determine whether this particular bus currently has any observers.
79       * 
80       * @return true if there is at least one observer, or false otherwise
81       */
82      public boolean hasObservers() {
83          return !observers.isEmpty();
84      }
85  
86      /**
87       * Unregister all registered observers, and mark this as no longer accepting new registered observers.
88       */
89      public void shutdown() {
90          observers.shutdown();
91      }
92  }