org.eclipse.microprofile.config.spi.ConfigProviderResolver Maven / Gradle / Ivy
/*
*******************************************************************************
* Copyright (c) 2016-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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 org.eclipse.microprofile.config.spi;
import java.util.Iterator;
import java.util.ServiceLoader;
import org.eclipse.microprofile.config.Config;
/**
* This class is not intended to be used by end-users but for
* portable container integration purpose only.
*
* Service provider for ConfigProviderResolver. The implementation registers
* itself via the {@link java.util.ServiceLoader} mechanism.
*
* @author Mark Struberg
* @author Romain Manni-Bucau
* @author Emily Jiang
*/
public abstract class ConfigProviderResolver {
protected ConfigProviderResolver() {
}
private static volatile ConfigProviderResolver instance = null;
/**
* @see org.eclipse.microprofile.config.ConfigProvider#getConfig()
* @return config the config object for the Thread Context Classloader
*/
public abstract Config getConfig();
/**
* @see org.eclipse.microprofile.config.ConfigProvider#getConfig(ClassLoader)
* @param loader the classloader
* @return config the config object for the specified classloader
*/
public abstract Config getConfig(ClassLoader loader);
/**
* Create a fresh {@link ConfigBuilder} instance.
*
* This ConfigBuilder will initially contain no {@link ConfigSource}. The other {@link ConfigSource} will have
* to be added manually or discovered by calling {@link ConfigBuilder#addDiscoveredSources()}.
*
* This ConfigBuilder will initially contain default {@link Converter Converters}. Any other converters will need to
* be added manually.
*
* The ConfigProvider will not manage the Config instance internally
* @return a fresh ConfigBuilder
*/
public abstract ConfigBuilder getBuilder();
/**
* Register a given {@link Config} within the Application (or Module) identified by the given ClassLoader.
* If the ClassLoader is {@code null} then the current Application will be used.
*
* @param config
* which should get registered
* @param classLoader
* which identifies the Application or Module the given Config should get associated with.
*
* @throws IllegalStateException
* if there is already a Config registered within the Application.
* A user could explicitly use {@link #releaseConfig(Config)} for this case.
*/
public abstract void registerConfig(Config config, ClassLoader classLoader);
/**
* A {@link Config} normally gets released if the Application it is associated with gets destroyed.
* Invoke this method if you like to destroy the Config prematurely.
*
* If the given Config is associated within an Application then it will be unregistered.
* @param config the config to be released
*/
public abstract void releaseConfig(Config config);
/**
* Creates a ConfigProviderResolver object
* Only used internally from within {@link org.eclipse.microprofile.config.ConfigProvider}
* @return ConfigProviderResolver an instance of ConfigProviderResolver
*/
public static ConfigProviderResolver instance() {
if (instance == null) {
synchronized (ConfigProviderResolver.class) {
if (instance != null) {
return instance;
}
instance = loadSpi(ConfigProviderResolver.class.getClassLoader());
}
}
return instance;
}
private static ConfigProviderResolver loadSpi(ClassLoader cl) {
ServiceLoader sl = ServiceLoader.load(
ConfigProviderResolver.class, cl);
final Iterator iterator = sl.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
throw new IllegalStateException(
"No ConfigProviderResolver implementation found!");
}
/**
* 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;
}
}