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 common Config in common configurable. #5015

Merged
merged 1 commit into from
Sep 30, 2022
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
59 changes: 59 additions & 0 deletions common/config/src/main/java/io/helidon/common/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* Immutable tree-structured configuration.
Expand Down Expand Up @@ -201,6 +202,17 @@ default Config get(Key key) {
*/
<T> ConfigValue<T> as(Class<T> type);

/**
* Typed value as a {@link ConfigValue} created from factory method.
* To convert from String, you can use
* {@link #asString() config.asString()}{@link ConfigValue#as(java.util.function.Function) .as(Function)}.
*
* @param mapper method to create an instance from config
* @param <T> type
* @return typed value
*/
<T> ConfigValue<T> map(Function<Config, T> mapper);

// shortcut methods

/**
Expand All @@ -221,6 +233,53 @@ default Config get(Key key) {
*/
ConfigValue<Map<String, String>> asMap() throws ConfigException;

/**
* String typed value.
*
* @return typed value
*/
default ConfigValue<String> asString() {
return as(String.class);
}

// shortcut methods

/**
* Boolean typed value.
*
* @return typed value
*/
default ConfigValue<Boolean> asBoolean() {
return as(Boolean.class);
}

/**
* Integer typed value.
*
* @return typed value
*/
default ConfigValue<Integer> asInt() {
return as(Integer.class);
}

/**
* Long typed value.
*
* @return typed value
*/
default ConfigValue<Long> asLong() {
return as(Long.class);
}

/**
* Double typed value.
*
* @return typed value
*/
default ConfigValue<Double> asDouble() {
return as(Double.class);
}

/**
* Object represents fully-qualified key of config node.
* <p>
Expand Down
28 changes: 20 additions & 8 deletions common/configurable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,15 @@
<artifactId>helidon-common-context</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
Expand All @@ -84,4 +78,22 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;

import io.helidon.config.Config;
import io.helidon.common.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

Expand Down Expand Up @@ -222,6 +222,9 @@ V directGet(K key) {
public static class Builder<K, V> implements io.helidon.common.Builder<Builder<K, V>, LruCache<K, V>> {
private int capacity = DEFAULT_CAPACITY;

private Builder() {
}

@Override
public LruCache<K, V> build() {
return new LruCache<>(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import java.nio.file.Path;
import java.util.Objects;

import io.helidon.config.Config;
import io.helidon.config.ConfigException;
import io.helidon.config.DeprecatedConfig;
import io.helidon.common.config.Config;
import io.helidon.common.config.ConfigException;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

Expand Down Expand Up @@ -152,7 +151,7 @@ static Resource create(String description, InputStream inputStream) {
* @param resourceConfig configuration current node must be the node containing the location of the resource, by
* convention in helidon, this should be on key named {@code resource}
* @return a resource ready to load from one of the locations
* @throws io.helidon.config.ConfigException in case this config does not define a resource configuration
* @throws io.helidon.common.config.ConfigException in case this config does not define a resource configuration
*/
@ConfiguredOption(key = "resource-path", description = "Classpath location of the resource.")
@ConfiguredOption(key = "path", description = "File system path to the resource.")
Expand All @@ -167,7 +166,7 @@ static Resource create(String description, InputStream inputStream) {
static Resource create(Config resourceConfig) {
return ResourceUtil.fromConfigPath(resourceConfig.get("path"))
.or(() -> ResourceUtil.fromConfigResourcePath(resourceConfig.get("resource-path")))
.or(() -> ResourceUtil.fromConfigUrl(DeprecatedConfig.get(resourceConfig, "uri", "url")))
.or(() -> ResourceUtil.fromConfigUrl(resourceConfig.get("uri")))
.or(() -> ResourceUtil.fromConfigContent(resourceConfig.get("content-plain")))
.or(() -> ResourceUtil.fromConfigB64Content(resourceConfig.get("content")))
.orElseThrow(() -> new ConfigException("Config is not a resource configuration on key: " + resourceConfig.key()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import io.helidon.config.Config;
import io.helidon.common.config.Config;

/**
* Utilities to move private static methods from interface,
* as javadoc fails when using source 8.
*/
final class ResourceUtil {
private static final int DEFAULT_PROXY_PORT = 80;
private static final Set<String> LOGGED_RESOURCES = new HashSet<>();
private static final System.Logger LOGGER = System.getLogger(ResourceUtil.class.getName());

private ResourceUtil() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.config.Config;
import io.helidon.common.context.Contexts;
import io.helidon.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

import io.helidon.config.Config;
import io.helidon.common.config.Config;

/**
* Supplier of a custom thread pool with defaults appropriate for a thread-per-request server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ public List<Runnable> shutdownNow() {
public static class RejectionHandler implements RejectedExecutionHandler {
private final AtomicInteger rejections = new AtomicInteger();

/**
* Creates a new rejection handler that counts rejections when attempting to offer rejected tasks to
* thread pool executor.
*/
protected RejectionHandler() {
}

@Override
public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.config.Config;
import io.helidon.common.context.Contexts;
import io.helidon.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

Expand Down Expand Up @@ -432,14 +432,10 @@ public Builder config(Config config) {
warnExperimental("growth-rate");
growthRate(value);
});
config.get("virtual-threads").asBoolean().ifPresent(value -> {
warnExperimental("virtual-threads");
virtualIfAvailable(value);
});
config.get("virtual-enforced").asBoolean().ifPresent(value -> {
warnExperimental("virtual-enforced");
virtualEnforced(value);
});

config.get("virtual-threads").asBoolean().ifPresent(this::virtualThreads);
config.get("virtual-enforced").asBoolean().ifPresent(this::virtualEnforced);

return this;
}

Expand All @@ -455,10 +451,14 @@ private void warnExperimental(String key) {
* @param enforceVirtualThreads whether to enforce virtual threads, defaults to {@code false}
* @return updated builder instance
* @see #virtualIfAvailable(boolean)
* @deprecated use {@link #virtualThreads(boolean)}
*/
@ConfiguredOption(value = "false", experimental = true)
@ConfiguredOption(value = "false", deprecated = true)
@Deprecated(forRemoval = true, since = "4.0.0")
public Builder virtualEnforced(boolean enforceVirtualThreads) {
this.virtualThreadsEnforced = enforceVirtualThreads;
if (enforceVirtualThreads) {
return virtualThreads(true);
}
return this;
}

Expand All @@ -471,11 +471,26 @@ public Builder virtualEnforced(boolean enforceVirtualThreads) {
*
* @param useVirtualThreads whether to use virtual threads or not, defaults to {@code false}
* @return updated builder instance
* @deprecated use {@link #virtualThreads(boolean)}
*/
@ConfiguredOption(key = "virtual-threads", value = "false", experimental = true)
@Deprecated(forRemoval = true, since = "4.0.0")
public Builder virtualIfAvailable(boolean useVirtualThreads) {
this.useVirtualThreads = useVirtualThreads;
return this;
}

/**
* When configured to {@code true}, an unbounded virtual executor service (project Loom) will be used.
* <p>
* If enabled, all other configuration options of this executor service are ignored!
*
* @param useVirtualThreads whether to use virtual threads or not, defaults to {@code false}
* @return updated builder instance
*/
@ConfiguredOption(value = "false")
public Builder virtualThreads(boolean useVirtualThreads) {
this.useVirtualThreads = useVirtualThreads;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/

/**
* General utilities that use {@link io.helidon.config.Config}.
* General utilities that use {@link io.helidon.common.config.Config}.
*
* @see io.helidon.common.configurable.Resource
*/
Expand Down
4 changes: 2 additions & 2 deletions common/configurable/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
module io.helidon.common.configurable {
requires java.management;
requires transitive io.helidon.config;
requires io.helidon.common;
requires transitive io.helidon.common.config;
requires transitive io.helidon.common;
requires io.helidon.common.context;
requires static io.helidon.config.metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ void testConfigClasPath() {
assertThat(resource.string().trim(), is(COPYRIGHT_TEXT));
}

@Test
void testConfigUrl() {
Resource resource = config.get("test-3.resource").as(Resource::create).get();
assertThat(resource.string().trim(), is(COPYRIGHT_TEXT));
}

@Test
void testConfigPlainContent() {
Resource resource = config.get("test-4.resource").as(Resource::create).get();
Expand Down
1 change: 0 additions & 1 deletion common/configurable/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ unit:
resources:
test-1.resource.path: "src/test/resources/sample.txt"
test-2.resource.resource-path: "sample.txt"
test-3.resource.url: "file:./src/test/resources/sample.txt"
test-4.resource.content-plain: "content"
test-5.resource.content: "YWJjZGVmZ8SNxZnFvsO6xa8="
test-6.reprosuce: "nothing"
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import java.util.Objects;
import java.util.Optional;

import io.helidon.common.config.Config;
import io.helidon.common.configurable.Resource;
import io.helidon.common.configurable.ResourceException;
import io.helidon.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

Expand Down Expand Up @@ -593,7 +593,7 @@ public KeystoreBuilder config(Config config) {
Config keystoreConfig = config.get("keystore");

// the actual resource (file, classpath) with the bytes of the keystore
keystoreConfig.get("resource").as(Resource::create).ifPresent(this::keystore);
keystoreConfig.get("resource").map(Resource::create).ifPresent(this::keystore);

// type of keystore
keystoreConfig.get("type")
Expand Down Expand Up @@ -779,10 +779,10 @@ private Builder updateBuilder(Builder builder) {
*/
public PemBuilder config(Config config) {
Config pemConfig = config.get("pem");
pemConfig.get("key.resource").as(Resource::create).ifPresent(this::key);
pemConfig.get("key.resource").map(Resource::create).ifPresent(this::key);
pemConfig.get("key.passphrase").asString().map(String::toCharArray).ifPresent(this::keyPassphrase);
pemConfig.get("cert-chain.resource").as(Resource::create).ifPresent(this::certChain);
pemConfig.get("certificates.resource").as(Resource::create).ifPresent(this::certificates);
pemConfig.get("cert-chain.resource").map(Resource::create).ifPresent(this::certChain);
pemConfig.get("certificates.resource").map(Resource::create).ifPresent(this::certificates);
return this;
}
}
Expand Down
Loading