This is an automated email from the ASF dual-hosted git repository. johndament pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cxf.git
commit e11cc25f1d8b638ab4926503e6faa8dd91b59f99 Author: Andy McCright <[email protected]> AuthorDate: Tue Dec 19 21:39:24 2017 -0600 ConfigFacade to handle cases where no MP Config exists --- .../client/MicroProfileClientConfigurableImpl.java | 16 ++----- .../microprofile/client/cdi/RestClientBean.java | 17 ++++--- .../microprofile/client/config/ConfigFacade.java | 53 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/MicroProfileClientConfigurableImpl.java b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/MicroProfileClientConfigurableImpl.java index b27c47e..97e8c7d 100644 --- a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/MicroProfileClientConfigurableImpl.java +++ b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/MicroProfileClientConfigurableImpl.java @@ -27,10 +27,10 @@ import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.ReaderInterceptor; import javax.ws.rs.ext.WriterInterceptor; + import org.apache.cxf.jaxrs.impl.ConfigurableImpl; import org.apache.cxf.jaxrs.impl.ConfigurationImpl; -import org.eclipse.microprofile.config.Config; -import org.eclipse.microprofile.config.ConfigProvider; +import org.apache.cxf.microprofile.client.config.ConfigFacade; import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; public class MicroProfileClientConfigurableImpl<C extends Configurable<C>> @@ -55,16 +55,8 @@ public class MicroProfileClientConfigurableImpl<C extends Configurable<C>> Object prop = getConfiguration().getProperty(CONFIG_KEY_DISABLE_MAPPER); if (prop instanceof Boolean) { return (Boolean)prop; - } else { - Config config; - try { - config = ConfigProvider.getConfig(); - } catch (ExceptionInInitializerError | NoClassDefFoundError | IllegalStateException ex) { - // no config provider implementation - return false; - } - return config.getOptionalValue(CONFIG_KEY_DISABLE_MAPPER, - Boolean.class).orElse(false); } + return ConfigFacade.getOptionalValue(CONFIG_KEY_DISABLE_MAPPER, + Boolean.class).orElse(false); } } diff --git a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/RestClientBean.java b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/RestClientBean.java index 7baec1c..7524f0c 100644 --- a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/RestClientBean.java +++ b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/RestClientBean.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; + import javax.enterprise.context.Dependent; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.Default; @@ -36,9 +37,9 @@ import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.PassivationCapable; import javax.enterprise.util.AnnotationLiteral; + import org.apache.cxf.microprofile.client.CxfTypeSafeClientBuilder; -import org.eclipse.microprofile.config.Config; -import org.eclipse.microprofile.config.ConfigProvider; +import org.apache.cxf.microprofile.client.config.ConfigFacade; import org.eclipse.microprofile.rest.client.inject.RestClient; public class RestClientBean implements Bean<Object>, PassivationCapable { @@ -48,12 +49,10 @@ public class RestClientBean implements Bean<Object>, PassivationCapable { private final Class<?> clientInterface; private final Class<? extends Annotation> scope; private final BeanManager beanManager; - private final Config config; RestClientBean(Class<?> clientInterface, BeanManager beanManager) { this.clientInterface = clientInterface; this.beanManager = beanManager; - this.config = ConfigProvider.getConfig(); this.scope = this.readScope(); } @Override @@ -124,16 +123,20 @@ public class RestClientBean implements Bean<Object>, PassivationCapable { private String getBaseUrl() { String property = String.format(REST_URL_FORMAT, clientInterface.getName()); - return config.getValue(property, String.class); + String baseURL = ConfigFacade.getValue(property, String.class); + if (baseURL == null) { + throw new IllegalStateException("Unable to determine base URL from configuration"); + } + return baseURL; } private Class<? extends Annotation> readScope() { // first check to see if the value is set String property = String.format(REST_SCOPE_FORMAT, clientInterface.getName()); - String configuredScope = config.getOptionalValue(property, String.class).orElse(null); + String configuredScope = ConfigFacade.getOptionalValue(property, String.class).orElse(null); if (configuredScope != null) { try { - return (Class<? extends Annotation>)Class.forName(configuredScope); + return (Class<? extends Annotation>) Class.forName(configuredScope); } catch (Exception e) { throw new IllegalArgumentException("The scope " + configuredScope + " is invalid", e); } diff --git a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/config/ConfigFacade.java b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/config/ConfigFacade.java new file mode 100644 index 0000000..e543d7c --- /dev/null +++ b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/config/ConfigFacade.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cxf.microprofile.client.config; + +import java.util.Optional; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; + +public final class ConfigFacade { + + private ConfigFacade() { + } + + private static Optional<Config> config() { + Config c; + try { + c = ConfigProvider.getConfig(); + } catch (ExceptionInInitializerError | NoClassDefFoundError | IllegalStateException ex) { + // expected if no MP Config implementation is available + c = null; + } + return Optional.ofNullable(c); + } + + public static <T> Optional<T> getOptionalValue(String propertyName, Class<T> clazz) { + Optional<Config> c = config(); + return c.isPresent() ? c.get().getOptionalValue(propertyName, clazz) : Optional.empty(); + } + + public static <T> T getValue(String propertyName, Class<T> clazz) { + Optional<Config> c = config(); + return c.isPresent() ? c.get().getValue(propertyName, clazz) : null; + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
