From ddfd8e2f0e41ca682136512f283273ed4714e325 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 23 Sep 2018 17:03:19 +0200 Subject: [PATCH] Support for external configuration mechanics (Microprofile Config) --- bundles/jaxrs-ri/pom.xml | 6 ++ core-server/pom.xml | 7 ++ .../server/internal/RuntimeDelegateImpl.java | 14 ++++ .../internal/RuntimeDelegateImplTest.java | 71 +++++++++++++++++++ pom.xml | 1 + 5 files changed, 99 insertions(+) diff --git a/bundles/jaxrs-ri/pom.xml b/bundles/jaxrs-ri/pom.xml index c78f2a78ecd..96e40b8541b 100644 --- a/bundles/jaxrs-ri/pom.xml +++ b/bundles/jaxrs-ri/pom.xml @@ -191,6 +191,12 @@ persistence-api provided + + org.eclipse.microprofile.config + microprofile-config-api + ${microprofile.config.version} + provided + diff --git a/core-server/pom.xml b/core-server/pom.xml index 56294c5a5ad..a3ba3d36c85 100644 --- a/core-server/pom.xml +++ b/core-server/pom.xml @@ -227,6 +227,13 @@ ${project.version} test + + + org.eclipse.microprofile.config + microprofile-config-api + ${microprofile.config.version} + provided + diff --git a/core-server/src/main/java/org/glassfish/jersey/server/internal/RuntimeDelegateImpl.java b/core-server/src/main/java/org/glassfish/jersey/server/internal/RuntimeDelegateImpl.java index 2ac42c4be61..6a7634c191b 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/internal/RuntimeDelegateImpl.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/internal/RuntimeDelegateImpl.java @@ -33,6 +33,7 @@ import javax.ws.rs.JAXRS.Configuration.SSLClientAuthentication; import javax.ws.rs.core.Application; +import org.eclipse.microprofile.config.Config; import org.glassfish.jersey.internal.AbstractRuntimeDelegate; import org.glassfish.jersey.message.internal.MessagingBinders; import org.glassfish.jersey.server.ContainerFactory; @@ -116,6 +117,19 @@ public final Builder from(final BiFunction, Optional> co return this; } + @Override + public final Builder from(final Object externalConfig) { + if (externalConfig instanceof Config) { + return this.from((Config) externalConfig); + } + + return this; + } + + private final Builder from(final Config config) { + return this.from(config::getOptionalValue); + } + @Override public final JAXRS.Configuration build() { return this.properties::get; diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java index 4f212a06873..f8b9076cf5b 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java @@ -41,6 +41,8 @@ import javax.ws.rs.core.Application; import javax.ws.rs.ext.RuntimeDelegate; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigSource; import org.glassfish.jersey.internal.ServiceFinder; import org.glassfish.jersey.internal.ServiceFinder.ServiceIteratorProvider; import org.glassfish.jersey.server.ApplicationHandler; @@ -228,6 +230,75 @@ public final void shouldBuildCustomConfigurationFromPropertiesProvider() { assertThat(configuration.property(ServerProperties.AUTO_START), is(FALSE)); } + @Test + public final void shouldBuildCustomConfigurationFromMicroprofileConfig() { + // given + final JAXRS.Configuration.Builder configurationBuilder = new RuntimeDelegateImpl().createConfigurationBuilder(); + final SSLContext mockSslContext = new SSLContext(null, null, null) { + }; + final Class mockServerClass = Server.class; + final Config config = new Config() { + @Override + public final T getValue(final String propertyName, final Class propertyType) { + return null; + } + + @Override + public final Optional getOptionalValue(final String propertyName, final Class propertyType) { + if (JAXRS.Configuration.PROTOCOL.equals(propertyName) && String.class.equals(propertyType)) { + return Optional.of(propertyType.cast("HTTPS")); + } + if (JAXRS.Configuration.HOST.equals(propertyName) && String.class.equals(propertyType)) { + return Optional.of(propertyType.cast("hostname")); + } + if (JAXRS.Configuration.PORT.equals(propertyName) && Integer.class.equals(propertyType)) { + return Optional.of(propertyType.cast(8080)); + } + if (JAXRS.Configuration.ROOT_PATH.equals(propertyName) && String.class.equals(propertyType)) { + return Optional.of(propertyType.cast("path")); + } + if (JAXRS.Configuration.SSL_CLIENT_AUTHENTICATION.equals(propertyName) + && JAXRS.Configuration.SSLClientAuthentication.class.equals(propertyType)) { + return Optional.of(propertyType.cast(JAXRS.Configuration.SSLClientAuthentication.OPTIONAL)); + } + if (JAXRS.Configuration.SSL_CONTEXT.equals(propertyName) && SSLContext.class.equals(propertyType)) { + return Optional.of(propertyType.cast(mockSslContext)); + } + if (ServerProperties.HTTP_SERVER_CLASS.equals(propertyName) && Class.class.equals(propertyType)) { + return Optional.of(propertyType.cast(mockServerClass)); + } + if (ServerProperties.AUTO_START.equals(propertyName) && Boolean.class.equals(propertyType)) { + return Optional.of(propertyType.cast(FALSE)); + } + return Optional.empty(); + } + + @Override + public final Iterable getPropertyNames() { + return null; + } + + @Override + public final Iterable getConfigSources() { + return null; + } + }; + + // when + final JAXRS.Configuration configuration = configurationBuilder.from(config).build(); + + // then + assertThat(configuration, is(notNullValue())); + assertThat(configuration.protocol(), is("HTTPS")); + assertThat(configuration.host(), is("hostname")); + assertThat(configuration.port(), is(8080)); + assertThat(configuration.rootPath(), is("path")); + assertThat(configuration.sslClientAuthentication(), is(JAXRS.Configuration.SSLClientAuthentication.OPTIONAL)); + assertThat(configuration.sslContext(), is(theInstance(mockSslContext))); + assertThat(configuration.property(ServerProperties.HTTP_SERVER_CLASS), is(theInstance(mockServerClass))); + assertThat(configuration.property(ServerProperties.AUTO_START), is(FALSE)); + } + @Test public final void shouldBootstrapApplication() throws InterruptedException, ExecutionException, TimeoutException { // given 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