View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.kernel.scanner;
23  
24  import org.apache.commons.lang3.BooleanUtils;
25  import org.apache.commons.lang3.StringUtils;
26  import org.osgi.service.cm.ConfigurationException;
27  import org.quartz.CronExpression;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.util.Dictionary;
32  
33  public abstract class AbstractBufferScanner extends AbstractScanner {
34    private static final Logger logger = LoggerFactory.getLogger(AbstractBufferScanner.class);
35  
36    /**
37     * The key that will be used to find the amount of buffer time to search for scheduled recordings to remove
38     * before now.
39     */
40    protected static final String PARAM_KEY_BUFFER = "buffer";
41  
42    protected long buffer = -1;
43  
44    public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
45      String cronExpression;
46      boolean enabled;
47  
48      unschedule();
49  
50      if (properties != null) {
51        logger.debug("Updating configuration...");
52  
53        enabled = BooleanUtils.toBoolean((String) properties.get(PARAM_KEY_ENABLED));
54        setEnabled(enabled);
55        logger.debug("enabled = " + enabled);
56  
57        cronExpression = (String) properties.get(PARAM_KEY_CRON_EXPR);
58        if (StringUtils.isBlank(cronExpression) || !CronExpression.isValidExpression(cronExpression)) {
59          throw new ConfigurationException(PARAM_KEY_CRON_EXPR, "Cron expression must be valid");
60        }
61        setCronExpression(cronExpression);
62        logger.debug("cronExpression = '" + cronExpression + "'");
63  
64        try {
65          buffer = Long.valueOf((String) properties.get(PARAM_KEY_BUFFER));
66          if (buffer < 0) {
67            throw new ConfigurationException(PARAM_KEY_BUFFER, "Buffer must be 0 or greater");
68          }
69        } catch (NumberFormatException e) {
70          throw new ConfigurationException(PARAM_KEY_BUFFER, "Buffer must be a valid integer", e);
71        }
72        logger.debug("buffer = " + buffer);
73      }
74  
75      schedule();
76    }
77  
78    public long getBuffer() {
79      return buffer;
80    }
81  
82    public void setBuffer(long buffer) {
83      this.buffer = buffer;
84    }
85  }