This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 9607e9b464531266c4e8e9c9dfff083230a50c04
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Fri Oct 4 15:17:17 2019 +0200

    Remove custom registry implementation #198
---
 .../runtime/support/RuntimeBeanRepository.java     | 44 ++++++++++
 .../core/runtime/support/RuntimeRegistry.java      | 97 ++--------------------
 2 files changed, 53 insertions(+), 88 deletions(-)

diff --git 
a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeBeanRepository.java
 
b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeBeanRepository.java
new file mode 100644
index 0000000..c89aed8
--- /dev/null
+++ 
b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeBeanRepository.java
@@ -0,0 +1,44 @@
+/*
+ * 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.camel.quarkus.core.runtime.support;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.spi.BeanRepository;
+
+public final class RuntimeBeanRepository implements BeanRepository {
+    @Override
+    public Object lookupByName(String name) {
+        return lookupByNameAndType(name, Object.class);
+    }
+
+    @Override
+    public <T> T lookupByNameAndType(String name, Class<T> type) {
+        return BeanManagerHelper.getReferenceByName(name, type).orElse(null);
+    }
+
+    @Override
+    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
+        return BeanManagerHelper.getReferencesByTypeWithName(type);
+    }
+
+    @Override
+    public <T> Set<T> findByType(Class<T> type) {
+        return BeanManagerHelper.getReferencesByType(type);
+    }
+}
diff --git 
a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
 
b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
index d796c6d..a640cbf 100644
--- 
a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
+++ 
b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
@@ -16,97 +16,18 @@
  */
 package org.apache.camel.quarkus.core.runtime.support;
 
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-
 import io.quarkus.runtime.RuntimeValue;
-import org.apache.camel.NoSuchBeanException;
-import org.apache.camel.spi.Registry;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * A {@link Map}-based registry.
- */
-public class RuntimeRegistry extends HashMap<String, Map<Class<?>, Object>> 
implements Registry {
-
-    protected final Logger log = LoggerFactory.getLogger(getClass());
-
-    public void bind(String name, Object object) {
-        bind(name, object.getClass(), object);
-    }
-
-    public void bind(String name, Class<?> clazz, Object object) {
-        this.computeIfAbsent(name, k -> new HashMap<>()).put(clazz, object);
-    }
-
-    public Object lookupByName(String name) {
-        return lookupByNameAndType(name, Object.class);
-    }
-
-    public <T> T lookupByNameAndType(String name, Class<T> type) {
-        Optional<T> t = BeanManagerHelper.getReferenceByName(name, type);
-        if (t.isPresent()) {
-            return t.get();
-        }
-        Map<Class<?>, Object> map = this.get(name);
-        if (map == null) {
-            return null;
-        }
-        Object answer = map.get(type);
-        if (answer == null) {
-            for (Map.Entry<Class<?>, Object> entry : map.entrySet()) {
-                if (type.isAssignableFrom(entry.getKey())) {
-                    answer = entry.getValue();
-                    break;
-                }
-            }
-        }
-        if (answer instanceof RuntimeValue) {
-            log.debug("Creating {} for name {}", type.toString(), name);
-            answer = ((RuntimeValue) answer).getValue();
-        }
-        try {
-            return type.cast(answer);
-        } catch (Throwable e) {
-            String msg = "Found bean: " + name + " in SimpleRegistry: " + this
-                    + " of type: " + answer.getClass().getName() + " expected 
type was: " + type;
-            throw new NoSuchBeanException(name, msg, e);
-        }
-    }
+import org.apache.camel.support.DefaultRegistry;
 
-    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
-        Map<String, T> result = new HashMap<>();
-        for (Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
-            for (Object answer : entry.getValue().values()) {
-                if (answer instanceof RuntimeValue) {
-                    answer = ((RuntimeValue) answer).getValue();
-                }
-                if (type.isInstance(answer)) {
-                    result.put(entry.getKey(), type.cast(answer));
-                }
-            }
-        }
-        result.putAll(BeanManagerHelper.getReferencesByTypeWithName(type));
-        return result;
+public class RuntimeRegistry extends DefaultRegistry {
+    public RuntimeRegistry() {
+        super(new RuntimeBeanRepository());
     }
 
-    public <T> Set<T> findByType(Class<T> type) {
-        Set<T> result = new HashSet<>();
-        for (Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
-            for (Object answer : entry.getValue().values()) {
-                if (answer instanceof RuntimeValue) {
-                    answer = ((RuntimeValue) answer).getValue();
-                }
-                if (type.isInstance(answer)) {
-                    result.add(type.cast(answer));
-                }
-            }
-        }
-        result.addAll(BeanManagerHelper.getReferencesByType(type));
-        return result;
+    @Override
+    public Object unwrap(Object value) {
+        return (value instanceof RuntimeValue)
+            ? ((RuntimeValue)value).getValue()
+            : value;
     }
 }

Reply via email to