Skip to content

Commit

Permalink
fixes eclipse#62 move seldom used parts away from ConfigProvider
Browse files Browse the repository at this point in the history
A power-user can use the ConfigProviderResolver directly to access the ConfigBuilder.
That way the casual user doesn't get hit with all the complexity he doesn't need most times.

Signed-off-by: Mark Struberg <[email protected]>
  • Loading branch information
struberg authored and OndroMih committed Mar 14, 2017
1 parent b5fb7c8 commit 493f66e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 101 deletions.
103 changes: 6 additions & 97 deletions api/src/main/java/org/eclipse/microprofile/config/ConfigProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.eclipse.microprofile.config;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

/**
* <p>
Expand All @@ -41,14 +39,14 @@
* </p>
*
* <p>
* A 'Configuration' consists of the information collected from the registered {@link ConfigSource ConfigSources}.
* These {@link ConfigSource ConfigSources} get sorted according to
* their <em>ordinal</em> defined via {@link ConfigSource#getOrdinal()}.
* A 'Configuration' consists of the information collected from the registered {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources}.
* These {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources} get sorted according to
* their <em>ordinal</em> defined via {@link org.eclipse.microprofile.config.spi.ConfigSource#getOrdinal()}.
* That way it is possible to overwrite configuration with lower importance from outside.
* </p>
*
* <p>
* It is also possible to register custom {@link ConfigSource ConfigSources} to flexibly
* It is also possible to register custom {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources} to flexibly
* extend the configuration mechanism. An example would be to pick up
* configuration values from a database table.</p>
*
Expand All @@ -73,7 +71,7 @@ private ConfigProvider() {
}

/**
* Provide a {@link Config} based on all {@link ConfigSource ConfigSources} of the
* Provide a {@link Config} based on all {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources} of the
* current Thread Context ClassLoader (TCCL)
* The {@link Config} will be stored for future retrieval.
* <p>
Expand All @@ -85,7 +83,7 @@ public static Config getConfig() {
}

/**
* Provide a {@link Config} based on all {@link ConfigSource ConfigSources} of the
* Provide a {@link Config} based on all {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources} of the
* specified ClassLoader
*
* <p>
Expand All @@ -96,93 +94,4 @@ public static Config getConfig(ClassLoader cl) {
return INSTANCE.getConfig(cl);
}

/**
* Create a fresh {@link ConfigBuilder} instance. This ConfigBuilder will
* initially contain no {@link ConfigSource} but with default {@link Converter Converters} and auto discovered
* {@link ConfigSource configsources} and {@link Converter converters}.
* Other undiscovered {@link ConfigSource} and {@link Converter Converters} will have to be added manually.
*
* The ConfigProvider will not manage the Config instance internally
*/
public static ConfigBuilder getBuilder() {
return INSTANCE.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 static void setConfig(Config config, ClassLoader classLoader) {
INSTANCE.setConfig(config, 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.
*/
public static void releaseConfig(Config config) {
INSTANCE.releaseConfig(config);
}

/**
* Builder for manually creating an instance of a {@code Config}.
*
* @see ConfigProvider#getBuilder()
*/
public interface ConfigBuilder {
/**
* Add the default config sources appearing on the builder's classpath
* including:
* <ol>
* <li>System properties</li>
* <li>Environment properties</li>
* <li>/META-INF/microprofile-config.properties</li>
* </ol>
*
* @return the ConfigBuilder with the default config sources
*/
ConfigBuilder addDefaultSources();

/**
* Return the ConfigBuilder for a given classloader
*
* @param loader
* @return the ConfigureBuilder for the given classloader
*/
ConfigBuilder forClassLoader(ClassLoader loader);

/**
* Add the specified {@link ConfigSource}.
*
* @param sources
* @return the ConfigBuilder with the configured sources
*/
ConfigBuilder withSources(ConfigSource... sources);

/**
* Add the specified {@link Converter}
*
* @param converters
* @return the ConfigBuilder with the added converters
*/
ConfigBuilder withConverters(Converter<?>... converters);

/**
* Build the {@link Config} object.
*
* @return the Config object
*/
Config build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*******************************************************************************
* Copyright (c) 2016-2017 Mark Struberg 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 org.eclipse.microprofile.config.spi;

import org.eclipse.microprofile.config.Config;

/**
* Builder for manually creating an instance of a {@code Config}.
*
* @see ConfigProviderResolver#getBuilder()
*
* @author <a href="mailto:[email protected]">Mark Struberg</a>
* @author <a href="mailto:[email protected]">Romain Manni-Bucau</a>
* @author <a href="mailto:[email protected]">Emily Jiang</a>
*/
public interface ConfigBuilder {
/**
* Add the default config sources appearing on the builder's classpath
* including:
* <ol>
* <li>System properties</li>
* <li>Environment properties</li>
* <li>/META-INF/microprofile-config.properties</li>
* </ol>
*
* @return the ConfigBuilder with the default config sources
*/
ConfigBuilder addDefaultSources();

/**
* Return the ConfigBuilder for a given classloader
*
* @param loader
* @return the ConfigureBuilder for the given classloader
*/
ConfigBuilder forClassLoader(ClassLoader loader);

/**
* Add the specified {@link ConfigSource}.
*
* @param sources
* @return the ConfigBuilder with the configured sources
*/
ConfigBuilder withSources(ConfigSource... sources);

/**
* Add the specified {@link Converter}
*
* @param converters
* @return the ConfigBuilder with the added converters
*/
ConfigBuilder withConverters(Converter<?>... converters);

/**
* Build the {@link Config} object.
*
* @return the Config object
*/
Config build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.ServiceLoader;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider.ConfigBuilder;

/**
* This class is not intended to be used by end-users but for
Expand Down Expand Up @@ -53,17 +52,35 @@ protected ConfigProviderResolver() {
public abstract Config getConfig(ClassLoader loader);

/**
* @see org.eclipse.microprofile.config.ConfigProvider#getBuilder()
* Create a fresh {@link ConfigBuilder} instance. This ConfigBuilder will
* initially contain no {@link ConfigSource} but with default {@link Converter Converters} and auto discovered
* {@link ConfigSource configsources} and {@link Converter converters}.
* Other undiscovered {@link ConfigSource} and {@link Converter Converters} will have to be added manually.
*
* The ConfigProvider will not manage the Config instance internally
*/
public abstract ConfigBuilder getBuilder();

/**
* @see org.eclipse.microprofile.config.ConfigProvider#setConfig(Config, ClassLoader)
* 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 setConfig(Config config, ClassLoader classLoader);

/**
* @see org.eclipse.microprofile.config.ConfigProvider#releaseConfig(Config)
* 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.
*/
public abstract void releaseConfig(Config config);

Expand Down Expand Up @@ -101,6 +118,7 @@ public ClassLoader run() {
return instance;
}


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

0 comments on commit 493f66e

Please sign in to comment.