Author: davsclaus
Date: Sun Mar 4 12:49:31 2012
New Revision: 1296790
URL: http://svn.apache.org/viewvc?rev=1296790&view=rev
Log:
CAMEL-4994: @Consume now pre validates method is valid.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=1296790&r1=1296789&r2=1296790&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
Sun Mar 4 12:49:31 2012
@@ -93,12 +93,28 @@ public class BeanInfo {
this(camelContext, type, createParameterMappingStrategy(camelContext));
}
+ public BeanInfo(CamelContext camelContext, Method explicitMethod) {
+ this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod,
createParameterMappingStrategy(camelContext));
+ }
+
public BeanInfo(CamelContext camelContext, Class<?> type,
ParameterMappingStrategy strategy) {
+ this(camelContext, type, null, strategy);
+ }
+
+ public BeanInfo(CamelContext camelContext, Class<?> type, Method
explicitMethod, ParameterMappingStrategy strategy) {
this.camelContext = camelContext;
this.type = type;
this.strategy = strategy;
- introspect(getType());
+ if (explicitMethod != null) {
+ // must be a valid method
+ if (!isValidMethod(type, explicitMethod)) {
+ throw new IllegalArgumentException("The method " +
explicitMethod + " is not valid (for example the method must be public)");
+ }
+ introspect(getType(), explicitMethod);
+ } else {
+ introspect(getType());
+ }
// if there are only 1 method with 1 operation then select it as a
default/fallback method
MethodInfo method = null;
@@ -139,17 +155,28 @@ public class BeanInfo {
return answer;
}
- public MethodInvocation createInvocation(Method method, Object pojo,
Exchange exchange) {
- MethodInfo methodInfo = introspect(type, method);
- if (methodInfo != null) {
- return methodInfo.createMethodInvocation(pojo, exchange);
- }
- return null;
+ public MethodInvocation createInvocation(Object pojo, Exchange exchange)
+ throws AmbiguousMethodCallException, MethodNotFoundException {
+ return createInvocation(pojo, exchange, null);
}
- public MethodInvocation createInvocation(Object pojo, Exchange exchange)
+ private MethodInvocation createInvocation(Object pojo, Exchange exchange,
Method explicitMethod)
throws AmbiguousMethodCallException, MethodNotFoundException {
MethodInfo methodInfo = null;
+
+ // find the explicit method to invoke
+ if (explicitMethod != null) {
+ Iterator<List<MethodInfo>> it = operations.values().iterator();
+ while (it.hasNext()) {
+ List<MethodInfo> infos = it.next();
+ for (MethodInfo info : infos) {
+ if (explicitMethod.equals(info.getMethod())) {
+ return info.createMethodInvocation(pojo, exchange);
+ }
+ }
+ }
+ throw new MethodNotFoundException(exchange, pojo,
explicitMethod.getName());
+ }
String methodName =
exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
if (methodName != null) {
@@ -212,7 +239,7 @@ public class BeanInfo {
*
* @param clazz the class
*/
- protected void introspect(Class<?> clazz) {
+ private void introspect(Class<?> clazz) {
// get the target clazz as it could potentially have been enhanced by
CGLIB etc.
clazz = getTargetClass(clazz);
ObjectHelper.notNull(clazz, "clazz", this);
@@ -252,7 +279,7 @@ public class BeanInfo {
* @param method the method
* @return the method info, is newer <tt>null</tt>
*/
- protected MethodInfo introspect(Class<?> clazz, Method method) {
+ private MethodInfo introspect(Class<?> clazz, Method method) {
LOG.trace("Introspecting class: {}, method: {}", clazz, method);
String opName = method.getName();
@@ -264,7 +291,6 @@ public class BeanInfo {
MethodInfo existingMethodInfo = overridesExistingMethod(methodInfo);
if (existingMethodInfo != null) {
LOG.trace("This method is already overridden in a subclass, so the
method from the sub class is preferred: {}", existingMethodInfo);
-
return existingMethodInfo;
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java?rev=1296790&r1=1296789&r2=1296790&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
Sun Mar 4 12:49:31 2012
@@ -139,30 +139,26 @@ public class BeanProcessor extends Servi
}
MethodInvocation invocation;
- if (methodObject != null) {
- invocation = beanInfo.createInvocation(methodObject, bean,
exchange);
- } else {
- // set explicit method name to invoke as a header, which is how
BeanInfo can detect it
- if (explicitMethodName != null) {
- in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
- }
- try {
- invocation = beanInfo.createInvocation(bean, exchange);
- } catch (Throwable e) {
- exchange.setException(e);
- callback.done(true);
- return true;
- }
+ // set explicit method name to invoke as a header, which is how
BeanInfo can detect it
+ if (explicitMethodName != null) {
+ in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
+ }
+ try {
+ invocation = beanInfo.createInvocation(bean, exchange);
+ } catch (Throwable e) {
+ exchange.setException(e);
+ callback.done(true);
+ return true;
+ } finally {
+ // must remove headers as they were provisional
+ in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
+ in.removeHeader(Exchange.BEAN_METHOD_NAME);
}
if (invocation == null) {
throw new IllegalStateException("No method invocation could be
created, no matching method could be found on: " + bean);
}
- // remove headers as they should not be propagated
- in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
- in.removeHeader(Exchange.BEAN_METHOD_NAME);
-
- Object value = null;
+ Object value;
try {
AtomicBoolean sync = new AtomicBoolean(true);
value = invocation.proceed(callback, sync);
@@ -215,14 +211,6 @@ public class BeanProcessor extends Servi
// Properties
// -----------------------------------------------------------------------
- public Method getMethodObject() {
- return methodObject;
- }
-
- public void setMethodObject(Method methodObject) {
- this.methodObject = methodObject;
- }
-
public String getMethod() {
return method;
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java?rev=1296790&r1=1296789&r2=1296790&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
Sun Mar 4 12:49:31 2012
@@ -32,6 +32,7 @@ import org.apache.camel.Producer;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.ProxyInstantiationException;
import org.apache.camel.Service;
+import org.apache.camel.component.bean.BeanInfo;
import org.apache.camel.component.bean.BeanProcessor;
import org.apache.camel.component.bean.ProxyHelper;
import org.apache.camel.processor.UnitOfWorkProcessor;
@@ -124,8 +125,8 @@ public class CamelPostProcessorHelper im
* message exchange is received
*/
protected Processor createConsumerProcessor(final Object pojo, final
Method method, final Endpoint endpoint) {
- BeanProcessor answer = new BeanProcessor(pojo, getCamelContext());
- answer.setMethodObject(method);
+ BeanInfo info = new BeanInfo(getCamelContext(), method);
+ BeanProcessor answer = new BeanProcessor(pojo, info);
// must ensure the consumer is being executed in an unit of work so
synchronization callbacks etc is invoked
return new UnitOfWorkProcessor(answer);
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java?rev=1296790&r1=1296789&r2=1296790&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
Sun Mar 4 12:49:31 2012
@@ -29,6 +29,7 @@ import org.apache.camel.Produce;
import org.apache.camel.Producer;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.support.SynchronizationAdapter;
import org.apache.camel.util.ObjectHelper;
@@ -74,6 +75,21 @@ public class CamelPostProcessorHelperTes
assertMockEndpointsSatisfied();
}
+ public void testConsumePrivate() throws Exception {
+ CamelPostProcessorHelper helper = new
CamelPostProcessorHelper(context);
+
+ MyPrivateConsumeBean my = new MyPrivateConsumeBean();
+ Method method =
my.getClass().getDeclaredMethod("consumeSomethingPrivate", String.class);
+ try {
+ helper.consumerInjection(method, my, "foo");
+ fail("Should have thrown exception");
+ } catch (RuntimeCamelException e) {
+ IllegalArgumentException iae =
assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertTrue(iae.getMessage().startsWith("The method private void"));
+ assertTrue(iae.getMessage().endsWith("(for example the method must
be public)"));
+ }
+ }
+
public void testConsumeSynchronization() throws Exception {
CamelPostProcessorHelper helper = new
CamelPostProcessorHelper(context);
@@ -456,4 +472,13 @@ public class CamelPostProcessorHelperTes
}
+ public class MyPrivateConsumeBean {
+
+ @Consume(uri = "seda:foo")
+ private void consumeSomethingPrivate(String body) {
+ assertEquals("Hello World", body);
+ template.sendBody("mock:result", body);
+ }
+ }
+
}