001/*
002 *  Copyright 2022 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 org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.configuration.DefaultConfiguration;
021
022import org.ametys.runtime.config.Config;
023
024/**
025 * This {@link Runnable} is launched if enabled in config (by the param defined by param-enabled) or always launched if there is no param 'param-enabled'.
026 */
027public class ConfigBasedRunnable extends StaticRunnable
028{
029
030    @Override
031    public void configure(Configuration configuration) throws ConfigurationException
032    {
033        DefaultConfiguration modifiedConfiguration = new DefaultConfiguration(configuration);
034    
035        if (isEnabled(configuration))
036        {
037            configureFireProcess(configuration, modifiedConfiguration);
038        }
039        else
040        {
041            // FireProcess = never
042            DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process");
043            fireProcessConfig.setValue(FireProcess.NEVER.name());
044            modifiedConfiguration.addChild(fireProcessConfig);
045        }
046        
047        // Call configure of StaticRunnable
048        super.configure(modifiedConfiguration);
049    }
050
051    /**
052     * Check if the Runnable should be enabled
053     * @param configuration the configuration
054     * @return true if the Runnable is enabled
055     */
056    protected boolean isEnabled(Configuration configuration)
057    {
058        Configuration enabledCfg = configuration.getChild("param-enabled", false);
059        if (enabledCfg != null)
060        {
061            return Config.getInstance().getValue(enabledCfg.getValue("").trim());
062        }
063        else
064        {
065            return true;
066        }
067    }
068    
069    /**
070     * Edit the runnable configuration based on the {@link Config}
071     * @param configuration the original configuration
072     * @param modifiedConfiguration the edited configuration
073     * @throws ConfigurationException if configuration is invalid
074     */
075    protected void configureFireProcess(Configuration configuration, DefaultConfiguration modifiedConfiguration) throws ConfigurationException
076    {
077        // StaticRunnable default implementation will look for a fire-process and cron child
078    }
079}