All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restheart.configuration.Utils Maven / Gradle / Ivy

There is a newer version: 8.1.4
Show newest version
/*-
 * ========================LICENSE_START=================================
 * restheart-commons
 * %%
 * Copyright (C) 2019 - 2024 SoftInstigate
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.restheart.configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.jxpath.JXPathContext;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.Option;
import io.undertow.UndertowOptions;
import io.undertow.Undertow.Builder;

/**
 *
 * @author Andrea Di Cesare {@literal }
 */
public class Utils {
    private static final Logger LOGGER = LoggerFactory.getLogger(Configuration.class);

    /**
     *
     * @param           return value
     * @param conf
     * @param key
     * @param defaultValue
     * @param silent
     * @return
     */
    public static  V getOrDefault(Configuration conf, final String key, final V defaultValue, boolean silent) {
        return getOrDefault(conf.toMap(), key, defaultValue, silent);
    }

    /**
     *
     * @param           return value
     * @param conf
     * @param key
     * @param defaultValue
     * @param silent
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  V getOrDefault(final Map conf, final String key, final V defaultValue, boolean silent) {
        if (conf == null || conf.get(key) == null) {
            // if default value is null there is no default value actually
            if (defaultValue != null && !silent) {
                LOGGER.warn("Parameter \"{}\" not specified in the configuration file, using its default value \"{}\"", key, defaultValue);
            }
            return defaultValue;
        }

        try {
            if (!silent) {
                LOGGER.trace("configuration paramenter \"{}\" set to \"{}\"", key, conf.get(key));
            }
            return (V) conf.get(key);
        } catch (Throwable t) {
            if (!silent) {
                LOGGER.warn("Wrong configuration parameter \"{}\": \"{}\", using its default value \"{}\"", key, conf.get(key), defaultValue);
            }
            return defaultValue;
        }
    }

    public static  V find(final Map conf, final String xpath, boolean silent) {
        return findOrDefault(conf, xpath, null, silent);
    }

    @SuppressWarnings("unchecked")
    public static  V findOrDefault(final Map conf, final String xpath, final V defaultValue, boolean silent) {
        var ctx = JXPathContext.newContext(conf);
        ctx.setLenient(true);

        try {
            V value = (V) ctx.getValue(xpath);

            if (value == null) {
                // if default value is null there is no default value actually
                if (defaultValue != null && !silent) {
                    LOGGER.warn("Parameter \"{}\" not specified in the configuration file, using its default value \"{}\"", xpath, defaultValue);
                }

                return defaultValue;
            } else {
                if (!silent) {
                    LOGGER.trace("configuration paramenter \"{}\" set to \"{}\"", xpath, value);
                }

                return value;
            }
        } catch (Throwable t) {
            if (!silent) {
                LOGGER.warn("Wrong configuration parameter \"{}\": \"{}\", using its default value \"{}\"", xpath, defaultValue);
            }
            return defaultValue;
        }
    }

    /**
     *
     * @param conf
     * @param key
     * @param defaultValue
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List> asListOfMaps(final Map conf, final String key, final List> defaultValue, boolean silent) {
        if (conf == null) {
            if (!silent) {
                LOGGER.trace("parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }

            return defaultValue;
        }

        var o = conf.get(key);

        if (o == null) {
            if (!silent) {
                LOGGER.trace("configuration parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        } else if (o instanceof List) {
            try {
                return (List>) o;
            } catch (Throwable t) {
                LOGGER.warn("wrong configuration parameter {}", key);
                return defaultValue;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong configuration parameter {}, expecting an array of objects", key, defaultValue);
            }
            return defaultValue;
        }
    }

    /**
     *
     * @param conf
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map> asMapOfMaps(final Map conf, final String key, final Map> defaultValue, boolean silent) {
        if (conf == null) {
            if (!silent) {
                LOGGER.trace("parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }

            return defaultValue;
        }

        var o = conf.get(key);

        if (o == null) {
            if (!silent) {
                LOGGER.trace("configuration parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        } else if (o instanceof Map) {
            try {
                return (Map>) o;
            } catch (Throwable t) {
                if (!silent) {
                    LOGGER.warn("wrong configuration parameter {}, expecting a map of maps, using its default value {}", key, defaultValue);
                }
                return defaultValue;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong configuration parameter {}, expecting a map of maps, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        }
    }

    /**
     *
     * @param conf
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map asMap(final Map conf, final String key, Map defaultValue, boolean silent) {
        if (conf == null) {
            if (!silent) {
                LOGGER.trace("parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }

            return null;
        }

        var o = conf.get(key);

        if (o == null) {
            if (!silent) {
                LOGGER.trace("configuration parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        } else if (o instanceof Map m) {
            try {
                return (Map) o;
            } catch (Throwable t) {
                if (!silent) {
                    LOGGER.warn("wrong configuration parameter {}, expecting a map, using its default value {}", key, defaultValue);
                }

                return null;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong configuration parameter {}, expecting a map, using its default value {}", key, defaultValue);
            }
            return null;
        }
    }

    public static int[] asArrayOfInts(final Map conf, final String key, final int[] defaultValue, boolean silent) {
        if (conf == null) {
            if (!silent) {
                LOGGER.trace("parameter {} not specified in the configuration file, using its default value {}", key, null);
            }

            return null;
        }

        var o = conf.get(key);

        if (o instanceof List l) {
            try {
                return l.stream().filter(i -> i instanceof Integer).mapToInt(i -> (Integer)i).toArray();
            } catch (Throwable t) {
                if (!silent) {
                    LOGGER.warn("wrong configuration parameter {}, expecting a list of ints", key);
                }

                return null;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong configuration parameter {}, expecting a list of ints", key);
            }
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public static List asListOfStrings(final Map conf, final String key, final List defaultValue, boolean silent) {
        if (conf == null || conf.get(key) == null) {
            // if default value is null there is no default value actually
            if (defaultValue != null && !silent) {
                LOGGER.trace("parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        } else if (conf.get(key) instanceof List) {
            if (!silent) {
                LOGGER.debug("paramenter {} set to {}", key, conf.get(key));
            }

            try {
                var ret = ((List) conf.get(key));

                if (ret.isEmpty()) {
                    if (!silent) {
                        LOGGER.warn("wrong value for parameter {}: {}, using its default value {}", key, conf.get(key), defaultValue);
                    }
                    return defaultValue;
                } else {
                    return ret;
                }
            } catch (Throwable t) {
                if (!silent) {
                    LOGGER.warn("wrong value for parameter {}: {}, using its default value {}", key, conf.get(key), defaultValue);
                }
                return defaultValue;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong value for parameter {}: {}, using its default value {}", key, conf.get(key), defaultValue);
            }
            return defaultValue;
        }
    }

    public static Boolean asBoolean(final Map conf, final String key, final Boolean defaultValue, boolean silent) {
        return getOrDefault(conf, key, defaultValue, silent);
    }

    public static String asString(final Map conf, final String key, final String defaultValue, boolean silent) {
        return getOrDefault(conf, key, defaultValue, silent);
    }

    public static Integer asInteger(final Map conf, final String key, final Integer defaultValue, boolean silent) {
        return getOrDefault(conf, key, defaultValue, silent);
    }

    public static Long asLong(final Map conf, final String key, final Long defaultValue, boolean silent) {
        if (conf == null || conf.get(key) == null) {
            // if default value is null there is no default value actually
            if (defaultValue != null && !silent) {
                LOGGER.debug("parameter {} not specified in the configuration file, using its default value {}", key, defaultValue);
            }
            return defaultValue;
        } else if (conf.get(key) instanceof Number) {
            if (!silent) {
                LOGGER.debug("paramenter {} set to {}", key, conf.get(key));
            }
            try {
                return Long.parseLong(conf.get(key).toString());
            } catch (NumberFormatException nfe) {
                if (!silent) {
                    LOGGER.warn("wrong value for parameter {}: {}, using its default value {}", key, conf.get(key), defaultValue);
                }
                return defaultValue;
            }
        } else {
            if (!silent) {
                LOGGER.warn("wrong value for parameter {}: {}, using its default value {}", key, conf.get(key), defaultValue);
            }
            return defaultValue;
        }
    }

    /**
     *
     * @param key
     * @return the environment or java property variable, if found
     */
    public static String valueFromEnv(final String confParameter) {
        return valueFromEnv(confParameter, false);
    }

    /**
     *
     * @param key
     * @return the environment or java property variable, if found
     */
    public static String valueFromEnv(final String confParameter, boolean silent) {
        var value = _valueFromEnv("RH", confParameter, silent);

        if (value != null) {
            return value;
        }

        return null;
    }

    private static String _valueFromEnv(final String prefix, final String confParameter, boolean silent) {
        var key = prefix != null ? prefix + "_" + confParameter.toUpperCase().replaceAll("-", "_")
                : confParameter.toUpperCase().replaceAll("-", "_");

        return __valueFromEnv(confParameter, key, silent);
    }

    private static String __valueFromEnv(final String confParameter, final String key, boolean silent) {
        var envValue = System.getProperty(key);

        if (envValue == null) {
            envValue = System.getenv(key);
        }

        if (!silent && envValue != null) {
            LOGGER.warn(">>> Found environment variable '{}': overriding parameter '{}' with value '{}'", key, confParameter, envValue);
        }

        return envValue;
    }

    @SuppressWarnings("rawtypes")
    private static final Set




© 2015 - 2024 Weber Informatics LLC | Privacy Policy