Author: davsclaus
Date: Fri Jul 15 12:27:53 2011
New Revision: 1147118
URL: http://svn.apache.org/viewvc?rev=1147118&view=rev
Log:
CAMEL-4236: registry lookup with type parameter will now report better
exceptions in case of a ClassCastException so end users can see the actual and
expected types.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RegistryLookupTypeClassCastExceptionTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchBeanException.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CompositeRegistry.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SimpleRegistry.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SimpleRegistryTest.java
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchBeanException.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchBeanException.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchBeanException.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchBeanException.java
Fri Jul 15 12:27:53 2011
@@ -40,6 +40,11 @@ public class NoSuchBeanException extends
this.name = name;
}
+ public NoSuchBeanException(String name, String message, Throwable cause) {
+ super(message, cause);
+ this.name = name;
+ }
+
public String getName() {
return name;
}
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=1147118&r1=1147117&r2=1147118&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
Fri Jul 15 12:27:53 2011
@@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
/**
@@ -44,9 +45,18 @@ public class CompositeRegistry implement
public <T> T lookup(String name, Class<T> type) {
T answer = null;
for (Registry registry : registryList) {
- answer = registry.lookup(name, type);
- if (answer != null) {
- break;
+ try {
+ answer = registry.lookup(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
+ + " with expected type: " + type + " due: " +
e.getMessage(), e);
}
}
return answer;
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
Fri Jul 15 12:27:53 2011
@@ -24,6 +24,7 @@ import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Registry;
@@ -43,8 +44,20 @@ public class JndiRegistry implements Reg
}
public <T> T lookup(String name, Class<T> type) {
- Object value = lookup(name);
- return type.cast(value);
+ Object answer = lookup(name);
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ try {
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in JNDI Context: " + context
+ + " of type: " + answer.getClass().getName() + " expected
type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
}
public Object lookup(String name) {
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SimpleRegistry.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SimpleRegistry.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SimpleRegistry.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/SimpleRegistry.java
Fri Jul 15 12:27:53 2011
@@ -19,6 +19,7 @@ package org.apache.camel.impl;
import java.util.HashMap;
import java.util.Map;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
/**
@@ -33,8 +34,20 @@ public class SimpleRegistry extends Hash
}
public <T> T lookup(String name, Class<T> type) {
- Object o = lookup(name);
- return type.cast(o);
+ Object answer = lookup(name);
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ 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);
+ }
}
public <T> Map<String, T> lookupByType(Class<T> type) {
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RegistryLookupTypeClassCastExceptionTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RegistryLookupTypeClassCastExceptionTest.java?rev=1147118&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RegistryLookupTypeClassCastExceptionTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RegistryLookupTypeClassCastExceptionTest.java
Fri Jul 15 12:27:53 2011
@@ -0,0 +1,90 @@
+/**
+ * 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 junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.NoSuchBeanException;
+
+/**
+ *
+ */
+public class RegistryLookupTypeClassCastExceptionTest extends TestCase {
+
+ public void testLookupOk() throws Exception {
+ SimpleRegistry simple = new SimpleRegistry();
+
+ MyClass my = new MyClass();
+ simple.put("my", my);
+
+ assertEquals(my, simple.lookup("my"));
+ assertEquals(my, simple.lookup("my", MyClass.class));
+
+ assertNull(simple.lookup("foo"));
+ assertNull(simple.lookup("foo", MyClass.class));
+ }
+
+ public void testCamelContextLookupOk() throws Exception {
+ SimpleRegistry simple = new SimpleRegistry();
+ CamelContext context = new DefaultCamelContext(simple);
+
+ MyClass my = new MyClass();
+ simple.put("my", my);
+
+ assertEquals(my, context.getRegistry().lookup("my"));
+ assertEquals(my, context.getRegistry().lookup("my", MyClass.class));
+
+ assertNull(context.getRegistry().lookup("foo"));
+ assertNull(context.getRegistry().lookup("foo", MyClass.class));
+ }
+
+ public void testLookupClassCast() throws Exception {
+ SimpleRegistry simple = new SimpleRegistry();
+
+ MyClass my = new MyClass();
+ simple.put("my", my);
+
+ try {
+ simple.lookup("my", String.class);
+ fail("Should have thrown exception");
+ } catch (NoSuchBeanException e) {
+ assertEquals("my", e.getName());
+ assertTrue(e.getMessage().endsWith("expected type was: class
java.lang.String"));
+ }
+ }
+
+ public void testCamelContextLookupClassCast() throws Exception {
+ SimpleRegistry simple = new SimpleRegistry();
+ CamelContext context = new DefaultCamelContext(simple);
+
+ MyClass my = new MyClass();
+ simple.put("my", my);
+
+ try {
+ context.getRegistry().lookup("my", String.class);
+ fail("Should have thrown exception");
+ } catch (NoSuchBeanException e) {
+ assertEquals("my", e.getName());
+ assertTrue(e.getMessage().endsWith("expected type was: class
java.lang.String"));
+ }
+ }
+
+ public static class MyClass {
+ // just a test class
+ }
+
+}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SimpleRegistryTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SimpleRegistryTest.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SimpleRegistryTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/SimpleRegistryTest.java
Fri Jul 15 12:27:53 2011
@@ -19,6 +19,7 @@ package org.apache.camel.impl;
import java.util.Map;
import junit.framework.TestCase;
+import org.apache.camel.NoSuchBeanException;
public class SimpleRegistryTest extends TestCase {
@@ -46,8 +47,10 @@ public class SimpleRegistryTest extends
try {
registry.lookup("a", Float.class);
fail();
- } catch (ClassCastException e) {
+ } catch (NoSuchBeanException e) {
// expected
+ assertEquals("a", e.getName());
+ assertTrue(e.getMessage().endsWith("of type: java.lang.String
expected type was: class java.lang.Float"));
}
}
Modified:
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
(original)
+++
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
Fri Jul 15 12:27:53 2011
@@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.aries.blueprint.ExtendedBeanMetadata;
import org.apache.aries.blueprint.mutable.MutableReferenceMetadata;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
import org.osgi.framework.Bundle;
import org.osgi.service.blueprint.container.BlueprintContainer;
@@ -39,11 +40,25 @@ public class BlueprintContainerRegistry
}
public <T> T lookup(String name, Class<T> type) {
+ Object answer;
try {
- return type.cast(blueprintContainer.getComponentInstance(name));
+ answer = blueprintContainer.getComponentInstance(name);
} catch (NoSuchComponentException e) {
return null;
}
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ try {
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in BlueprintContainer: " +
blueprintContainer
+ + " of type: " + answer.getClass().getName() + " expected
type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
}
public <T> Map<String, T> lookupByType(Class<T> type) {
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
(original)
+++
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
Fri Jul 15 12:27:53 2011
@@ -91,8 +91,9 @@ public class HttpComponent extends Heade
* @param parameters the map of parameters
* @param secure whether the endpoint is secure (eg https4)
* @return the configurer
+ * @throws Exception is thrown if error creating configurer
*/
- protected HttpClientConfigurer createHttpClientConfigurer(Map<String,
Object> parameters, boolean secure) {
+ protected HttpClientConfigurer createHttpClientConfigurer(Map<String,
Object> parameters, boolean secure) throws Exception {
// prefer to use endpoint configured over component configured
HttpClientConfigurer configurer =
resolveAndRemoveReferenceParameter(parameters, "httpClientConfigurerRef",
HttpClientConfigurer.class);
if (configurer == null) {
@@ -125,7 +126,7 @@ public class HttpComponent extends Heade
return configurer;
}
- private HttpClientConfigurer configureHttpProxy(Map<String, Object>
parameters, HttpClientConfigurer configurer, boolean secure) {
+ private HttpClientConfigurer configureHttpProxy(Map<String, Object>
parameters, HttpClientConfigurer configurer, boolean secure) throws Exception {
String proxyAuthScheme = getAndRemoveParameter(parameters,
"proxyAuthScheme", String.class);
if (proxyAuthScheme == null) {
// fallback and use either http4 or https4 depending on secure
@@ -140,6 +141,9 @@ public class HttpComponent extends Heade
String proxyAuthDomain = getAndRemoveParameter(parameters,
"proxyAuthDomain", String.class);
String proxyAuthNtHost = getAndRemoveParameter(parameters,
"proxyAuthNtHost", String.class);
+ // register scheme for proxy
+ registerPort(secure, x509HostnameVerifier, proxyAuthPort,
sslContextParameters);
+
if (proxyAuthUsername != null && proxyAuthPassword != null) {
return CompositeHttpConfigurer.combineConfigurers(
configurer, new ProxyHttpClientConfigurer(proxyAuthHost,
proxyAuthPort, proxyAuthScheme, proxyAuthUsername, proxyAuthPassword,
proxyAuthDomain, proxyAuthNtHost));
Modified:
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java?rev=1147118&r1=1147117&r2=1147118&view=diff
==============================================================================
---
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
(original)
+++
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
Fri Jul 15 12:27:53 2011
@@ -18,6 +18,7 @@ package org.apache.camel.spring.spi;
import java.util.Map;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -37,14 +38,27 @@ public class ApplicationContextRegistry
}
public <T> T lookup(String name, Class<T> type) {
+ Object answer;
try {
- Object value = applicationContext.getBean(name, type);
- return type.cast(value);
+ answer = applicationContext.getBean(name, type);
} catch (NoSuchBeanDefinitionException e) {
return null;
} catch (BeanNotOfRequiredTypeException e) {
return null;
}
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ try {
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in ApplicationContext: " +
applicationContext
+ + " of type: " + answer.getClass().getName() + " expected
type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
}
public Object lookup(String name) {