camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astefanu...@apache.org
Subject [6/6] camel git commit: Optional as return types for CDI bean manager references
Date Tue, 19 Apr 2016 12:58:58 GMT
Optional as return types for CDI bean manager references


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7a2a58cb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7a2a58cb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7a2a58cb

Branch: refs/heads/master
Commit: 7a2a58cb444b55488f2e94b5e29cb553e594aaf4
Parents: fb7b9bc
Author: Antonin Stefanutti <antonin@stefanutti.fr>
Authored: Mon Apr 18 15:13:23 2016 +0200
Committer: Antonin Stefanutti <antonin@stefanutti.fr>
Committed: Tue Apr 19 14:49:35 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/cdi/BeanManagerHelper.java | 35 +++++++++-----------
 .../camel/cdi/CdiCamelBeanPostProcessor.java    |  8 +++--
 .../org/apache/camel/cdi/CdiCamelInjector.java  |  8 ++---
 .../org/apache/camel/cdi/CdiCamelRegistry.java  |  8 +++--
 4 files changed, 28 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7a2a58cb/components/camel-cdi/src/main/java/org/apache/camel/cdi/BeanManagerHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/BeanManagerHelper.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/BeanManagerHelper.java
index 47118bf..9da35c1 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/BeanManagerHelper.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/BeanManagerHelper.java
@@ -16,11 +16,12 @@
  */
 package org.apache.camel.cdi;
 
-import java.lang.annotation.Annotation;
-import java.util.HashSet;
-import java.util.Set;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
+import java.lang.annotation.Annotation;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 @Vetoed
 final class BeanManagerHelper {
@@ -29,27 +30,21 @@ final class BeanManagerHelper {
     }
 
     static <T> Set<T> getReferencesByType(BeanManager manager, Class<T>
type, Annotation... qualifiers) {
-        Set<T> references = new HashSet<>();
-        for (Bean<?> bean : manager.getBeans(type, qualifiers)) {
-            references.add(getReference(manager, type, bean));
-        }
-        return references;
+        return manager.getBeans(type, qualifiers).stream()
+            .map(bean -> getReference(manager, type, bean))
+            .collect(Collectors.toSet());
     }
 
-    static <T> T getReferenceByName(BeanManager manager, String name, Class<T>
type) {
-        Set<Bean<?>> beans = manager.getBeans(name);
-        if (beans == null || beans.isEmpty()) {
-            return null;
-        }
-        return getReference(manager, type, manager.resolve(beans));
+    static <T> Optional<T> getReferenceByName(BeanManager manager, String name,
Class<T> type) {
+        return Optional.of(manager.getBeans(name))
+            .map(manager::resolve)
+            .map(bean -> getReference(manager, type, bean));
     }
 
-    static <T> T getReferenceByType(BeanManager manager, Class<T> type, Annotation...
qualifiers) {
-        Set<Bean<?>> beans = manager.getBeans(type, qualifiers);
-        if (beans == null || beans.isEmpty()) {
-            return null;
-        }
-        return getReference(manager, type, manager.resolve(beans));
+    static <T> Optional<T> getReferenceByType(BeanManager manager, Class<T>
type, Annotation... qualifiers) {
+        return Optional.of(manager.getBeans(type, qualifiers))
+            .map(manager::resolve)
+            .map(bean -> getReference(manager, type, bean));
     }
 
     static <T> T getReference(BeanManager manager, Class<T> type, Bean<?>
bean) {

http://git-wip-us.apache.org/repos/asf/camel/blob/7a2a58cb/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelBeanPostProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelBeanPostProcessor.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelBeanPostProcessor.java
index 7d1a1b8..f8411e4 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelBeanPostProcessor.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelBeanPostProcessor.java
@@ -32,6 +32,8 @@ import org.apache.camel.impl.CamelPostProcessorHelper;
 import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
 import org.apache.camel.util.ReflectionHelper;
 
+import static org.apache.camel.cdi.BeanManagerHelper.getReferenceByType;
+
 @Vetoed
 final class CdiCamelBeanPostProcessor extends DefaultCamelBeanPostProcessor {
 
@@ -110,11 +112,13 @@ final class CdiCamelBeanPostProcessor extends DefaultCamelBeanPostProcessor
{
 
     private CamelContext getOrLookupCamelContext(String contextName) {
         // TODO: proper support for custom context qualifiers
-        return BeanManagerHelper.getReferenceByType(manager, CamelContext.class, contextName.isEmpty()
? DefaultLiteral.INSTANCE : ContextName.Literal.of(contextName));
+        return getReferenceByType(manager, CamelContext.class,
+            contextName.isEmpty() ? DefaultLiteral.INSTANCE : ContextName.Literal.of(contextName))
+            .orElseThrow(() -> new UnsatisfiedResolutionException("No Camel context with
name [" + contextName + "] is deployed!"));
     }
 
     @Override
     public CamelContext getOrLookupCamelContext() {
-        return BeanManagerHelper.getReferenceByType(manager, CamelContext.class);
+        return getReferenceByType(manager, CamelContext.class).orElse(null);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7a2a58cb/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java
index 471b7f0..d1e8ba9 100755
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelInjector.java
@@ -33,12 +33,8 @@ final class CdiCamelInjector implements Injector {
 
     @Override
     public <T> T newInstance(Class<T> type) {
-        T instance = BeanManagerHelper.getReferenceByType(manager, type);
-        if (instance != null) {
-            return instance;
-        } else {
-            return injector.newInstance(type);
-        }
+        return BeanManagerHelper.getReferenceByType(manager, type)
+            .orElseGet(() -> injector.newInstance(type));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/7a2a58cb/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelRegistry.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelRegistry.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelRegistry.java
index 946fecf..6c68b7e 100755
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelRegistry.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelRegistry.java
@@ -48,9 +48,11 @@ final class CdiCamelRegistry implements Registry {
         logger.trace("Looking up bean with name [{}]", name);
         // Work-around for WELD-2089
         if ("properties".equals(name) && findByTypeWithName(PropertiesComponent.class).containsKey("properties"))
{
-            return BeanManagerHelper.getReferenceByName(manager, name, PropertiesComponent.class);
+            return BeanManagerHelper.getReferenceByName(manager, name, PropertiesComponent.class)
+                .orElse(null);
         }
-        return BeanManagerHelper.getReferenceByName(manager, name, Object.class);
+        return BeanManagerHelper.getReferenceByName(manager, name, Object.class)
+            .orElse(null);
     }
 
     @Override
@@ -58,7 +60,7 @@ final class CdiCamelRegistry implements Registry {
         ObjectHelper.notEmpty(name, "name");
         ObjectHelper.notNull(type, "type");
         logger.trace("Looking up bean with name [{}] of type [{}]", name, type);
-        return BeanManagerHelper.getReferenceByName(manager, name, type);
+        return BeanManagerHelper.getReferenceByName(manager, name, type).orElse(null);
     }
 
     @Override


Mime
View raw message