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

Use application.properties to configure the application #1110

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.quarkus.deployment.recording.ObjectLoader;
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.configuration.ApplicationPropertiesConfigSource;
import io.quarkus.runtime.configuration.CidrAddressConverter;
import io.quarkus.runtime.configuration.ConverterFactory;
import io.quarkus.runtime.configuration.DefaultConfigSource;
Expand Down Expand Up @@ -180,6 +181,17 @@ private static <T> void withConverterHelper(final SmallRyeConfigBuilder builder,
}
}

/**
* Add a config sources for {@code application.properties}.
*/
@BuildStep
void setUpConfigFile(BuildProducer<RunTimeConfigurationSourceBuildItem> configSourceConsumer) {
configSourceConsumer.produce(new RunTimeConfigurationSourceBuildItem(
ApplicationPropertiesConfigSource.InJar.class.getName(), OptionalInt.empty()));
configSourceConsumer.produce(new RunTimeConfigurationSourceBuildItem(
ApplicationPropertiesConfigSource.InFileSystem.class.getName(), OptionalInt.empty()));
}

/**
* Write the default run time configuration.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.quarkus.runtime.configuration;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import io.smallrye.config.PropertiesConfigSource;

/**
* A configuration source for {@code application.properties}.
*/
public abstract class ApplicationPropertiesConfigSource extends PropertiesConfigSource {
private static final long serialVersionUID = -4694780118527396798L;

static final String APPLICATION_PROPERTIES = "application.properties";

ApplicationPropertiesConfigSource(InputStream is, int ordinal) {
super(readProperties(is), APPLICATION_PROPERTIES, ordinal);
}

private static Map<String, String> readProperties(final InputStream is) {
if (is == null) {
return Collections.emptyMap();
}
try (Closeable ignored = is) {
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8)) {
try (BufferedReader br = new BufferedReader(isr)) {
final Properties properties = new Properties();
properties.load(br);
return (Map<String, String>) (Map) properties;
}
}
} catch (IOException e) {
throw new IOError(e);
}
}

public static final class InJar extends ApplicationPropertiesConfigSource {
public InJar() {
super(openStream(), 250);
}

private static InputStream openStream() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = ApplicationPropertiesConfigSource.class.getClassLoader();
}
InputStream is;
if (cl == null) {
is = ClassLoader.getSystemResourceAsStream(APPLICATION_PROPERTIES);
} else {
is = cl.getResourceAsStream(APPLICATION_PROPERTIES);
}
return is;
}
}

public static final class InFileSystem extends ApplicationPropertiesConfigSource {
public InFileSystem() {
super(openStream(), 260);
}

private static InputStream openStream() {
final Path path = Paths.get("config", APPLICATION_PROPERTIES);
if (Files.exists(path)) {
try {
return Files.newInputStream(path);
} catch (NoSuchFileException | FileNotFoundException e) {
return null;
} catch (IOException e) {
throw new IOError(e);
}
} else {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.runtime.configuration;

import java.util.Arrays;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

/**
* This is a temporary hack until the class loader mess is worked out.
*/
public class TemporaryConfigSourceProvider implements ConfigSourceProvider {
public Iterable<ConfigSource> getConfigSources(final ClassLoader forClassLoader) {
return Arrays.asList(
new ApplicationPropertiesConfigSource.InJar(),
new ApplicationPropertiesConfigSource.InFileSystem());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.runtime.configuration.TemporaryConfigSourceProvider