-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MP Config implementation to correctly support mutable config sources.
- Loading branch information
1 parent
b842c91
commit 7d2f708
Showing
122 changed files
with
3,572 additions
and
1,818 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
common/service-loader/src/test/java/io/helidon/common/serviceloader/PrioritiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2020 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. | ||
* 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 io.helidon.common.serviceloader; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.annotation.Priority; | ||
|
||
import io.helidon.common.Prioritized; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class PrioritiesTest { | ||
@Test | ||
void testSort() { | ||
List<Service> services = new ArrayList<>(5); | ||
services.add(new DefaultPriority()); | ||
services.add(new VeryLowPriority()); | ||
services.add(new LowestPriority()); | ||
services.add(new HigherPriority()); | ||
services.add(new LowerPriority()); | ||
|
||
Priorities.sort(services, 100); | ||
|
||
validate(services); | ||
} | ||
|
||
@Test | ||
void testComparator() { | ||
List<Service> services = new ArrayList<>(5); | ||
// intentionally different order than in other methods, to make sure it is not working "by accident" | ||
services.add(new LowestPriority()); | ||
services.add(new LowerPriority()); | ||
services.add(new VeryLowPriority()); | ||
services.add(new HigherPriority()); | ||
services.add(new DefaultPriority()); | ||
|
||
services.sort(Priorities.priorityComparator(100)); | ||
|
||
validate(services); | ||
} | ||
|
||
private void validate(List<Service> services) { | ||
assertThat("There must be 5 services in the list", services.size(), is(5)); | ||
assertThat(services.get(0).getIt(), is(HigherPriority.IT)); | ||
assertThat(services.get(1).getIt(), is(LowerPriority.IT)); | ||
assertThat(services.get(2).getIt(), is(DefaultPriority.IT)); | ||
assertThat(services.get(3).getIt(), is(VeryLowPriority.IT)); | ||
assertThat(services.get(4).getIt(), is(LowestPriority.IT)); | ||
} | ||
|
||
private interface Service { | ||
String getIt(); | ||
} | ||
|
||
@Priority(1) | ||
private static class HigherPriority implements Service { | ||
private static final String IT = "higher"; | ||
|
||
@Override | ||
public String getIt() { | ||
return IT; | ||
} | ||
} | ||
|
||
@Priority(2) | ||
private static class LowerPriority implements Service { | ||
private static final String IT = "lower"; | ||
|
||
@Override | ||
public String getIt() { | ||
return IT; | ||
} | ||
} | ||
|
||
private static class DefaultPriority implements Service { | ||
private static final String IT = "default"; | ||
|
||
@Override | ||
public String getIt() { | ||
return IT; | ||
} | ||
} | ||
|
||
@Priority(101) | ||
private static class VeryLowPriority implements Service { | ||
private static final String IT = "veryLow"; | ||
|
||
@Override | ||
public String getIt() { | ||
return IT; | ||
} | ||
} | ||
|
||
private static class LowestPriority implements Service, Prioritized { | ||
private static final String IT = "lowest"; | ||
|
||
@Override | ||
public String getIt() { | ||
return IT; | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 1000; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2020 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. | ||
* 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 io.helidon.config.mp; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.helidon.config.ConfigSources; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
/** | ||
* Utilities for Helidon MicroProfile Config implementation. | ||
*/ | ||
public final class MpConfig { | ||
private MpConfig() { | ||
} | ||
|
||
/** | ||
* This method allows use to use Helidon Config on top of an MP config. | ||
* There is a limitation - the converters configured with MP config will not be available, unless | ||
* the implementation is coming from Helidon. | ||
* <p> | ||
* If you want to use the Helidon {@link io.helidon.config.Config} API instead of the MicroProfile | ||
* {@link org.eclipse.microprofile.config.Config} one, this method will create a Helidon config | ||
* instance that is based on the provided configuration instance. | ||
* | ||
* @param mpConfig MP Config instance | ||
* @return a new Helidon config using only the mpConfig as its config source | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static io.helidon.config.Config toHelidonConfig(Config mpConfig) { | ||
if (mpConfig instanceof io.helidon.config.Config) { | ||
return (io.helidon.config.Config) mpConfig; | ||
} | ||
|
||
io.helidon.config.Config.Builder builder = io.helidon.config.Config.builder() | ||
.disableEnvironmentVariablesSource() | ||
.disableSystemPropertiesSource() | ||
.disableMapperServices() | ||
.disableCaching() | ||
.disableParserServices() | ||
.disableFilterServices(); | ||
|
||
if (mpConfig instanceof MpConfigImpl) { | ||
((MpConfigImpl) mpConfig).converters() | ||
.forEach((clazz, converter) -> { | ||
Class<Object> cl = (Class<Object>) clazz; | ||
builder.addStringMapper(cl, converter::convert); | ||
}); | ||
} | ||
|
||
Map<String, String> allConfig = new HashMap<>(); | ||
mpConfig.getPropertyNames() | ||
.forEach(it -> { | ||
// covering the condition where a config key disappears between getting the property names and requesting | ||
// the value | ||
Optional<String> optionalValue = mpConfig.getOptionalValue(it, String.class); | ||
optionalValue.ifPresent(value -> allConfig.put(it, value)); | ||
}); | ||
|
||
return builder.addSource(ConfigSources.create(allConfig)) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.