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.esper.internal;
19  
20  import org.apache.log4j.Logger;
21  import org.w3c.dom.Document;
22  import at.ac.tuwien.infosys.sm4all.copal.api.ContextListener;
23  import com.espertech.esper.client.EventBean;
24  import com.espertech.esper.client.UnmatchedListener;
25  
26  /**
27   * Esper's UnmatchedListener which helps transform Esper's event to context
28   * events understood by {@link ContextListener}.
29   * 
30   * @author sanjin
31   */
32  public class UnmatchedEventListener implements UnmatchedListener {
33  
34      private static final Logger LOGGER = Logger.getLogger(UnmatchedEventListener.class);
35  
36      private final ContextListener listener;
37  
38      /**
39       * Creates Esper's unmatched listener using specified context listener to
40       * notify about unmatched events.
41       * 
42       * @param listener the context listener for unmatched events.
43       */
44      public UnmatchedEventListener(final ContextListener listener) {
45          super();
46          this.listener = listener;
47      }
48  
49      @Override
50      public void update(final EventBean eventBean) {
51          final Object event = eventBean.get("*");
52          if (event instanceof Document)
53              this.listener.onEvent((Document) event);
54          else
55              LOGGER.warn("Recieved non-context event! Ignoring.");
56      }
57  }