Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding simple ConfigFilesProvider impl. #234

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import org.cfg4j.source.context.filesprovider.ConfigFilesProvider;
import org.cfg4j.source.context.filesprovider.DefaultConfigFilesProvider;
import org.cfg4j.source.context.filesprovider.SimpleConfigFilesProvider;
import org.cfg4j.source.context.propertiesprovider.JsonBasedPropertiesProvider;
import org.cfg4j.source.context.propertiesprovider.PropertiesProvider;
import org.cfg4j.source.context.propertiesprovider.PropertiesProviderSelector;
Expand Down Expand Up @@ -56,14 +58,27 @@ public class ClasspathConfigurationSource implements ConfigurationSource {
* calls (see corresponding javadoc for detail).
*/
public ClasspathConfigurationSource() {
this(new ConfigFilesProvider() {
@Override
public Iterable<Path> getConfigFiles() {
return Collections.singletonList(
Paths.get("application.properties")
);
}
});
this(new DefaultConfigFilesProvider());
}

/**
* Construct {@link ConfigurationSource} backed by classpath files. Uses the provided path strings to locate the
* needed files within the {@link Environment} provided to {@link #getConfiguration(Environment)} calls (see
* corresponding javadoc for detail).
* @param paths The paths to read from.
*/
public ClasspathConfigurationSource(String... paths) {
this(new SimpleConfigFilesProvider(paths));
}

/**
* Construct {@link ConfigurationSource} backed by classpath files. Uses the provided paths to locate the needed
* files within the {@link Environment} provided to {@link #getConfiguration(Environment)} calls (see corresponding
* javadoc for detail).
* @param paths The paths to read from.
*/
public ClasspathConfigurationSource(Path... paths) {
this(new SimpleConfigFilesProvider(paths));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.cfg4j.source.context.filesprovider;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* A simple provider implementation that
*/
public class SimpleConfigFilesProvider implements ConfigFilesProvider {

private final List<Path> paths;

public SimpleConfigFilesProvider(String... files) {
paths = new ArrayList<>(files.length);
for (String file : files) {
paths.add(Paths.get(file));
}
}

public SimpleConfigFilesProvider(Path... paths) {
this.paths = Arrays.asList(paths);
}

@Override
public Iterable<Path> getConfigFiles() {
return paths;
}

@Override
public String toString() {
return "SimpleConfigFilesProvider{" +
"paths=" + paths +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import org.cfg4j.source.context.filesprovider.ConfigFilesProvider;
import org.cfg4j.source.context.filesprovider.DefaultConfigFilesProvider;
import org.cfg4j.source.context.filesprovider.SimpleConfigFilesProvider;
import org.cfg4j.source.context.propertiesprovider.JsonBasedPropertiesProvider;
import org.cfg4j.source.context.propertiesprovider.PropertiesProvider;
import org.cfg4j.source.context.propertiesprovider.PropertiesProviderSelector;
Expand Down Expand Up @@ -51,14 +53,27 @@ public class FilesConfigurationSource implements ConfigurationSource {
* calls (see corresponding javadoc for detail).
*/
public FilesConfigurationSource() {
this(new ConfigFilesProvider() {
@Override
public Iterable<Path> getConfigFiles() {
return Collections.singletonList(
Paths.get("application.properties")
);
}
});
this(new DefaultConfigFilesProvider());
}

/**
* Construct {@link ConfigurationSource} backed by files. Uses the provided path strings to locate the needed files
* within the {@link Environment} provided to {@link #getConfiguration(Environment)} calls (see corresponding javadoc
* for detail).
* @param paths The paths to read from.
*/
public FilesConfigurationSource(String... paths) {
this(new SimpleConfigFilesProvider(paths));
}

/**
* Construct {@link ConfigurationSource} backed by files. Uses the provided paths to locate the needed files within
* the {@link Environment} provided to {@link #getConfiguration(Environment)} calls (see corresponding javadoc for
* detail).
* @param paths The paths to read from.
*/
public FilesConfigurationSource(Path... paths) {
this(new SimpleConfigFilesProvider(paths));
}

/**
Expand Down