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.core.internal;
19  
20  import java.text.MessageFormat;
21  import org.apache.log4j.Logger;
22  import org.w3c.dom.Document;
23  import at.ac.tuwien.infosys.sm4all.copal.api.ContextException;
24  import at.ac.tuwien.infosys.sm4all.copal.api.ContextListener;
25  import at.ac.tuwien.infosys.sm4all.copal.api.MalformedDocumentException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
27  import at.ac.tuwien.infosys.sm4all.copal.api.event.CurrentAction;
28  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ActionsFinished;
29  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.XMLContextEvent;
30  import at.ac.tuwien.infosys.sm4all.copal.service.event.PublishingService;
31  
32  /**
33   * Processor of events that were not matched by any query.
34   * 
35   * @author sanjin
36   */
37  public class UnmatchedEventProcessor implements ContextListener {
38  
39      private static final Logger LOGGER = Logger.getLogger(UnmatchedEventProcessor.class);
40  
41      private final PublishingService publishingService;
42  
43      /**
44       * Creates an instance using specified {@link PublishingService} to
45       * republish {@link ContextEvent}s that were not matched by any other
46       * processor for an optional action.
47       * 
48       * @param publishingService the {@link PublishingService}.
49       */
50      public UnmatchedEventProcessor(final PublishingService publishingService) {
51          super();
52          this.publishingService = publishingService;
53      }
54  
55      @Override
56      public String getName() {
57          return "UnmatchedEventProcessor";
58      }
59  
60      @Override
61      public void onEvent(final Document document) {
62          try {
63              if (!ActionsFinished.INSTANCE.retrieve(document)) {
64                  final XMLContextEvent event = new XMLContextEvent(document);
65                  final CurrentAction currentAction = event.getCurrentAction();
66                  if (currentAction.isRequried())
67                      LOGGER.error(MessageFormat.format(
68                              "Dropped event ''{0}''! No processor for the required ''{1}'' action.",
69                              event.getType().getName(), currentAction.getName()));
70                  else {
71                      event.nextAction();
72                      try {
73                          this.publishingService.publish(event);
74                      } catch (final ContextException ex) {
75                          LOGGER.error(
76                                  MessageFormat.format(
77                                          "Could not republish event ''{0}''! Dropping the event.",
78                                          event.getType().getName()), ex);
79                      }
80                  }
81              }
82          } catch (final MalformedDocumentException ex) {
83              LOGGER.error("Dropped malformed event!", ex);
84          }
85      }
86  }