<commit_before>/*******************************************************************************
 * Copyright (c) 2016 IBM Corp. and others
 *
 * 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.
 *
 *******************************************************************************/

package io.microprofile.config.spi;

import io.microprofile.config.Config;
import io.microprofile.config.ConfigProvider.ConfigBuilder;

import java.util.ServiceLoader;
import java.util.logging.Logger;

/**
 * Service provider for ConfigProviderResolver. The implementation registers
 * itself via {@link java.util.ServiceLoader} mechanism.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class ConfigProviderResolver {
    private static final ThreadLocal<ServiceLoader<ConfigProviderResolver>> THREAD_LOCAL = new ThreadLocal<ServiceLoader<ConfigProviderResolver>>() {
        @Override
        protected ServiceLoader<ConfigProviderResolver> initialValue() {
            return ServiceLoader.load(ConfigProviderResolver.class);
        }
    };

    protected ConfigProviderResolver() {
    }

    private static ConfigProviderResolver instance = null;

    public abstract Config getConfig();

    public abstract Config getConfig(ClassLoader loader);

    public abstract ConfigBuilder getEmptyBuilder();

    public abstract ConfigBuilder getBuilder();

    public abstract void releaseConfig(Config config);

    /**
     * Creates a ConfigProviderResolver object
     *
     * @return
     */
    public static ConfigProviderResolver instance() {
        if (instance == null) {
            ServiceLoader<ConfigProviderResolver> sl = THREAD_LOCAL.get();
            for (ConfigProviderResolver cpr : sl) {
                if (instance != null) {
                    Logger.getLogger(ConfigProviderResolver.class.getName()).warning(
                                    "Multiple ConfigProviderResolver found. Ignoring "
                                                    + cpr.getClass().getName());
                } else {
                    instance = cpr;
                }
            }
            if (instance == null) {
                throw new IllegalStateException("No ConfigProviderResolver implementation found!");
            }
        }
        return instance;

    }

    /**
     * Set the instance. It is used by OSGi environment while service loader
     * pattern is not supported.
     *
     * @param resolver
     *            set the instance.
     */
    public static void setInstance(ConfigProviderResolver resolver) {
        instance = resolver;
    }
}<commit_msg>refs #20 start top down with picking up a mp-config implementation.<commit_after>/*******************************************************************************
 * Copyright (c) 2016 IBM Corp. and others
 *
 * 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.
 *
 *******************************************************************************/

package io.microprofile.config.spi;

import io.microprofile.config.Config;
import io.microprofile.config.ConfigProvider.ConfigBuilder;

import java.util.ServiceLoader;

/**
 * Service provider for ConfigProviderResolver. The implementation registers
 * itself via {@link java.util.ServiceLoader} mechanism.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class ConfigProviderResolver {
    protected ConfigProviderResolver() {
    }

    private static volatile ConfigProviderResolver instance = null;

    public abstract Config getConfig();

    public abstract Config getConfig(ClassLoader loader);

    public abstract ConfigBuilder getEmptyBuilder();

    public abstract ConfigBuilder getBuilder();

    public abstract void releaseConfig(Config config);

    /**
     * Creates a ConfigProviderResolver object
     *
     * @return
     */
    public static ConfigProviderResolver instance() {
        if (instance == null) {
            synchronized (ConfigProviderResolver.class) {
            if (instance != null) {
                return instance;
            }
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            if (cl == null) {
                cl = ConfigProviderResolver.class.getClassLoader();
            }

            ConfigProviderResolver newInstance = loadSpi(cl);

            if (newInstance == null) {
                throw new IllegalStateException("No ConfigProviderResolver implementation found!");
            }

            instance = newInstance;
        }
    }

        return instance;
}

    private static ConfigProviderResolver loadSpi(ClassLoader cl) {
        if (cl == null) {
            return null;
        }

        // start from the root CL and go back down to the TCCL
        ConfigProviderResolver instance = loadSpi(cl.getParent());

        if (instance == null) {
            ServiceLoader<ConfigProviderResolver> sl = ServiceLoader.load(ConfigProviderResolver.class, cl);
            for (ConfigProviderResolver spi : sl) {
                if (instance != null) {
                    throw new IllegalStateException("Multiple ConfigResolverProvider implementations found: " + spi.getClass().getName()
                            + " and " + instance.getClass().getName());
                } else {
                    instance = spi;
                }
            }
        }
        return instance;
    }


    /**
     * Set the instance. It is used by OSGi environment while service loader
     * pattern is not supported.
     *
     * @param resolver
     *            set the instance.
     */
    public static void setInstance(ConfigProviderResolver resolver) {
        instance = resolver;
    }
}
{'fraction_non_alphanumeric': 0.1308084486525856, 'fraction_numerical': 0.002621995630007283, 'mean_word_length': 2.6854535695115405, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6541908', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1556, 'n_words': 516}