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 LocalObservationBus implements ObservationBus {
36      private final ChangeObservers observers = new ChangeObservers();
37      private static final Logger LOGGER = Logger.getLogger(LocalObservationBus.class);
38  
39      public LocalObservationBus() {
40      }
41  
42      /**
43       * {@inheritDoc}
44       * 
45       * @see org.modeshape.graph.observe.ObservationBus#start()
46       */
47      public void start() {
48          // nothing to do
49      }
50  
51      /**
52       * {@inheritDoc}
53       * 
54       * @see org.modeshape.graph.observe.Observable#register(org.modeshape.graph.observe.Observer)
55       */
56      public boolean register( Observer observer ) {
57          if (observer == null) return false;
58          return observers.register(observer);
59      }
60  
61      /**
62       * {@inheritDoc}
63       * 
64       * @see org.modeshape.graph.observe.Observable#unregister(org.modeshape.graph.observe.Observer)
65       */
66      public boolean unregister( Observer observer ) {
67          return observers.unregister(observer);
68      }
69  
70      /**
71       * {@inheritDoc}
72       * 
73       * @see org.modeshape.graph.observe.Observer#notify(org.modeshape.graph.observe.Changes)
74       */
75      public void notify( Changes changes ) {
76          if (changes != null) {
77              // Broadcast the changes to the registered observers ...
78              try {
79                  observers.broadcast(changes);
80              } catch (RuntimeException t) {
81                  LOGGER.error(t, GraphI18n.errorNotifyingObserver, t.getLocalizedMessage());
82              }
83          }
84      }
85  
86      /**
87       * {@inheritDoc}
88       * 
89       * @see org.modeshape.graph.observe.ObservationBus#hasObservers()
90       */
91      public boolean hasObservers() {
92          return !observers.isEmpty();
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see org.modeshape.graph.observe.ObservationBus#shutdown()
99       */
100     public void shutdown() {
101         observers.shutdown();
102     }
103 }