Author: ningjiang Date: Thu Mar 7 06:23:24 2013 New Revision: 1453704 URL: http://svn.apache.org/r1453704 Log: CAMEL-6135 CompositeRegistry should catch the exception when it lookup the component across the registries
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CompositeRegistryTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java?rev=1453704&r1=1453703&r2=1453704&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java Thu Mar 7 06:23:24 2013 @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Set; import org.apache.camel.NoSuchBeanException; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.Registry; /** @@ -45,22 +46,28 @@ public class CompositeRegistry implement public <T> T lookupByNameAndType(String name, Class<T> type) { T answer = null; + RuntimeCamelException ex = null; for (Registry registry : registryList) { try { answer = registry.lookupByNameAndType(name, type); - if (answer != null) { - break; - } } catch (Throwable e) { // do not double wrap the exception if (e instanceof NoSuchBeanException) { - throw (NoSuchBeanException) e; - } - throw new NoSuchBeanException(name, "Cannot lookup: " + name + " from registry: " + registry + ex = (NoSuchBeanException)e; + } else { + ex = new NoSuchBeanException(name, "Cannot lookup: " + name + " from registry: " + registry + " with expected type: " + type + " due: " + e.getMessage(), e); + } + } + if (answer != null) { + return answer; } } - return answer; + if (ex != null) { + throw ex; + } else { + return answer; + } } public Object lookupByName(String name) { Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CompositeRegistryTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CompositeRegistryTest.java?rev=1453704&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CompositeRegistryTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CompositeRegistryTest.java Thu Mar 7 06:23:24 2013 @@ -0,0 +1,49 @@ +/** + * 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 org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class CompositeRegistryTest { + private CompositeRegistry registry; + @Before + public void createContext() throws Exception { + SimpleRegistry sr1 = new SimpleRegistry(); + sr1.put("name", new Integer(12)); + SimpleRegistry sr2 = new SimpleRegistry(); + sr2.put("name", "12"); + registry = new CompositeRegistry(); + registry.addRegistry(sr2); + registry.addRegistry(sr1); + } + + @Test + public void testGetNameAndType() throws Exception { + Object result = registry.lookupByNameAndType("name", String.class); + assertNotNull(result); + assertEquals("Get a wrong result", result, "12"); + + result = registry.lookup("test", Integer.class); + assertNull(result); + } + +}