View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.publisher;
19  
20  import java.text.MessageFormat;
21  import org.apache.log4j.Level;
22  import org.apache.log4j.Logger;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventType;
25  import at.ac.tuwien.infosys.sm4all.copal.api.service.FailedPublishingException;
26  
27  /**
28   * Simple {@link ContextPublisher} that can publish {@link ContextEvent}s of
29   * only one {@link ContextEventType}.
30   * 
31   * @author sanjin
32   */
33  public class SimplePublisher extends BasePublisher {
34  
35      private static final Logger LOGGER = Logger.getLogger(SimplePublisher.class);
36  
37      private final String sourceID;
38      private final ContextEventType publishedType;
39  
40      /**
41       * Creates an instance of simple {@link ContextPublisher} with specified
42       * source ID and and published {@link ContextEventType}.
43       * 
44       * @param sourceID the source ID of this {@link ContextPublisher}.
45       * @param publishedType the published {@link ContextEventType}.
46       */
47      protected SimplePublisher(final String sourceID,
48              final ContextEventType publishedType) {
49          super(sourceID, publishedType.getName());
50  
51          this.sourceID = sourceID;
52          this.publishedType = publishedType;
53      }
54  
55      /**
56       * Returns if this {@link SimplePublisher} is started and can publish
57       * {@link ContextEvent}s.
58       * 
59       * @return if this {@link SimplePublisher} is started.
60       */
61      public boolean isStarted() {
62          return super.isStarted(this.publishedType.getName());
63      }
64  
65      @Override
66      protected boolean start(final ContextEventType type) {
67          return this.publishedType.equals(type);
68      }
69  
70      @Override
71      protected void stop(final ContextEventType type) {
72          /* do nothing */
73      }
74  
75      @Override
76      public void publish(final ContextEvent event)
77              throws FailedPublishingException {
78          if (this.sourceID.equals(event.getSourceID())) {
79              super.publish(event);
80          } else {
81              if (LOGGER.isEnabledFor(Level.ERROR)) {
82                  LOGGER.error(MessageFormat.format(
83                          "Failed to publish event ''{0}'' from ''{1}''! Wrong source ID ''{2}''.",
84                          event.getType().getName(), this.sourceID,
85                          event.getSourceID()));
86              }
87              throw new FailedPublishingException("Wrong source ID.", event);
88          }
89      }
90  }