rmannibucau commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory URL: https://github.com/apache/cxf/pull/619#discussion_r366706385
########## File path: rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/CDIUtils.java ########## @@ -0,0 +1,56 @@ +/** + * 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.cdi; + +import java.util.NoSuchElementException; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.CDI; + +public final class CDIUtils { + + private CDIUtils() { + } + + public static <T> T getInstanceFromCDI(Class<T> clazz) { + T t; + try { + t = findBean(clazz); + } catch (ExceptionInInitializerError | NoClassDefFoundError | IllegalStateException ex) { + // expected if no CDI implementation is available + t = null; + } catch (NoSuchElementException ex) { + // expected if ClientHeadersFactory is not managed by CDI + t = null; + } + return t; + } + + @SuppressWarnings("unchecked") + private static <T> T findBean(Class<T> clazz) { + BeanManager beanManager = CDI.current().getBeanManager(); + Bean<?> bean = beanManager.getBeans(clazz).iterator().next(); + CreationalContext<?> ctx = beanManager.createCreationalContext(bean); Review comment: Works since there is one factory per bus Side note being: why cdi utility does not return an Instance{value,runnable} and you use the runnable as a close callback to call the release method. Main difference is then the storage can be an instance list (not set ;)) in the factory instead of a static method which would leak if anything happens wrong in the app lifecycle. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services