<commit_before>package com.psddev.dari.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/** Global map of settings. */
public final class Settings {

    /**
     * Sub-key used to identify the implementation class for
     * {@link #newInstance}.
     */
    public static final String CLASS_SUB_SETTING = "class";

    /** Key used to toggle debug mode. */
    public static final String DEBUG_SETTING = "DEBUG";

    /** Key used to indicate when running in a production environment. */
    public static final String PRODUCTION_SETTING = "PRODUCTION";

    /** Key used to specify a shared secret. */
    public static final String SECRET_SETTING = "SECRET";

    /** Default properties file that contains all settings. */
    public static final String SETTINGS_FILE = "/settings.properties";

    private static final String JNDI_PREFIX = "java:comp/env";
    private static final Logger LOGGER = LoggerFactory.getLogger(Settings.class);
    private static final ThreadLocal<Map<String, Object>> OVERRIDES = new ThreadLocal<Map<String, Object>>();
    private static final String RANDOM_SECRET = UUID.randomUUID().toString();

    private static final LoadingCache<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = CacheBuilder.
            newBuilder().
            build(new CacheLoader<Class<?>, Constructor<?>>() {

                @Override
                public Constructor<?> load(Class<?> instanceClass) throws NoSuchMethodException {
                    Constructor<?> constructor = instanceClass.getDeclaredConstructor();

                    constructor.setAccessible(true);
                    return constructor;
                }
            });

    private static final PullThroughValue<Map<String, Object>> SETTINGS = new PullThroughValue<Map<String, Object>>() {

        @Override
        protected Map<String, Object> produce() {
            return new PeriodicCache<String, Object>(0.0, 10.0) {

                @Override
                protected Map<String, Object> update() {

                    // Optional file.
                    Map<String, Object> settings = new TreeMap<String, Object>();
                    InputStream input = Settings.class.getResourceAsStream(SETTINGS_FILE);
                    if (input != null) {
                        try {
                            try {
                                Properties properties = new Properties();
                                properties.load(input);
                                putAllMap(settings, properties);
                            } finally {
                                input.close();
                            }
                        } catch (IOException ex) {
                            LOGGER.warn(String.format("Cannot read [%s] file!", SETTINGS_FILE), ex);
                        }
                    }

                    putAllMap(settings, System.getenv());
                    putAllMap(settings, System.getProperties());

                    // JNDI.
                    try {
                        putAllContext(settings, new InitialContext(), JNDI_PREFIX);
                    } catch (Throwable error) {
                        LOGGER.warn("Can't read from JNDI!", error);
                    }

                    return Collections.unmodifiableMap(settings);
                }

                private void putAllMap(Map<String, Object> map, Map<?, ?> other) {
                    for (Map.Entry<?, ?> entry : other.entrySet()) {
                        Object key = entry.getKey();
                        if (key != null) {
                            CollectionUtils.putByPath(map, key.toString(), entry.getValue());
                        }
                    }
                }

                private void putAllContext(Map<String, Object> map, Context context, String path) throws NamingException {
<commit_msg>Changes Settings not to emit the error stack trace on JNDI warning log as not to confuse the user into thinking that it's an error.<commit_after>package com.psddev.dari.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/** Global map of settings. */
public final class Settings {

    /**
     * Sub-key used to identify the implementation class for
     * {@link #newInstance}.
     */
    public static final String CLASS_SUB_SETTING = "class";

    /** Key used to toggle debug mode. */
    public static final String DEBUG_SETTING = "DEBUG";

    /** Key used to indicate when running in a production environment. */
    public static final String PRODUCTION_SETTING = "PRODUCTION";

    /** Key used to specify a shared secret. */
    public static final String SECRET_SETTING = "SECRET";

    /** Default properties file that contains all settings. */
    public static final String SETTINGS_FILE = "/settings.properties";

    private static final String JNDI_PREFIX = "java:comp/env";
    private static final Logger LOGGER = LoggerFactory.getLogger(Settings.class);
    private static final ThreadLocal<Map<String, Object>> OVERRIDES = new ThreadLocal<Map<String, Object>>();
    private static final String RANDOM_SECRET = UUID.randomUUID().toString();

    private static final LoadingCache<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = CacheBuilder.
            newBuilder().
            build(new CacheLoader<Class<?>, Constructor<?>>() {

                @Override
                public Constructor<?> load(Class<?> instanceClass) throws NoSuchMethodException {
                    Constructor<?> constructor = instanceClass.getDeclaredConstructor();

                    constructor.setAccessible(true);
                    return constructor;
                }
            });

    private static final PullThroughValue<Map<String, Object>> SETTINGS = new PullThroughValue<Map<String, Object>>() {

        @Override
        protected Map<String, Object> produce() {
            return new PeriodicCache<String, Object>(0.0, 10.0) {

                @Override
                protected Map<String, Object> update() {

                    // Optional file.
                    Map<String, Object> settings = new TreeMap<String, Object>();
                    InputStream input = Settings.class.getResourceAsStream(SETTINGS_FILE);
                    if (input != null) {
                        try {
                            try {
                                Properties properties = new Properties();
                                properties.load(input);
                                putAllMap(settings, properties);
                            } finally {
                                input.close();
                            }
                        } catch (IOException ex) {
                            LOGGER.warn(String.format("Cannot read [%s] file!", SETTINGS_FILE), ex);
                        }
                    }

                    putAllMap(settings, System.getenv());
                    putAllMap(settings, System.getProperties());

                    // JNDI.
                    try {
                        putAllContext(settings, new InitialContext(), JNDI_PREFIX);
                    } catch (Throwable error) {
                        LOGGER.info("Can't read from JNDI! [{}]", error.getMessage());
                    }

                    return Collections.unmodifiableMap(settings);
                }

                private void putAllMap(Map<String, Object> map, Map<?, ?> other) {
                    for (Map.Entry<?, ?> entry : other.entrySet()) {
                        Object key = entry.getKey();
                        if (key != null) {
                            CollectionUtils.putByPath(map, key.toString(), entry.getValue());
                        }
                    }
                }

                private void putAllContext(Map<String, Object> map, Context context, String path) throws NamingException {

{'fraction_non_alphanumeric': 0.09647796024200518, 'fraction_numerical': 0.001512532411408816, 'mean_word_length': 1.91650913673598, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491330', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2182, 'n_words': 592}