001/*
002 *  Copyright 2016 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.runtime.plugins.admin.jvmstatus.monitoring.alerts;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023
024import org.ametys.runtime.config.Config;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts.AlertSampleManager.Threshold.Operator;
027import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample.AbstractSampleManager;
028
029/**
030 * AbstractAlertSampleManager gives you the infrastructure for easily
031 * deploying an {@link AlertSampleManager}.
032 * If the configuration mailBody is i18n, it can include two parameters :
033 * the first one is the current value, the second one is the threshold value
034 */
035public abstract class AbstractAlertSampleManager extends AbstractSampleManager implements AlertSampleManager
036{
037    /** The subject of the mail */
038    protected I18nizableText _subject;
039    /** The body of the mail */
040    protected I18nizableText _body;
041    
042    @Override
043    public void configure(Configuration configuration) throws ConfigurationException
044    {
045        super.configure(configuration);
046        
047        _subject = I18nizableText.parseI18nizableText(configuration.getChild("mailSubject"), "plugin." + _pluginName);
048        _body = I18nizableText.parseI18nizableText(configuration.getChild("mailBody"), "plugin." + _pluginName);
049    }
050    
051    @Override
052    public Map<String, Threshold> getThresholdValues()
053    {
054        if (Config.getInstance() == null)
055        {
056            return null;
057        }
058        
059        Map<String, Threshold> result = new HashMap<>();
060        Map<String, String> configNames = getThresholdConfigNames();
061        Map<String, Operator> operators = getOperators();
062        for (String datasourceName : configNames.keySet())
063        {
064            String configName = configNames.get(datasourceName);
065            Object value = _getTypedValue(configName);
066            result.put(datasourceName, new Threshold(operators.get(datasourceName), datasourceName, value, _subject, _body));
067        }
068        return result;
069    }
070    
071    private Object _getTypedValue(String configName)
072    {
073        String stringValue = Config.getInstance().getValueAsString(configName);
074        if (stringValue == null || "".equals(stringValue))
075        {
076            return null;
077        }
078        
079        Long longValue = Config.getInstance().getValueAsLong(configName);
080        if (longValue != null)
081        {
082            return longValue;
083        }
084        
085        Double doubleValue = Config.getInstance().getValueAsDouble(configName);
086        return doubleValue;
087    }
088    
089    /**
090     * Provides the configuration names for each datasource an alert is attached to.
091     * This method must return a map with the same keys as {@link #getOperators()}
092     * @return the configuration names for each datasource an alert is attached to.
093     */
094    protected abstract Map<String, String> getThresholdConfigNames();
095    
096    /**
097     * Provides the kind of operator for triggering the alert for each datasource an alert is attached to.
098     * This method must return a map with the same keys as {@link #getThresholdConfigNames()}
099     * @return the kind of operator for triggering the alert for each datasource an alert is attached to.
100     */
101    protected abstract Map<String, Operator> getOperators();
102}