View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak 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 GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.epcis.repository.query;
22  
23  import java.util.Calendar;
24  
25  import javax.management.Notification;
26  import javax.management.timer.Timer;
27  
28  import org.fosstrak.epcis.model.ArrayOfString;
29  import org.fosstrak.epcis.model.Poll;
30  import org.fosstrak.epcis.model.QueryParam;
31  import org.fosstrak.epcis.model.QueryParams;
32  import org.fosstrak.epcis.model.QueryResults;
33  import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * Implementation of triggers. The Schedule checks every once in a while on the
39   * trigger condition. If the trigger condition is met the query associated with
40   * the subscription is executed. The checking frequency may be changed in the
41   * properties.
42   * 
43   * @author Andrea Grössbauer
44   */
45  public class QuerySubscriptionTriggered extends QuerySubscriptionScheduled {
46  
47      private static final long serialVersionUID = 658402150914797471L;
48  
49      private static final Log LOG = LogFactory.getLog(QuerySubscriptionTriggered.class);
50  
51      private String trigger;
52  
53      public QuerySubscriptionTriggered(final String subscriptionID, final QueryParams queryParams, final String dest,
54              final Boolean reportIfEmpty, final Calendar initialRecordTime,
55              final Calendar lastTimeExecuted, final String queryName, final String trigger,
56              final Schedule every10min) throws ImplementationExceptionResponse {
57          super(subscriptionID, queryParams, dest, reportIfEmpty, initialRecordTime, lastTimeExecuted, every10min,
58                queryName);
59          this.trigger = trigger;
60      }
61  
62      /**
63       * {@inheritDoc} First checks on the trigger condition: if fulfilled then
64       * execute Query.
65       * 
66       * @see org.fosstrak.epcis.repository.query.QuerySubscriptionScheduled#handleNotification(javax.management.Notification,
67       *      java.lang.Object)
68       */
69      @Override
70      public void handleNotification(final Notification pNotification, final Object pHandback) {
71          if (pHandback == null) {
72              LOG.error("The timer stating the next scheduled query execution time is null!");
73              return;
74          }
75          Timer timer = (Timer) pHandback;
76  
77          if (!doItAgain.booleanValue()) {
78              timer.stop();
79          } else {
80              try {
81                  LOG.debug("Checking trigger condition ...");
82                  String queryName = "SimpleEventQuery";
83                  QueryParams params = new QueryParams();
84  
85                  // add MATCH_anyEPC query param
86                  QueryParam param = new QueryParam();
87                  param.setName("MATCH_anyEPC");
88                  ArrayOfString strings = new ArrayOfString();
89                  strings.getString().add(trigger);
90                  param.setValue(strings);
91                  params.getParam().add(param);
92  
93                  // add GE_recordTime query param
94                  param = new QueryParam();
95                  param.setName("GE_recordTime");
96                  param.setValue(initialRecordTime);
97                  params.getParam().add(param);
98  
99                  // send the query
100                 Poll poll = new Poll();
101                 poll.setParams(params);
102                 poll.setQueryName(queryName);
103                 QueryResults results = executePoll(poll);
104                 if (results != null && results.getResultsBody() != null
105                         && results.getResultsBody().getEventList() != null) {
106                     LOG.debug("Trigger condition fulfilled!");
107                     LOG.debug("Executing subscribed query associated with trigger event ...");
108                     super.executeQuery();
109                     LOG.debug("Triggered query successfully executed!");
110                 }
111             } catch (Exception e) {
112                 String msg = "An error occurred while checking trigger condition for query with subscriptionID '"
113                         + subscriptionID + "': " + e.getMessage();
114                 LOG.error(msg, e);
115             }
116 
117             // determine next scheduled execution time
118             setNextScheduledExecutionTime(timer);
119         }
120     }
121 }