You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/01/24 13:43:26 UTC
svn commit: r1235225 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/impl/
camel-core/src/main/java/org/apache/camel/util/
components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/
components/camel-spring-javacon...
Author: davsclaus
Date: Tue Jan 24 12:43:25 2012
New Revision: 1235225
URL: http://svn.apache.org/viewvc?rev=1235225&view=rev
Log:
CAMEL-4934: Moved CamelBeanPostProcessor from camel-spring to camel-core, to make it reusable by others. Ensure CamelTestSupport and camel-test-blueprint can run and do Camel @EndpointInjection without Spring JARs on classpath.
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ReflectionHelper.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/GenericBeansException.java
Modified:
camel/trunk/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/BeanJavaConfigTest.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/MainTest.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/config/ContextConfig.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithNestedConfigClassTest.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithPostProcessorTest.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyConfig.java
camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyPostProcessor.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterJUnit4Test.java
camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java
camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java
camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/ExchangeTestSupport.java
camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/TestSupport.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugNoLazyTypeConverterTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTestNGTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterCreateCamelContextPerClassTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTestNGTest.java
camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java?rev=1235225&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java Tue Jan 24 12:43:25 2012
@@ -0,0 +1,219 @@
+/**
+ * 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.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ReflectionHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A bean post processor which implements the <a href="http://camel.apache.org/bean-integration.html">Bean Integration</a>
+ * features in Camel. Features such as the <a href="http://camel.apache.org/bean-injection.html">Bean Injection</a> of objects like
+ * {@link org.apache.camel.Endpoint} and
+ * {@link org.apache.camel.ProducerTemplate} together with support for
+ * <a href="http://camel.apache.org/pojo-consuming.html">POJO Consuming</a> via the
+ * {@link org.apache.camel.Consume} annotation along with
+ * <a href="http://camel.apache.org/pojo-producing.html">POJO Producing</a> via the
+ * {@link org.apache.camel.Produce} annotation along with other annotations such as
+ * {@link org.apache.camel.DynamicRouter} for creating <a href="http://camel.apache.org/dynamicrouter-annotation.html">a Dynamic router via annotations</a>.
+ * {@link org.apache.camel.RecipientList} for creating <a href="http://camel.apache.org/recipientlist-annotation.html">a Recipient List router via annotations</a>.
+ * {@link org.apache.camel.RoutingSlip} for creating <a href="http://camel.apache.org/routingslip-annotation.html">a Routing Slip router via annotations</a>.
+ * <p/>
+ * Components such as <tt>camel-spring</tt>, and <tt>camel-blueprint</tt> can leverage this post processor to hook in Camel
+ * bean post processing into their bean processing framework.
+ */
+public class DefaultCamelBeanPostProcessor {
+
+ protected final transient Logger log = LoggerFactory.getLogger(getClass());
+ protected CamelPostProcessorHelper camelPostProcessorHelper;
+ protected CamelContext camelContext;
+
+ public DefaultCamelBeanPostProcessor() {
+ }
+
+ public DefaultCamelBeanPostProcessor(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ /**
+ * Apply this post processor to the given new bean instance <i>before</i> any bean
+ * initialization callbacks (like <code>afterPropertiesSet</code>
+ * or a custom init-method). The bean will already be populated with property values.
+ * The returned bean instance may be a wrapper around the original.
+ *
+ * @param bean the new bean instance
+ * @param beanName the name of the bean
+ * @return the bean instance to use, either the original or a wrapped one; if
+ * <code>null</code>, no subsequent BeanPostProcessors will be invoked
+ * @throws Exception is thrown if error post processing bean
+ */
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
+ log.trace("Camel bean processing before initialization for bean: {}", beanName);
+
+ // some beans cannot be post processed at this given time, so we gotta check beforehand
+ if (!canPostProcessBean(bean, beanName)) {
+ return bean;
+ }
+
+ injectFields(bean, beanName);
+ injectMethods(bean, beanName);
+
+ if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
+ CamelContextAware contextAware = (CamelContextAware)bean;
+ CamelContext context = getOrLookupCamelContext();
+ if (context == null) {
+ log.warn("No CamelContext defined yet so cannot inject into bean: " + beanName);
+ } else {
+ contextAware.setCamelContext(context);
+ }
+ }
+
+ return bean;
+ }
+
+ /**
+ * Apply this post processor to the given new bean instance <i>after</i> any bean
+ * initialization callbacks (like <code>afterPropertiesSet</code>
+ * or a custom init-method). The bean will already be populated with property values.
+ * The returned bean instance may be a wrapper around the original.
+ *
+ * @param bean the new bean instance
+ * @param beanName the name of the bean
+ * @return the bean instance to use, either the original or a wrapped one; if
+ * <code>null</code>, no subsequent BeanPostProcessors will be invoked
+ * @throws Exception is thrown if error post processing bean
+ */
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
+ log.trace("Camel bean processing after initialization for bean: {}", beanName);
+
+ // some beans cannot be post processed at this given time, so we gotta check beforehand
+ if (!canPostProcessBean(bean, beanName)) {
+ return bean;
+ }
+
+ if (bean instanceof DefaultEndpoint) {
+ DefaultEndpoint defaultEndpoint = (DefaultEndpoint) bean;
+ defaultEndpoint.setEndpointUriIfNotSpecified(beanName);
+ }
+
+ return bean;
+ }
+
+ /**
+ * Strategy to get the {@link CamelContext} to use.
+ */
+ public CamelContext getOrLookupCamelContext() {
+ return camelContext;
+ }
+
+ /**
+ * Strategy to get the {@link CamelPostProcessorHelper}
+ */
+ protected CamelPostProcessorHelper getPostProcessorHelper() {
+ if (camelPostProcessorHelper == null) {
+ camelPostProcessorHelper = new CamelPostProcessorHelper(getOrLookupCamelContext());
+ }
+ return camelPostProcessorHelper;
+ }
+
+ protected boolean canPostProcessBean(Object bean, String beanName) {
+ return bean != null;
+ }
+
+ protected boolean canSetCamelContext(Object bean, String beanName) {
+ if (bean instanceof CamelContextAware) {
+ CamelContextAware camelContextAware = (CamelContextAware) bean;
+ CamelContext context = camelContextAware.getCamelContext();
+ if (context != null) {
+ log.trace("CamelContext already set on bean with id [{}]. Will keep existing CamelContext on bean.", beanName);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
+ /**
+ * A strategy method to allow implementations to perform some custom JBI
+ * based injection of the POJO
+ *
+ * @param bean the bean to be injected
+ */
+ protected void injectFields(final Object bean, final String beanName) {
+ ReflectionHelper.doWithFields(bean.getClass(), new ReflectionHelper.FieldCallback() {
+ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
+ EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
+ if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) {
+ injectField(field, endpointInject.uri(), endpointInject.ref(), bean, beanName);
+ }
+
+ Produce produce = field.getAnnotation(Produce.class);
+ if (produce != null && getPostProcessorHelper().matchContext(produce.context())) {
+ injectField(field, produce.uri(), produce.ref(), bean, beanName);
+ }
+ }
+ });
+ }
+
+ protected void injectField(Field field, String endpointUri, String endpointRef, Object bean, String beanName) {
+ ReflectionHelper.setField(field, bean, getPostProcessorHelper().getInjectionValue(field.getType(), endpointUri, endpointRef, field.getName(), bean, beanName));
+ }
+
+ protected void injectMethods(final Object bean, final String beanName) {
+ ReflectionHelper.doWithMethods(bean.getClass(), new ReflectionHelper.MethodCallback() {
+ public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
+ setterInjection(method, bean, beanName);
+ getPostProcessorHelper().consumerInjection(method, bean, beanName);
+ }
+ });
+ }
+
+ protected void setterInjection(Method method, Object bean, String beanName) {
+ EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
+ if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) {
+ setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref());
+ }
+
+ Produce produce = method.getAnnotation(Produce.class);
+ if (produce != null && getPostProcessorHelper().matchContext(produce.context())) {
+ setterInjection(method, bean, beanName, produce.uri(), produce.ref());
+ }
+ }
+
+ protected void setterInjection(Method method, Object bean, String beanName, String endpointUri, String endpointRef) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes != null) {
+ if (parameterTypes.length != 1) {
+ log.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
+ } else {
+ String propertyName = ObjectHelper.getPropertyName(method);
+ Object value = getPostProcessorHelper().getInjectionValue(parameterTypes[0], endpointUri, endpointRef, propertyName, bean, beanName);
+ ObjectHelper.invokeMethod(method, bean, value);
+ }
+ }
+ }
+
+}
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ReflectionHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ReflectionHelper.java?rev=1235225&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ReflectionHelper.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ReflectionHelper.java Tue Jan 24 12:43:25 2012
@@ -0,0 +1,125 @@
+/**
+ * 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.util;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * Helper for working with reflection on classes.
+ * <p/>
+ * This code is based on org.apache.camel.spring.util.ReflectionUtils class.
+ */
+public final class ReflectionHelper {
+
+ private ReflectionHelper() {
+ // utility class
+ }
+
+ /**
+ * Callback interface invoked on each field in the hierarchy.
+ */
+ public interface FieldCallback {
+
+ /**
+ * Perform an operation using the given field.
+ *
+ * @param field the field to operate on
+ */
+ void doWith(Field field) throws IllegalArgumentException, IllegalAccessException;
+ }
+
+ /**
+ * Action to take on each method.
+ */
+ public interface MethodCallback {
+
+ /**
+ * Perform an operation using the given method.
+ *
+ * @param method the method to operate on
+ */
+ void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
+ }
+
+ /**
+ * Invoke the given callback on all fields in the target class, going up the
+ * class hierarchy to get all declared fields.
+ * @param clazz the target class to analyze
+ * @param fc the callback to invoke for each field
+ */
+ public static void doWithFields(Class<?> clazz, FieldCallback fc) throws IllegalArgumentException {
+ // Keep backing up the inheritance hierarchy.
+ Class<?> targetClass = clazz;
+ do {
+ Field[] fields = targetClass.getDeclaredFields();
+ for (Field field : fields) {
+ try {
+ fc.doWith(field);
+ } catch (IllegalAccessException ex) {
+ throw new IllegalStateException("Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
+ }
+ }
+ targetClass = targetClass.getSuperclass();
+ }
+ while (targetClass != null && targetClass != Object.class);
+ }
+
+ /**
+ * Perform the given callback operation on all matching methods of the given
+ * class and superclasses (or given interface and super-interfaces).
+ *
+ * @param clazz class to start looking at
+ * @param mc the callback to invoke for each method
+ */
+ public static void doWithMethods(Class<?> clazz, MethodCallback mc) throws IllegalArgumentException {
+ // Keep backing up the inheritance hierarchy.
+ Method[] methods = clazz.getDeclaredMethods();
+ for (Method method : methods) {
+ try {
+ mc.doWith(method);
+ } catch (IllegalAccessException ex) {
+ throw new IllegalStateException("Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
+ }
+ }
+ if (clazz.getSuperclass() != null) {
+ doWithMethods(clazz.getSuperclass(), mc);
+ } else if (clazz.isInterface()) {
+ for (Class<?> superIfc : clazz.getInterfaces()) {
+ doWithMethods(superIfc, mc);
+ }
+ }
+ }
+
+ public static void setField(Field f, Object instance, Object value) {
+ try {
+ boolean oldAccessible = f.isAccessible();
+ boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible;
+ if (shouldSetAccessible) {
+ f.setAccessible(true);
+ }
+ f.set(instance, value);
+ if (shouldSetAccessible) {
+ f.setAccessible(oldAccessible);
+ }
+ } catch (Exception ex) {
+ throw new UnsupportedOperationException("Cannot inject value of class: " + value.getClass() + " into: " + f);
+ }
+ }
+
+}
Modified: camel/trunk/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java Tue Jan 24 12:43:25 2012
@@ -34,8 +34,6 @@ import org.springframework.context.Resou
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-
-
/**
* A useful base class for writing
* <a href="http://www.springsource.org/javaconfig">Spring JavaConfig</a>
@@ -127,10 +125,8 @@ public abstract class CamelConfiguration
@Bean
public CamelBeanPostProcessor camelBeanPostProcessor() throws Exception {
CamelBeanPostProcessor answer = new CamelBeanPostProcessor();
-
- CamelContext camelContext = getBean(CamelContext.class);
- // lets lookup a bean
- answer.setCamelContext(camelContext);
+ answer.setApplicationContext(getApplicationContext());
+ // do not set CamelContext as we will lazy evaluate that later
return answer;
}
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/BeanJavaConfigTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/BeanJavaConfigTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/BeanJavaConfigTest.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/BeanJavaConfigTest.java Tue Jan 24 12:43:25 2012
@@ -55,7 +55,6 @@ public class BeanJavaConfigTest extends
}
public static class SomeBean {
-
@Handler
public String someMethod(String body) {
return "Hello " + body;
@@ -64,7 +63,6 @@ public class BeanJavaConfigTest extends
@Configuration
public static class ContextConfig extends SingleRouteCamelConfiguration {
-
@Bean
@Override
public RouteBuilder route() {
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/MainTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/MainTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/MainTest.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/MainTest.java Tue Jan 24 12:43:25 2012
@@ -25,6 +25,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext;
public class MainTest extends Assert {
+
@Test
public void testOptions() throws Exception {
CamelContext context = createCamelContext(new String[] {"-cc", "org.apache.camel.spring.javaconfig.config.ContextConfig"});
@@ -39,36 +40,28 @@ public class MainTest extends Assert {
context.start();
runTests(context);
context.stop();
-
}
private CamelContext createCamelContext(String[] options) throws Exception {
Main main = new Main();
main.parseArguments(options);
ApplicationContext applicationContext = main.createDefaultApplicationContext();
- CamelContext context = SpringCamelContext.springCamelContext(applicationContext);
+ CamelContext context = SpringCamelContext.springCamelContext(applicationContext);
return context;
}
-
-
+
private void runTests(CamelContext context) throws Exception {
MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
ProducerTemplate template = context.createProducerTemplate();
String expectedBody = "<matched/>";
-
resultEndpoint.expectedBodiesReceived(expectedBody);
-
template.sendBodyAndHeader("direct:start", expectedBody, "foo", "bar");
-
resultEndpoint.assertIsSatisfied();
resultEndpoint.reset();
-
resultEndpoint.expectedMessageCount(0);
-
template.sendBodyAndHeader("direct:start", "<notMatched/>", "foo", "notMatchedHeaderValue");
-
resultEndpoint.assertIsSatisfied();
}
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/config/ContextConfig.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/config/ContextConfig.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/config/ContextConfig.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/config/ContextConfig.java Tue Jan 24 12:43:25 2012
@@ -23,6 +23,7 @@ import org.springframework.context.annot
@Configuration
public class ContextConfig extends SingleRouteCamelConfiguration {
+
@Bean
public RouteBuilder route() {
return new RouteBuilder() {
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithNestedConfigClassTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithNestedConfigClassTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithNestedConfigClassTest.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithNestedConfigClassTest.java Tue Jan 24 12:43:25 2012
@@ -29,7 +29,6 @@ import static org.junit.Assert.assertEqu
* @version
*/
@ContextConfiguration(locations = "org.apache.camel.spring.javaconfig.test.JavaConfigWithNestedConfigClassTest$ContextConfig", loader = JavaConfigContextLoader.class)
-
@Component
public class JavaConfigWithNestedConfigClassTest extends AbstractJUnit4SpringContextTests implements Cheese {
private boolean doCheeseCalled;
@@ -37,7 +36,6 @@ public class JavaConfigWithNestedConfigC
@Test
public void testPostProcessorInjectsMe() throws Exception {
assertEquals("doCheese() should be called", true, doCheeseCalled);
-
}
public void doCheese() {
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithPostProcessorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithPostProcessorTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithPostProcessorTest.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/JavaConfigWithPostProcessorTest.java Tue Jan 24 12:43:25 2012
@@ -28,7 +28,6 @@ import static org.junit.Assert.assertEqu
* @version
*/
@ContextConfiguration(locations = "org.apache.camel.spring.javaconfig.test.MyConfig", loader = JavaConfigContextLoader.class)
-
@Component
public class JavaConfigWithPostProcessorTest extends AbstractJUnit4SpringContextTests implements Cheese {
private boolean doCheeseCalled;
@@ -36,7 +35,6 @@ public class JavaConfigWithPostProcessor
@Test
public void testPostProcessorInjectsMe() throws Exception {
assertEquals("doCheese() should be called", true, doCheeseCalled);
-
}
public void doCheese() {
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyConfig.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyConfig.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyConfig.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyConfig.java Tue Jan 24 12:43:25 2012
@@ -24,6 +24,7 @@ import org.springframework.context.annot
*/
@Configuration
public class MyConfig {
+
@Bean
public MyPostProcessor myPostProcessor() {
return new MyPostProcessor();
Modified: camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyPostProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyPostProcessor.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyPostProcessor.java (original)
+++ camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/MyPostProcessor.java Tue Jan 24 12:43:25 2012
@@ -27,20 +27,16 @@ import org.springframework.beans.factory
public class MyPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
- System.out.println("Invoked before Initialization on " + bean + " name " + name);
if (bean instanceof Cheese) {
Cheese cheese = (Cheese) bean;
- System.out.println("Before Initialization Invoking Cheese");
cheese.doCheese();
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
- System.out.println("Invoked after Initialization on " + bean + " name " + name);
if (bean instanceof Cheese) {
Cheese cheese = (Cheese) bean;
- System.out.println("After Initialization Invoking Cheese");
cheese.doCheese();
}
return bean;
Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java Tue Jan 24 12:43:25 2012
@@ -16,27 +16,21 @@
*/
package org.apache.camel.spring;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
import java.util.LinkedHashSet;
+import java.util.Map;
import java.util.Set;
-
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.CamelContext;
-import org.apache.camel.CamelContextAware;
import org.apache.camel.Endpoint;
-import org.apache.camel.EndpointInject;
-import org.apache.camel.Produce;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Service;
import org.apache.camel.core.xml.CamelJMXAgentDefinition;
import org.apache.camel.impl.CamelPostProcessorHelper;
-import org.apache.camel.impl.DefaultEndpoint;
-import org.apache.camel.spring.util.ReflectionUtils;
-import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
import org.apache.camel.util.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,23 +41,9 @@ import org.springframework.context.Appli
import org.springframework.context.ApplicationContextAware;
/**
- * A bean post processor which implements the <a href="http://camel.apache.org/bean-integration.html">Bean Integration</a>
- * features in Camel. Features such as the <a href="http://camel.apache.org/bean-injection.html">Bean Injection</a> of objects like
- * {@link Endpoint} and
- * {@link org.apache.camel.ProducerTemplate} together with support for
- * <a href="http://camel.apache.org/pojo-consuming.html">POJO Consuming</a> via the
- * {@link org.apache.camel.Consume} annotation along with
- * <a href="http://camel.apache.org/pojo-producing.html">POJO Producing</a> via the
- * {@link org.apache.camel.Produce} annotation along with other annotations such as
- * {@link org.apache.camel.DynamicRouter} for creating <a href="http://camel.apache.org/dynamicrouter-annotation.html">a Dynamic router via annotations</a>.
- * {@link org.apache.camel.RecipientList} for creating <a href="http://camel.apache.org/recipientlist-annotation.html">a Recipient List router via annotations</a>.
- * {@link org.apache.camel.RoutingSlip} for creating <a href="http://camel.apache.org/routingslip-annotation.html">a Routing Slip router via annotations</a>.
- * <p>
- * If you use the <camelContext> element in your <a href="http://camel.apache.org/spring.html">Spring XML</a>
- * then one of these bean post processors is implicitly installed and configured for you. So you should never have to
- * explicitly create or configure one of these instances.
+ * Spring specific {@link DefaultCamelBeanPostProcessor} which uses Spring {@link BeanPostProcessor} to post process beans.
*
- * @version
+ * @see DefaultCamelBeanPostProcessor
*/
@XmlRootElement(name = "beanPostProcessor")
@XmlAccessorType(XmlAccessType.FIELD)
@@ -76,51 +56,118 @@ public class CamelBeanPostProcessor impl
@XmlTransient
private ApplicationContext applicationContext;
@XmlTransient
- private CamelPostProcessorHelper postProcessor;
+ private CamelPostProcessorHelper camelPostProcessorHelper;
@XmlTransient
private String camelId;
- public CamelBeanPostProcessor() {
- }
+ // must use a delegate, as we cannot extend DefaultCamelBeanPostProcessor, as this will cause the
+ // XSD schema generator to include the DefaultCamelBeanPostProcessor as a type, which we do not want to
+ @XmlTransient
+ private final DefaultCamelBeanPostProcessor delegate = new DefaultCamelBeanPostProcessor() {
+ @Override
+ public CamelContext getOrLookupCamelContext() {
+ if (camelContext == null) {
+ if (camelId != null) {
+ LOG.trace("Looking up CamelContext by id: {} from Spring ApplicationContext: {}", camelId, applicationContext);
+ camelContext = applicationContext.getBean(camelId, CamelContext.class);
+ } else {
+ // lookup by type and grab the single CamelContext if exists
+ LOG.trace("Looking up CamelContext by type from Spring ApplicationContext: {}", applicationContext);
+ Map<String, CamelContext> contexts = applicationContext.getBeansOfType(CamelContext.class);
+ if (contexts != null && contexts.size() == 1) {
+ camelContext = contexts.values().iterator().next();
+ }
+ }
+ }
+ return camelContext;
+ }
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- LOG.trace("Camel bean processing before initialization for bean: {}", beanName);
+ @Override
+ public boolean canPostProcessBean(Object bean, String beanName) {
+ // the JMXAgent is a bit strange and causes Spring issues if we let it being
+ // post processed by this one. It does not need it anyway so we are good to go.
+ // We should also avoid to process the null object bean (in Spring 2.5.x)
+ if (bean == null || bean instanceof CamelJMXAgentDefinition) {
+ return false;
+ }
- // some beans cannot be post processed at this given time, so we gotta check beforehand
- if (!canPostProcessBean(bean, beanName)) {
- return bean;
+ return super.canPostProcessBean(bean, beanName);
}
- injectFields(bean, beanName);
- injectMethods(bean, beanName);
+ @Override
+ public CamelPostProcessorHelper getPostProcessorHelper() {
+ // lets lazily create the post processor
+ if (camelPostProcessorHelper == null) {
+ camelPostProcessorHelper = new CamelPostProcessorHelper() {
+
+ @Override
+ public CamelContext getCamelContext() {
+ // lets lazily lookup the camel context here
+ // as doing this will cause this context to be started immediately
+ // breaking the lifecycle ordering of different camel contexts
+ // so we only want to do this on demand
+ return delegate.getOrLookupCamelContext();
+ }
- if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
- CamelContextAware contextAware = (CamelContextAware)bean;
- CamelContext context = getOrLookupCamelContext();
- if (context == null) {
- LOG.warn("No CamelContext defined yet so cannot inject into bean: " + beanName);
- } else {
- contextAware.setCamelContext(context);
+ @Override
+ protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) {
+ return new BeanInstantiationException(type, "Could not instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
+ }
+
+ protected boolean isSingleton(Object bean, String beanName) {
+ // no application context has been injected which means the bean
+ // has not been enlisted in Spring application context
+ if (applicationContext == null || beanName == null) {
+ return super.isSingleton(bean, beanName);
+ } else {
+ return applicationContext.isSingleton(beanName);
+ }
+ }
+
+ protected void startService(Service service, Object bean, String beanName) throws Exception {
+ if (isSingleton(bean, beanName)) {
+ getCamelContext().addService(service);
+ } else {
+ // only start service and do not add it to CamelContext
+ ServiceHelper.startService(service);
+ if (prototypeBeans.add(beanName)) {
+ // do not spam the log with WARN so do this only once per bean name
+ LOG.warn("The bean with id [" + beanName + "] is prototype scoped and cannot stop the injected service when bean is destroyed: "
+ + service + ". You may want to stop the service manually from the bean.");
+ }
+ }
+ }
+ };
}
+ return camelPostProcessorHelper;
}
+ };
- return bean;
+ public CamelBeanPostProcessor() {
}
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- LOG.trace("Camel bean processing after initialization for bean: {}", beanName);
-
- // some beans cannot be post processed at this given time, so we gotta check beforehand
- if (!canPostProcessBean(bean, beanName)) {
- return bean;
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+ try {
+ return delegate.postProcessBeforeInitialization(bean, beanName);
+ } catch (Exception e) {
+ // do not wrap already beans exceptions
+ if (e instanceof BeansException) {
+ throw (BeansException) e;
+ }
+ throw new GenericBeansException("Error post processing bean: " + beanName, e);
}
+ }
- if (bean instanceof DefaultEndpoint) {
- DefaultEndpoint defaultEndpoint = (DefaultEndpoint) bean;
- defaultEndpoint.setEndpointUriIfNotSpecified(beanName);
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ try {
+ return delegate.postProcessAfterInitialization(bean, beanName);
+ } catch (Exception e) {
+ // do not wrap already beans exceptions
+ if (e instanceof BeansException) {
+ throw (BeansException) e;
+ }
+ throw new GenericBeansException("Error post processing bean: " + beanName, e);
}
-
- return bean;
}
// Properties
@@ -146,155 +193,4 @@ public class CamelBeanPostProcessor impl
this.camelId = camelId;
}
- // Implementation methods
- // -------------------------------------------------------------------------
-
- /**
- * Can we post process the given bean?
- *
- * @param bean the bean
- * @param beanName the bean name
- * @return true to process it
- */
- protected boolean canPostProcessBean(Object bean, String beanName) {
-
- // the JMXAgent is a bit strange and causes Spring issues if we let it being
- // post processed by this one. It does not need it anyway so we are good to go.
- // We should also avoid to process the null object bean (in Spring 2.5.x)
- if (bean == null || bean instanceof CamelJMXAgentDefinition) {
- return false;
- }
-
- // all other beans can of course be processed
- return true;
- }
-
-
- protected boolean canSetCamelContext(Object bean, String beanName) {
- if (bean instanceof CamelContextAware) {
- CamelContextAware camelContextAware = (CamelContextAware) bean;
- CamelContext context = camelContextAware.getCamelContext();
- if (context != null) {
- LOG.trace("CamelContext already set on bean with id [{}]. Will keep existing CamelContext on bean.", beanName);
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * A strategy method to allow implementations to perform some custom JBI
- * based injection of the POJO
- *
- * @param bean the bean to be injected
- */
- protected void injectFields(final Object bean, final String beanName) {
- ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
- public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
- EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
- if (endpointInject != null && getPostProcessor().matchContext(endpointInject.context())) {
- injectField(field, endpointInject.uri(), endpointInject.ref(), bean, beanName);
- }
-
- Produce produce = field.getAnnotation(Produce.class);
- if (produce != null && getPostProcessor().matchContext(produce.context())) {
- injectField(field, produce.uri(), produce.ref(), bean, beanName);
- }
- }
- });
- }
-
- protected void injectField(Field field, String endpointUri, String endpointRef, Object bean, String beanName) {
- ReflectionUtils.setField(field, bean, getPostProcessor().getInjectionValue(field.getType(), endpointUri, endpointRef, field.getName(), bean, beanName));
- }
-
- protected void injectMethods(final Object bean, final String beanName) {
- ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
- public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
- setterInjection(method, bean, beanName);
- getPostProcessor().consumerInjection(method, bean, beanName);
- }
- });
- }
-
- protected void setterInjection(Method method, Object bean, String beanName) {
- EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
- if (endpointInject != null && getPostProcessor().matchContext(endpointInject.context())) {
- setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref());
- }
-
- Produce produce = method.getAnnotation(Produce.class);
- if (produce != null && getPostProcessor().matchContext(produce.context())) {
- setterInjection(method, bean, beanName, produce.uri(), produce.ref());
- }
- }
-
- protected void setterInjection(Method method, Object bean, String beanName, String endpointUri, String endpointRef) {
- Class<?>[] parameterTypes = method.getParameterTypes();
- if (parameterTypes != null) {
- if (parameterTypes.length != 1) {
- LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
- } else {
- String propertyName = ObjectHelper.getPropertyName(method);
- Object value = getPostProcessor().getInjectionValue(parameterTypes[0], endpointUri, endpointRef, propertyName, bean, beanName);
- ObjectHelper.invokeMethod(method, bean, value);
- }
- }
- }
-
- protected CamelContext getOrLookupCamelContext() {
- if (camelContext == null && applicationContext.containsBean(camelId)) {
- camelContext = applicationContext.getBean(camelId, CamelContext.class);
- }
- return camelContext;
- }
-
- public CamelPostProcessorHelper getPostProcessor() {
- // lets lazily create the post processor
- if (postProcessor == null) {
- postProcessor = new CamelPostProcessorHelper() {
-
- @Override
- public CamelContext getCamelContext() {
- // lets lazily lookup the camel context here
- // as doing this will cause this context to be started immediately
- // breaking the lifecycle ordering of different camel contexts
- // so we only want to do this on demand
- return getOrLookupCamelContext();
- }
-
- @Override
- protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) {
- return new BeanInstantiationException(type, "Could not instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
- }
-
- protected boolean isSingleton(Object bean, String beanName) {
- // no application context has been injected which means the bean
- // has not been enlisted in Spring application context
- if (applicationContext == null || beanName == null) {
- return super.isSingleton(bean, beanName);
- } else {
- return applicationContext.isSingleton(beanName);
- }
- }
-
- protected void startService(Service service, Object bean, String beanName) throws Exception {
- if (isSingleton(bean, beanName)) {
- getCamelContext().addService(service);
- } else {
- // only start service and do not add it to CamelContext
- ServiceHelper.startService(service);
- if (prototypeBeans.add(beanName)) {
- // do not spam the log with WARN so do this only once per bean name
- LOG.warn("The bean with id [" + beanName + "] is prototype scoped and cannot stop the injected service when bean is destroyed: "
- + service + ". You may want to stop the service manually from the bean.");
- }
- }
- }
- };
- }
- return postProcessor;
- }
-
}
Added: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/GenericBeansException.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/GenericBeansException.java?rev=1235225&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/GenericBeansException.java (added)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/GenericBeansException.java Tue Jan 24 12:43:25 2012
@@ -0,0 +1,34 @@
+/**
+ * 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.spring;
+
+import org.springframework.beans.BeansException;
+
+/**
+ * A generic {@link org.springframework.beans.BeansException}.
+ */
+public final class GenericBeansException extends BeansException {
+
+ public GenericBeansException(String msg) {
+ super(msg);
+ }
+
+ public GenericBeansException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+
+}
Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java (original)
+++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java Tue Jan 24 12:43:25 2012
@@ -65,13 +65,11 @@ public abstract class CamelSpringTestSup
}
}
- /**
- * Lets post process this test instance to process any Camel annotations.
- * Note that using Spring Test or Guice is a more powerful approach.
- */
@Override
protected void postProcessTest() throws Exception {
+ // use the bean post processor from camel-spring
CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
+ processor.setApplicationContext(applicationContext);
processor.setCamelContext(context);
processor.postProcessBeforeInitialization(this, "this");
}
Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java (original)
+++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java Tue Jan 24 12:43:25 2012
@@ -37,6 +37,7 @@ import org.apache.camel.Service;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.BreakpointSupport;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultDebugger;
import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
@@ -240,7 +241,14 @@ public abstract class CamelTestSupport e
return true;
}
+ /**
+ * Lets post process this test instance to process any Camel annotations.
+ * Note that using Spring Test or Guice is a more powerful approach.
+ */
protected void postProcessTest() throws Exception {
+ // use the default bean post processor from camel-core
+ DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(context);
+ processor.postProcessBeforeInitialization(this, "this");
}
protected void stopCamelContext() throws Exception {
Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java (original)
+++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java Tue Jan 24 12:43:25 2012
@@ -60,7 +60,9 @@ public abstract class CamelSpringTestSup
applicationContext = threadAppContext.get();
}
+ // use the bean post processor from camel-spring
CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
+ processor.setApplicationContext(applicationContext);
processor.setCamelContext(context);
processor.postProcessBeforeInitialization(this, "this");
}
Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java (original)
+++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java Tue Jan 24 12:43:25 2012
@@ -37,6 +37,7 @@ import org.apache.camel.Service;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.BreakpointSupport;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultDebugger;
import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
@@ -348,6 +349,10 @@ public abstract class CamelTestSupport e
template = threadTemplate.get();
consumer = threadConsumer.get();
camelContextService = threadService.get();
+
+ // use the default bean post processor from camel-core
+ DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(context);
+ processor.postProcessBeforeInitialization(this, "this");
}
protected void stopCamelContext() throws Exception {
Modified: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterJUnit4Test.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterJUnit4Test.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterJUnit4Test.java (original)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterJUnit4Test.java Tue Jan 24 12:43:25 2012
@@ -16,7 +16,11 @@
*/
package org.apache.camel.test.patterns;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
@@ -28,24 +32,30 @@ import org.junit.Test;
// START SNIPPET: example
public class FilterJUnit4Test extends CamelTestSupport {
+ @EndpointInject(uri = "mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @Produce(uri = "direct:start")
+ protected ProducerTemplate template;
+
@Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
- getMockEndpoint("mock:result").expectedBodiesReceived(expectedBody);
+ resultEndpoint.expectedBodiesReceived(expectedBody);
- template.sendBodyAndHeader("direct:start", expectedBody, "foo", "bar");
+ template.sendBodyAndHeader(expectedBody, "foo", "bar");
- assertMockEndpointsSatisfied();
+ resultEndpoint.assertIsSatisfied();
}
@Test
public void testSendNotMatchingMessage() throws Exception {
- getMockEndpoint("mock:result").expectedMessageCount(0);
+ resultEndpoint.expectedMessageCount(0);
- template.sendBodyAndHeader("direct:start", "<notMatched/>", "foo", "notMatchedHeaderValue");
+ template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
- assertMockEndpointsSatisfied();
+ resultEndpoint.assertIsSatisfied();
}
@Override
@@ -56,5 +66,6 @@ public class FilterJUnit4Test extends Ca
}
};
}
+
}
// END SNIPPET: example
Modified: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java (original)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java Tue Jan 24 12:43:25 2012
@@ -16,8 +16,13 @@
*/
package org.apache.camel.test.patterns;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.CamelTestSupport;
+import org.junit.Test;
/**
* Tests filtering using Camel Test
@@ -27,22 +32,30 @@ import org.apache.camel.test.CamelTestSu
// START SNIPPET: example
public class FilterTest extends CamelTestSupport {
+ @EndpointInject(uri = "mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @Produce(uri = "direct:start")
+ protected ProducerTemplate template;
+
+ @Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
- getMockEndpoint("mock:result").expectedBodiesReceived(expectedBody);
+ resultEndpoint.expectedBodiesReceived(expectedBody);
- template.sendBodyAndHeader("direct:start", expectedBody, "foo", "bar");
+ template.sendBodyAndHeader(expectedBody, "foo", "bar");
- assertMockEndpointsSatisfied();
+ resultEndpoint.assertIsSatisfied();
}
+ @Test
public void testSendNotMatchingMessage() throws Exception {
- getMockEndpoint("mock:result").expectedMessageCount(0);
+ resultEndpoint.expectedMessageCount(0);
- template.sendBodyAndHeader("direct:start", "<notMatched/>", "foo", "notMatchedHeaderValue");
+ template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
- assertMockEndpointsSatisfied();
+ resultEndpoint.assertIsSatisfied();
}
@Override
Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java (original)
+++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java Tue Jan 24 12:43:25 2012
@@ -27,6 +27,7 @@ import org.apache.camel.Route;
import org.apache.camel.impl.DefaultPackageScanClassResolver;
import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
import org.apache.camel.impl.scan.InvertingPackageScanFilter;
+import org.apache.camel.spring.CamelBeanPostProcessor;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.camel.util.CastUtils;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -36,10 +37,6 @@ import org.springframework.context.suppo
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
-
-/**
- * @version $Revision$
- */
public abstract class CamelSpringTestSupport extends CamelTestSupport {
protected static ThreadLocal<AbstractApplicationContext> threadAppContext
= new ThreadLocal<AbstractApplicationContext>();
@@ -53,6 +50,12 @@ public abstract class CamelSpringTestSup
if (isCreateCamelContextPerClass()) {
applicationContext = threadAppContext.get();
}
+
+ // use the bean post processor from camel-spring
+ CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
+ processor.setApplicationContext(applicationContext);
+ processor.setCamelContext(context);
+ processor.postProcessBeforeInitialization(this, "this");
}
@Override
Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java (original)
+++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java Tue Jan 24 12:43:25 2012
@@ -37,6 +37,7 @@ import org.apache.camel.Service;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.BreakpointSupport;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultDebugger;
import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
@@ -45,7 +46,6 @@ import org.apache.camel.management.JmxSy
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.spi.Language;
-import org.apache.camel.spring.CamelBeanPostProcessor;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.TimeUtils;
import org.slf4j.Logger;
@@ -57,8 +57,6 @@ import org.testng.annotations.BeforeMeth
/**
* A useful base class which creates a {@link org.apache.camel.CamelContext} with some routes
* along with a {@link org.apache.camel.ProducerTemplate} for use in the test case
- *
- * @version $Revision$
*/
public abstract class CamelTestSupport extends TestSupport {
private static final Logger LOG = LoggerFactory.getLogger(TestSupport.class);
@@ -359,8 +357,8 @@ public abstract class CamelTestSupport e
consumer = threadConsumer.get();
camelContextService = threadService.get();
- CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
- processor.setCamelContext(context);
+ // use the default bean post processor from camel-core
+ DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(context);
processor.postProcessBeforeInitialization(this, "this");
}
Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/ExchangeTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/ExchangeTestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/ExchangeTestSupport.java (original)
+++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/ExchangeTestSupport.java Tue Jan 24 12:43:25 2012
@@ -24,8 +24,6 @@ import org.testng.annotations.BeforeTest
/**
* A base class for a test which requires a {@link org.apache.camel.CamelContext} and
* a populated {@link Exchange}
- *
- * @version $Revision$
*/
public abstract class ExchangeTestSupport extends CamelTestSupport {
protected Exchange exchange;
Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/TestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/TestSupport.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/TestSupport.java (original)
+++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/TestSupport.java Tue Jan 24 12:43:25 2012
@@ -45,8 +45,6 @@ import org.testng.Assert;
/**
* A bunch of useful testing methods
- *
- * @version $Revision$
*/
public abstract class TestSupport extends Assert {
protected static final String LS = System.getProperty("line.separator");
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugNoLazyTypeConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugNoLazyTypeConverterTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugNoLazyTypeConverterTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugNoLazyTypeConverterTest.java Tue Jan 24 12:43:25 2012
@@ -23,9 +23,6 @@ import org.apache.camel.model.ProcessorD
import org.apache.camel.testng.CamelTestSupport;
import org.testng.annotations.Test;
-/**
- * @version $Revision$
- */
public class DebugNoLazyTypeConverterTest extends CamelTestSupport {
@Override
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java Tue Jan 24 12:43:25 2012
@@ -20,7 +20,6 @@ import org.apache.camel.builder.RouteBui
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-
public class DebugSpringCamelContextTest extends DebugSpringTest {
@Override
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTest.java Tue Jan 24 12:43:25 2012
@@ -23,9 +23,6 @@ import org.apache.camel.model.ProcessorD
import org.apache.camel.testng.CamelTestSupport;
import org.testng.annotations.Test;
-/**
- * @version $Revision$
- */
public class DebugTest extends CamelTestSupport {
@Override
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTestNGTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTestNGTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTestNGTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugTestNGTest.java Tue Jan 24 12:43:25 2012
@@ -23,9 +23,6 @@ import org.apache.camel.model.ProcessorD
import org.apache.camel.testng.CamelTestSupport;
import org.testng.annotations.Test;
-/**
- * @version $Revision$
- */
public class DebugTestNGTest extends CamelTestSupport {
@Override
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterCreateCamelContextPerClassTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterCreateCamelContextPerClassTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterCreateCamelContextPerClassTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterCreateCamelContextPerClassTest.java Tue Jan 24 12:43:25 2012
@@ -26,8 +26,6 @@ import org.testng.annotations.Test;
/**
* Tests filtering using Camel Test
- *
- * @version $Revision$
*/
public class FilterCreateCamelContextPerClassTest extends CamelTestSupport {
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTest.java Tue Jan 24 12:43:25 2012
@@ -26,8 +26,6 @@ import org.testng.annotations.Test;
/**
* Tests filtering using Camel Test
- *
- * @version $Revision$
*/
public class FilterTest extends CamelTestSupport {
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTestNGTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTestNGTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTestNGTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/FilterTestNGTest.java Tue Jan 24 12:43:25 2012
@@ -26,8 +26,6 @@ import org.testng.annotations.Test;
/**
* Tests filtering using Camel Test
- *
- * @version $Revision$
*/
public class FilterTestNGTest extends CamelTestSupport {
Modified: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java?rev=1235225&r1=1235224&r2=1235225&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java (original)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java Tue Jan 24 12:43:25 2012
@@ -21,9 +21,6 @@ import org.apache.camel.builder.RouteBui
import org.apache.camel.testng.CamelTestSupport;
import org.testng.annotations.Test;
-/**
- *
- */
public class IsUseAdviceWithTest extends CamelTestSupport {
@Test