View Javadoc

1   /*
2    *  Copyright 2010 Felix Roethenbacher
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package ch.syabru.nagios.broker.impl;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  
21  import org.apache.commons.lang.Validate;
22  
23  import ch.syabru.nagios.broker.ExternalCommand;
24  import ch.syabru.nagios.broker.ExternalCommandFactory;
25  
26  /**
27   * Schedule service check command factory.
28   *
29   * @author Felix Roethenbacher
30   *
31   */
32  public class ScheduleServiceCheckCommandFactory
33  implements ExternalCommandFactory
34  {
35      private String hostName;
36      private String serviceDescription;
37      private Date checkTime;
38      private int delaySeconds;
39  
40      /**
41       * C'tor.
42       * @param hostName Host name.
43       * @param serviceDescription Service description.
44       * @param checkTime Absolute check time, set to null for current time.
45       * @param delaySeconds Delay in seconds. Set to 0 for immediate check.
46       */
47      public ScheduleServiceCheckCommandFactory(String hostName,
48              String serviceDescription, Date checkTime, int delaySeconds)
49      {
50          Validate.notNull(hostName, "hostName must not be null");
51          Validate.notNull(serviceDescription,
52                  "serviceDescription must not be null");
53          this.hostName = hostName;
54          this.serviceDescription = serviceDescription;
55          this.checkTime = checkTime;
56          this.delaySeconds = delaySeconds;
57      }
58  
59      @Override
60      public ExternalCommand createCommand() {
61          Calendar checkTimeCal = Calendar.getInstance();
62          if (checkTime != null)
63              checkTimeCal.setTime(checkTime);
64          checkTimeCal.add(Calendar.SECOND, delaySeconds);
65          return new ScheduleServiceCheckCommand(
66                  hostName, serviceDescription, checkTimeCal.getTime());
67      }
68  
69  }