From eeb5e1afd44ec7b71b0232a75406962d9c23dbf6 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 14 Oct 2018 08:09:38 +0200 Subject: [PATCH] Microprofile Config support for Java SE Bootstrap API Signed-off-by: Markus KARG --- ext/microprofile-config-javase/pom.xml | 65 +++++++++++++++++++ .../MicroprofileConfigurator.java | 48 ++++++++++++++ ...g.glassfish.jersey.server.spi.Configurator | 1 + .../MicroprofileConfiguratorTest.java | 62 ++++++++++++++++++ ext/pom.xml | 1 + pom.xml | 1 + 6 files changed, 178 insertions(+) create mode 100644 ext/microprofile-config-javase/pom.xml create mode 100644 ext/microprofile-config-javase/src/main/java/org/glassfish/jersey/server/microprofile/MicroprofileConfigurator.java create mode 100644 ext/microprofile-config-javase/src/main/resources/META-INF/services/org.glassfish.jersey.server.spi.Configurator create mode 100644 ext/microprofile-config-javase/src/test/java/org/glassfish/jersey/server/microprofile/MicroprofileConfiguratorTest.java diff --git a/ext/microprofile-config-javase/pom.xml b/ext/microprofile-config-javase/pom.xml new file mode 100644 index 00000000000..f86fe47f5ab --- /dev/null +++ b/ext/microprofile-config-javase/pom.xml @@ -0,0 +1,65 @@ + + + + + 4.0.0 + + + org.glassfish.jersey.ext + project + 2.28-SNAPSHOT + + + jersey-microprofile-config-javase + jersey-microprofile-config-javase + + + Jersey extension module providing support for Microprofile Config integration on Java SE platforms. + + + + + javax.ws.rs + javax.ws.rs-api + + + + org.glassfish.jersey.core + jersey-server + ${project.version} + + + + org.eclipse.microprofile.config + microprofile-config-api + ${microprofile.config.version} + provided + + + + junit + junit + + + + org.mockito + mockito-all + + + + org.hamcrest + hamcrest-library + test + + + + diff --git a/ext/microprofile-config-javase/src/main/java/org/glassfish/jersey/server/microprofile/MicroprofileConfigurator.java b/ext/microprofile-config-javase/src/main/java/org/glassfish/jersey/server/microprofile/MicroprofileConfigurator.java new file mode 100644 index 00000000000..59e7a110a96 --- /dev/null +++ b/ext/microprofile-config-javase/src/main/java/org/glassfish/jersey/server/microprofile/MicroprofileConfigurator.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018 Markus KARG. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.microprofile; + +import javax.ws.rs.JAXRS.Configuration; +import javax.ws.rs.ProcessingException; + +import org.eclipse.microprofile.config.Config; +import org.glassfish.jersey.server.spi.Configurator; + +/** + * Configurator for Microprofile Config configurations. + * + * @author Markus KARG (markus@headcrashing.eu) + * @since 2.28 + */ +public class MicroprofileConfigurator implements Configurator { + + /** + * Configures the configuration builder according to the provided configuration. Just does nothing if the type of + * configuration is other than {@link Config}. + * + * @param configurationBuilder The configuration builder to configure. + * @param configuration The configuration to use for configuring the configuration builder. + */ + @Override + public void configure(final Configuration.Builder configurationBuilder, final Object configuration) + throws ProcessingException { + if (configuration instanceof Config) { + configurationBuilder.from(((Config) configuration)::getOptionalValue); + } + } + +} diff --git a/ext/microprofile-config-javase/src/main/resources/META-INF/services/org.glassfish.jersey.server.spi.Configurator b/ext/microprofile-config-javase/src/main/resources/META-INF/services/org.glassfish.jersey.server.spi.Configurator new file mode 100644 index 00000000000..9beae7afd4a --- /dev/null +++ b/ext/microprofile-config-javase/src/main/resources/META-INF/services/org.glassfish.jersey.server.spi.Configurator @@ -0,0 +1 @@ +org.glassfish.jersey.server.microprofile.MicroprofileConfigurator diff --git a/ext/microprofile-config-javase/src/test/java/org/glassfish/jersey/server/microprofile/MicroprofileConfiguratorTest.java b/ext/microprofile-config-javase/src/test/java/org/glassfish/jersey/server/microprofile/MicroprofileConfiguratorTest.java new file mode 100644 index 00000000000..3e62cef57aa --- /dev/null +++ b/ext/microprofile-config-javase/src/test/java/org/glassfish/jersey/server/microprofile/MicroprofileConfiguratorTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 Markus KARG. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.microprofile; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Optional; +import java.util.function.BiFunction; + +import javax.ws.rs.JAXRS; + +import org.eclipse.microprofile.config.Config; +import org.glassfish.jersey.server.spi.Configurator; +import org.junit.Test; + +/** + * Unit tests for {@link MicroprofileConfigurator}. + * + * @author Markus KARG (markus@headcrashing.eu) + * @since 2.28 + */ +public class MicroprofileConfiguratorTest { + + @Test + @SuppressWarnings("unchecked") + public final void shouldProvideGetOptionalValueAsPropertiesProviderToConfigurationBuilder() { + // given + final Configurator configurator = new MicroprofileConfigurator(); + final Config config = mock(Config.class); + final JAXRS.Configuration.Builder configurationBuilder = mock(JAXRS.Configuration.Builder.class); + given(configurationBuilder.from(any(BiFunction.class))).willAnswer(invocation -> { + final BiFunction, Optional> propertiesProvider = + invocation.getArgumentAt(0, BiFunction.class); + propertiesProvider.apply("NAME", String.class); + return invocation.getMock(); + }); + + // when + configurator.configure(configurationBuilder, config); + + // then + verify(config).getOptionalValue("NAME", String.class); + } + +} diff --git a/ext/pom.xml b/ext/pom.xml index 0e56abfa332..130e56aad23 100644 --- a/ext/pom.xml +++ b/ext/pom.xml @@ -54,6 +54,7 @@ servlet-portability spring4 wadl-doclet + microprofile-config-javase diff --git a/pom.xml b/pom.xml index 9399349deae..d85b7b749b6 100644 --- a/pom.xml +++ b/pom.xml @@ -2010,5 +2010,6 @@ 1.6 1.0.1 false + 1.3