001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.core.impl.schedule; 017 018import java.time.LocalTime; 019 020import org.apache.avalon.framework.configuration.Configuration; 021import org.apache.avalon.framework.configuration.ConfigurationException; 022import org.apache.avalon.framework.configuration.DefaultConfiguration; 023 024import org.ametys.runtime.config.Config; 025 026/** 027 * A {@link Runnable} is launched if enabled in config (by the param defined by param-enabled) 028 * Will run every day at the specified hour in config (by the param defined by param-hour) 029 */ 030public class ConfigBasedDailyRunnable extends StaticRunnable 031{ 032 @Override 033 public void configure(Configuration configuration) throws ConfigurationException 034 { 035 DefaultConfiguration modifiedConfiguration = new DefaultConfiguration(configuration); 036 037 String enabledParamName = configuration.getChild("param-enabled").getValue("").trim(); 038 boolean enabled = Config.getInstance().getValue(enabledParamName); 039 if (enabled) 040 { 041 // FireProcess = cron 042 DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process"); 043 fireProcessConfig.setValue("cron"); 044 modifiedConfiguration.addChild(fireProcessConfig); 045 046 // Set our custom cron 047 String hourParamName = configuration.getChild("param-hour").getValue("").trim(); 048 String hourCfg = Config.getInstance().getValue(hourParamName); 049 LocalTime lt = LocalTime.parse(hourCfg); 050 String cronExpression = "0 " /*seconds*/ + lt.getMinute() /*minutes*/ + " " + lt.getHour() /*hour*/ + " * * ? *" /*every day, on every month, without restriction on day-of-week, on every year*/; 051 DefaultConfiguration cronConfig = new DefaultConfiguration("cron"); 052 cronConfig.setValue(cronExpression); 053 modifiedConfiguration.addChild(cronConfig); 054 } 055 else 056 { 057 // FireProcess = never 058 DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process"); 059 fireProcessConfig.setValue("never"); 060 modifiedConfiguration.addChild(fireProcessConfig); 061 } 062 063 // Call configure of StaticRunnable 064 super.configure(modifiedConfiguration); 065 } 066}