001/*
002 *  Copyright 2017 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.cms.indexing;
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.plugins.core.impl.schedule.StaticRunnable;
025import org.ametys.runtime.config.Config;
026
027/**
028 * A {@link Runnable} for reloading Solr ACL cache if enabled in config (by cms.solr.reload.cache.scheduler.enabled)
029 * Will run every day at the specified hour in config (by cms.solr.reload.cache.scheduler.hour)
030 */
031public class ReloadSolrAclCacheDailyRunnable extends StaticRunnable
032{
033    @Override
034    public void configure(Configuration configuration) throws ConfigurationException
035    {
036        DefaultConfiguration modifiedConfiguration = new DefaultConfiguration(configuration);
037        
038        if (Config.getInstance().getValueAsBoolean("cms.solr.reload.cache.scheduler.enabled"))
039        {
040            // FireProcess = cron
041            DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process");
042            fireProcessConfig.setValue("cron");
043            modifiedConfiguration.addChild(fireProcessConfig);
044            
045            // Set our custom cron
046            String hourCfg = Config.getInstance().getValueAsString("cms.solr.reload.cache.scheduler.hour");
047            LocalTime lt = LocalTime.parse(hourCfg);
048            String cronExpression = "0 " /*seconds*/ + lt.getMinute() /*minutes*/ + " " + lt.getHour() /*hour*/ + " * * ? *" /*every day, on every month, without restriction on day-of-week, on every year*/;
049            DefaultConfiguration cronConfig = new DefaultConfiguration("cron");
050            cronConfig.setValue(cronExpression);
051            modifiedConfiguration.addChild(cronConfig);
052        }
053        else
054        {
055            // FireProcess = never
056            DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process");
057            fireProcessConfig.setValue("never");
058            modifiedConfiguration.addChild(fireProcessConfig);
059        }
060        
061        // Call configure of StaticRunnable
062        super.configure(modifiedConfiguration);
063    }
064}