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    /** The key that will be used to find the amount of buffer time to search for scheduled recordings to remove before now. */
37    protected static final String PARAM_KEY_BUFFER = "buffer";
38  
39    protected long buffer = -1;
40  
41    public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
42      String cronExpression;
43      boolean enabled;
44  
45      unschedule();
46  
47      if (properties != null) {
48        logger.debug("Updating configuration...");
49  
50        enabled = BooleanUtils.toBoolean((String) properties.get(PARAM_KEY_ENABLED));
51        setEnabled(enabled);
52        logger.debug("enabled = " + enabled);
53  
54        cronExpression = (String) properties.get(PARAM_KEY_CRON_EXPR);
55        if (StringUtils.isBlank(cronExpression) || !CronExpression.isValidExpression(cronExpression))
56          throw new ConfigurationException(PARAM_KEY_CRON_EXPR, "Cron expression must be valid");
57        setCronExpression(cronExpression);
58        logger.debug("cronExpression = '" + cronExpression + "'");
59  
60        try {
61          buffer = Long.valueOf((String) properties.get(PARAM_KEY_BUFFER));
62          if (buffer < 0) {
63            throw new ConfigurationException(PARAM_KEY_BUFFER, "Buffer must be 0 or greater");
64          }
65        } catch (NumberFormatException e) {
66          throw new ConfigurationException(PARAM_KEY_BUFFER, "Buffer must be a valid integer", e);
67        }
68        logger.debug("buffer = " + buffer);
69      }
70  
71      schedule();
72    }
73  
74    public long getBuffer() {
75      return buffer;
76    }
77  
78    public void setBuffer(long buffer) {
79      this.buffer = buffer;
80    }
81  }