Repository: camel
Updated Branches:
  refs/heads/master fe1552e5a -> 71fe095de


CAMEL-7257: Added getRegistry(T) so its easier to get hold of the registrty 
unwrapped.


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

Branch: refs/heads/master
Commit: 71fe095de07007f9c535a1aab36d6bb979373790
Parents: fe1552e
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Mar 12 18:12:44 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Mar 12 18:12:44 2014 +0100

----------------------------------------------------------------------
 .../java/org/apache/camel/CamelContext.java     |  8 +++
 .../apache/camel/impl/CompositeRegistry.java    |  6 +-
 .../apache/camel/impl/DefaultCamelContext.java  | 21 ++++++
 .../camel/impl/GetRegistryAsTypeTest.java       | 70 ++++++++++++++++++++
 4 files changed, 104 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/71fe095d/camel-core/src/main/java/org/apache/camel/CamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java 
b/camel-core/src/main/java/org/apache/camel/CamelContext.java
index 9232038..7f8e42f 100644
--- a/camel-core/src/main/java/org/apache/camel/CamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java
@@ -664,6 +664,14 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
     Registry getRegistry();
 
     /**
+     * Returns the registry used to lookup components by name and as the given 
type
+     *
+     * @param type the registry type such as {@link 
org.apache.camel.impl.JndiRegistry}
+     * @return the registry, or <tt>null</tt> if the given type was not found 
as a registry implementation
+     */
+    <T> T getRegistry(Class<T> type);
+
+    /**
      * Returns the injector used to instantiate objects by type
      *
      * @return the injector

http://git-wip-us.apache.org/repos/asf/camel/blob/71fe095d/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java 
b/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java
index 884f544f..d40dbbe 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java
@@ -30,7 +30,7 @@ import org.apache.camel.spi.Registry;
  * This registry will look up the object with the sequence of the registry 
list until it finds the Object.
  */
 public class CompositeRegistry implements Registry {
-    private List<Registry> registryList;
+    private final List<Registry> registryList;
     
     public CompositeRegistry() {
         registryList = new ArrayList<Registry>();
@@ -44,6 +44,10 @@ public class CompositeRegistry implements Registry {
         registryList.add(registry);
     }
 
+    List<Registry> getRegistryList() {
+        return registryList;
+    }
+
     public <T> T lookupByNameAndType(String name, Class<T> type) {
         T answer = null;
         RuntimeCamelException ex = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/71fe095d/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index c10563d..d339805 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -1324,6 +1324,27 @@ public class DefaultCamelContext extends ServiceSupport 
implements ModelCamelCon
         return registry;
     }
 
+    public <T> T getRegistry(Class<T> type) {
+        Registry reg = getRegistry();
+
+        // unwrap the property placeholder delegate
+        if (reg instanceof PropertyPlaceholderDelegateRegistry) {
+            reg = ((PropertyPlaceholderDelegateRegistry) reg).getRegistry();
+        }
+
+        if (type.isAssignableFrom(reg.getClass())) {
+            return type.cast(reg);
+        } else if (reg instanceof CompositeRegistry) {
+            List<Registry> list = ((CompositeRegistry) reg).getRegistryList();
+            for (Registry r : list) {
+                if (type.isAssignableFrom(r.getClass())) {
+                    return type.cast(r);
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * Sets the registry to the given JNDI context
      *

http://git-wip-us.apache.org/repos/asf/camel/blob/71fe095d/camel-core/src/test/java/org/apache/camel/impl/GetRegistryAsTypeTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/GetRegistryAsTypeTest.java 
b/camel-core/src/test/java/org/apache/camel/impl/GetRegistryAsTypeTest.java
new file mode 100644
index 0000000..31d0507
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/GetRegistryAsTypeTest.java
@@ -0,0 +1,70 @@
+/**
+ * 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.impl;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+
+public class GetRegistryAsTypeTest extends TestCase {
+
+    public void testDefault() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        context.start();
+
+        JndiRegistry jndi = context.getRegistry(JndiRegistry.class);
+        assertNotNull(jndi);
+
+        assertNull(context.getRegistry(Map.class));
+        assertNull(context.getRegistry(SimpleRegistry.class));
+
+        context.stop();
+    }
+
+    public void testSimple() throws Exception {
+        CamelContext context = new DefaultCamelContext(new SimpleRegistry());
+        context.start();
+
+        SimpleRegistry simple = context.getRegistry(SimpleRegistry.class);
+        assertNotNull(simple);
+
+        // simple extends Map
+        assertNotNull(context.getRegistry(Map.class));
+        assertNull(context.getRegistry(JndiRegistry.class));
+
+        context.stop();
+    }
+
+    public void testComposite() throws Exception {
+        CompositeRegistry cr = new CompositeRegistry();
+        cr.addRegistry(new SimpleRegistry());
+        cr.addRegistry(new JndiRegistry());
+
+        CamelContext context = new DefaultCamelContext(cr);
+        context.start();
+
+        CompositeRegistry comp = context.getRegistry(CompositeRegistry.class);
+        assertNotNull(comp);
+        SimpleRegistry simple = context.getRegistry(SimpleRegistry.class);
+        assertNotNull(simple);
+        JndiRegistry jndi = context.getRegistry(JndiRegistry.class);
+        assertNotNull(jndi);
+
+        context.stop();
+    }
+}

Reply via email to