[GitHub] [cxf] andymc12 opened a new pull request #619: CXF-8188: Inject into ClientHeadersFactory

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[GitHub] [cxf] rmannibucau commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory

GitBox
rmannibucau commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory
URL: https://github.com/apache/cxf/pull/619#discussion_r367080698
 
 

 ##########
 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:
   If it is not. If it is, it is implicitly called, when not (kind of caller managed) it is your responsability.

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services
Reply | Threaded
Open this post in threaded view
|

[GitHub] [cxf] andymc12 commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory

GitBox
In reply to this post by GitBox
andymc12 commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory
URL: https://github.com/apache/cxf/pull/619#discussion_r367138644
 
 

 ##########
 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:
   Those changes are in - as well as updated unit tests.  Thanks again!

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services
Reply | Threaded
Open this post in threaded view
|

[GitHub] [cxf] reta commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory

GitBox
In reply to this post by GitBox
reta commented on a change in pull request #619: CXF-8188: Inject into ClientHeadersFactory
URL: https://github.com/apache/cxf/pull/619#discussion_r367184435
 
 

 ##########
 File path: rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/cdi/CDIFacade.java
 ##########
 @@ -0,0 +1,81 @@
+/**
+ * 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.Optional;
+import java.util.concurrent.Callable;
+
+import org.apache.cxf.Bus;
+
+
+public final class CDIFacade {
+
+    private static final boolean CDI_AVAILABLE;
+
+    private CDIFacade() {
+    }
+
+    static {
+        boolean b;
+        try {
+            Class.forName("javax.enterprise.inject.spi.BeanManager");
+            b = true;
+        } catch (Throwable t) {
+            b = false;
+        }
+        CDI_AVAILABLE = b;
+    }
+
+    public static Optional<Object> getBeanManager(Bus b) {
+        return nullableOptional(() -> CDIUtils.getCurrentBeanManager(b));
+    }
+
+    public static Optional<Object> getBeanManager() {
+        try {
+            return nullableOptional(() -> CDIUtils.getCurrentBeanManager());
+        } catch (Throwable t) {
+            t.printStackTrace();
+            return Optional.ofNullable(null);
+        }
+    }
+
+    public static <T> Optional<Instance<T>> getInstanceFromCDI(Class<T> clazz, Bus b) {
+        return nullableOptional(() -> CDIUtils.getInstanceFromCDI(clazz, b));
+    }
+
+    public static <T> Optional<Instance<T>> getInstanceFromCDI(Class<T> clazz) {
+        return nullableOptional(() -> CDIUtils.getInstanceFromCDI(clazz));
+    }
+
+    private static <T> Optional<T> nullableOptional(Callable<T> callable) {
+        if (!CDI_AVAILABLE) {
+            return Optional.ofNullable(null);
 
 Review comment:
   Minor, `Optional.empty()` would be the thing you are looking for :)

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services
Reply | Threaded
Open this post in threaded view
|

[GitHub] [cxf] andymc12 merged pull request #619: CXF-8188: Inject into ClientHeadersFactory

GitBox
In reply to this post by GitBox
andymc12 merged pull request #619: CXF-8188: Inject into ClientHeadersFactory
URL: https://github.com/apache/cxf/pull/619
 
 
   

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services
12