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 2010/04/16 12:22:19 UTC
svn commit: r934731 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/
camel-core/src/main/java/org/apache/camel/impl/
camel-core/src/test/java/org/apache/camel/impl/
components/camel-guice/src/main/java/org/apache/camel/guice/impl/ componen...
Author: davsclaus
Date: Fri Apr 16 10:22:19 2010
New Revision: 934731
URL: http://svn.apache.org/viewvc?rev=934731&view=rev
Log:
CAMEL-2650: Using @Produce or other Camel injected services will not consider singleton or prototype scope beans. For prototype scoped a WARN is logged about the need to manually stop the service after usage.
Added:
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java (with props)
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java (with props)
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java (with props)
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml
- copied, changed from r934703, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/AnotherCamelProxyTest.xml
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/IsSingleton.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
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/IsSingleton.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/IsSingleton.java?rev=934731&r1=934730&r2=934731&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/IsSingleton.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/IsSingleton.java Fri Apr 16 10:22:19 2010
@@ -26,7 +26,7 @@ package org.apache.camel;
public interface IsSingleton {
/**
- * Wheter this class supports being singleton or not.
+ * Whether this class supports being singleton or not.
*
* @return <tt>true</tt> to be a single shared instance, <tt>false</tt> to create new instances.
*/
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=934731&r1=934730&r2=934731&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 Fri Apr 16 10:22:19 2010
@@ -17,7 +17,6 @@
package org.apache.camel.impl;
import java.lang.reflect.Method;
-
import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.CamelContext;
@@ -26,6 +25,7 @@ import org.apache.camel.Consume;
import org.apache.camel.Consumer;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Endpoint;
+import org.apache.camel.IsSingleton;
import org.apache.camel.PollingConsumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
@@ -35,6 +35,7 @@ import org.apache.camel.component.bean.B
import org.apache.camel.component.bean.ProxyHelper;
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -78,15 +79,15 @@ public class CamelPostProcessorHelper im
return true;
}
- public void consumerInjection(Method method, Object bean) {
+ public void consumerInjection(Method method, Object bean, String beanName) {
Consume consume = method.getAnnotation(Consume.class);
if (consume != null && matchContext(consume.context())) {
LOG.info("Creating a consumer for: " + consume);
- subscribeMethod(method, bean, consume.uri(), consume.ref());
+ subscribeMethod(method, bean, beanName, consume.uri(), consume.ref());
}
}
- public void subscribeMethod(Method method, Object bean, String endpointUri, String endpointName) {
+ public void subscribeMethod(Method method, Object bean, String beanName, String endpointUri, String endpointName) {
// lets bind this method to a listener
String injectionPointName = method.getName();
Endpoint endpoint = getEndpointInjection(endpointUri, endpointName, injectionPointName, true);
@@ -97,7 +98,7 @@ public class CamelPostProcessorHelper im
if (LOG.isDebugEnabled()) {
LOG.debug("Created processor: " + processor + " for consumer: " + consumer);
}
- startService(consumer);
+ startService(consumer, bean, beanName);
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
@@ -107,8 +108,15 @@ public class CamelPostProcessorHelper im
/**
* Stats the given service
*/
- protected void startService(Service service) throws Exception {
- getCamelContext().addService(service);
+ protected void startService(Service service, Object bean, String beanName) throws Exception {
+ if (isSingleton(bean, beanName)) {
+ getCamelContext().addService(service);
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Service is not singleton so you must remember to stop it manually " + service);
+ }
+ ServiceHelper.startService(service);
+ }
}
/**
@@ -129,7 +137,8 @@ public class CamelPostProcessorHelper im
* Creates the object to be injected for an {@link org.apache.camel.EndpointInject} or {@link org.apache.camel.Produce} injection point
*/
@SuppressWarnings("unchecked")
- public Object getInjectionValue(Class<?> type, String endpointUri, String endpointRef, String injectionPointName) {
+ public Object getInjectionValue(Class<?> type, String endpointUri, String endpointRef, String injectionPointName,
+ Object bean, String beanName) {
if (type.isAssignableFrom(ProducerTemplate.class)) {
return createInjectionProducerTemplate(endpointUri, endpointRef, injectionPointName);
} else if (type.isAssignableFrom(ConsumerTemplate.class)) {
@@ -140,9 +149,9 @@ public class CamelPostProcessorHelper im
if (type.isInstance(endpoint)) {
return endpoint;
} else if (type.isAssignableFrom(Producer.class)) {
- return createInjectionProducer(endpoint);
+ return createInjectionProducer(endpoint, bean, beanName);
} else if (type.isAssignableFrom(PollingConsumer.class)) {
- return createInjectionPollingConsumer(endpoint);
+ return createInjectionPollingConsumer(endpoint, bean, beanName);
} else if (type.isInterface()) {
// lets create a proxy
try {
@@ -192,10 +201,10 @@ public class CamelPostProcessorHelper im
/**
* Factory method to create a started {@link org.apache.camel.PollingConsumer} to be injected into a POJO
*/
- protected PollingConsumer createInjectionPollingConsumer(Endpoint endpoint) {
+ protected PollingConsumer createInjectionPollingConsumer(Endpoint endpoint, Object bean, String beanName) {
try {
PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
- startService(pollingConsumer);
+ startService(pollingConsumer, bean, beanName);
return pollingConsumer;
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
@@ -205,10 +214,10 @@ public class CamelPostProcessorHelper im
/**
* A Factory method to create a started {@link org.apache.camel.Producer} to be injected into a POJO
*/
- protected Producer createInjectionProducer(Endpoint endpoint) {
+ protected Producer createInjectionProducer(Endpoint endpoint, Object bean, String beanName) {
try {
Producer producer = endpoint.createProducer();
- startService(producer);
+ startService(producer, bean, beanName);
return producer;
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
@@ -218,4 +227,18 @@ public class CamelPostProcessorHelper im
protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) {
return new ProxyInstantiationException(type, endpoint, e);
}
+
+ /**
+ * Implementations can override this method to determine if the bean is singleton.
+ *
+ * @param bean the bean
+ * @return <tt>true</tt> if its singleton scoped, for prototype scoped <tt>false</tt> is returned.
+ */
+ protected boolean isSingleton(Object bean, String beanName) {
+ if (bean instanceof IsSingleton) {
+ IsSingleton singleton = (IsSingleton) bean;
+ return singleton.isSingleton();
+ }
+ return true;
+ }
}
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=934731&r1=934730&r2=934731&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 Fri Apr 16 10:22:19 2010
@@ -59,7 +59,7 @@ public class CamelPostProcessorHelperTes
MyConsumeBean my = new MyConsumeBean();
Method method = my.getClass().getMethod("consumeSomething", String.class);
- helper.consumerInjection(method, my);
+ helper.consumerInjection(method, my, "foo");
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Hello World");
@@ -79,7 +79,7 @@ public class CamelPostProcessorHelperTes
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class type : parameterTypes) {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
ObjectHelper.invokeMethod(method, bean, value);
}
@@ -102,7 +102,7 @@ public class CamelPostProcessorHelperTes
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class type : parameterTypes) {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
ObjectHelper.invokeMethod(method, bean, value);
}
@@ -129,7 +129,7 @@ public class CamelPostProcessorHelperTes
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class type : parameterTypes) {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
ObjectHelper.invokeMethod(method, bean, value);
}
@@ -155,7 +155,7 @@ public class CamelPostProcessorHelperTes
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
Class<?> type = field.getType();
String propertyName = "producer";
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
field.set(bean, value);
@@ -179,7 +179,7 @@ public class CamelPostProcessorHelperTes
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
Class<?> type = field.getType();
String propertyName = "producer";
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
field.set(bean, value);
@@ -205,7 +205,7 @@ public class CamelPostProcessorHelperTes
String propertyName = "producer";
try {
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (IllegalArgumentException e) {
assertEquals("registry entry called unknown of type org.apache.camel.Endpoint must be specified", e.getMessage());
@@ -223,7 +223,7 @@ public class CamelPostProcessorHelperTes
String propertyName = "producer";
try {
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (ResolveEndpointFailedException e) {
assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
@@ -241,7 +241,7 @@ public class CamelPostProcessorHelperTes
String propertyName = "producer";
try {
- Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName);
+ Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (IllegalArgumentException e) {
assertEquals("Both uri and name is provided, only either one is allowed: uri=seda:foo, ref=myEndpoint", e.getMessage());
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java?rev=934731&r1=934730&r2=934731&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java Fri Apr 16 10:22:19 2010
@@ -31,8 +31,9 @@ import org.guiceyfruit.support.MethodHan
* @version $Revision$
*/
public class ConsumerInjection<I> extends CamelPostProcessorHelper implements MethodHandler<I, Consume> {
+
public void afterInjection(I injectee, Consume consume, Method method) throws InvocationTargetException, IllegalAccessException {
- consumerInjection(method, injectee);
+ consumerInjection(method, injectee, null);
}
@Override
@@ -47,4 +48,5 @@ public class ConsumerInjection<I> extend
public void setCamelContext(CamelContext camelContext) {
super.setCamelContext(camelContext);
}
+
}
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java?rev=934731&r1=934730&r2=934731&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java Fri Apr 16 10:22:19 2010
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.camel.guice.impl;
import java.lang.reflect.Field;
@@ -47,22 +46,22 @@ public class EndpointInjector extends Ca
String injectionPointName = field.getName();
String uri = inject.uri();
String endpointRef = inject.ref();
- return getInjectionValue(type, uri, endpointRef, injectionPointName);
- }
- public Object provide(EndpointInject inject, TypeLiteral<?> typeLiteral, Method method, Class<?> aClass,
- int index) {
+ return getInjectionValue(type, uri, endpointRef, injectionPointName, null, null);
+ }
+ public Object provide(EndpointInject inject, TypeLiteral<?> typeLiteral, Method method, Class<?> aClass, int index) {
Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> type = parameterTypes[index];
String injectionPointName = ObjectHelper.getPropertyName(method);
String endpointRef = inject.ref();
String uri = inject.uri();
- return getInjectionValue(type, uri, endpointRef, injectionPointName);
+ return getInjectionValue(type, uri, endpointRef, injectionPointName, null, null);
}
- public boolean isNullParameterAllowed(EndpointInject endpointInject, Method method, Class<?> aClass, int i) {
+ public boolean isNullParameterAllowed(EndpointInject endpointInject, Method method, Class<?> aClass, int index) {
return false;
}
+
}
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java?rev=934731&r1=934730&r2=934731&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java Fri Apr 16 10:22:19 2010
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.camel.guice.impl;
import java.lang.reflect.Field;
@@ -41,7 +40,7 @@ public class ProduceInjector extends Cam
super(camelContext);
}
- public boolean isNullParameterAllowed(Produce produce, Method method, Class<?> aClass, int i) {
+ public boolean isNullParameterAllowed(Produce produce, Method method, Class<?> aClass, int index) {
return false;
}
@@ -51,17 +50,17 @@ public class ProduceInjector extends Cam
String endpointRef = inject.ref();
String uri = inject.uri();
- return getInjectionValue(type, uri, endpointRef, injectionPointName);
+ return getInjectionValue(type, uri, endpointRef, injectionPointName, null, null);
}
- public Object provide(Produce inject, TypeLiteral<?> typeLiteral, Method method, Class<?> aClass,
- int index) {
-
+ public Object provide(Produce inject, TypeLiteral<?> typeLiteral, Method method, Class<?> aClass, int index) {
Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> type = parameterTypes[index];
String injectionPointName = ObjectHelper.getPropertyName(method);
String endpointRef = inject.ref();
String uri = inject.uri();
- return getInjectionValue(type, uri, endpointRef, injectionPointName);
+
+ return getInjectionValue(type, uri, endpointRef, injectionPointName, null, null);
}
+
}
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=934731&r1=934730&r2=934731&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 Fri Apr 16 10:22:19 2010
@@ -18,6 +18,8 @@ package org.apache.camel.spring;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.LinkedHashSet;
+import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -29,10 +31,12 @@ import org.apache.camel.CamelContextAwar
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
+import org.apache.camel.Service;
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.util.ServiceHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanInstantiationException;
@@ -63,6 +67,8 @@ import org.springframework.context.Appli
public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
private static final transient Log LOG = LogFactory.getLog(CamelBeanPostProcessor.class);
@XmlTransient
+ Set<String> prototypeBeans = new LinkedHashSet<String>();
+ @XmlTransient
private CamelContext camelContext;
@XmlTransient
private ApplicationContext applicationContext;
@@ -88,8 +94,8 @@ public class CamelBeanPostProcessor impl
setCamelContext((CamelContext) applicationContext.getBean(camelId));
}
- injectFields(bean);
- injectMethods(bean);
+ injectFields(bean, beanName);
+ injectMethods(bean, beanName);
if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
CamelContextAware contextAware = (CamelContextAware)bean;
@@ -139,6 +145,24 @@ public class CamelBeanPostProcessor impl
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) {
+ 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.");
+ }
+ }
+ }
};
}
@@ -195,56 +219,56 @@ public class CamelBeanPostProcessor impl
*
* @param bean the bean to be injected
*/
- protected void injectFields(final Object bean) {
+ 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 && postProcessor.matchContext(endpointInject.context())) {
- injectField(field, endpointInject.uri(), endpointInject.ref(), bean);
+ injectField(field, endpointInject.uri(), endpointInject.ref(), bean, beanName);
}
Produce produce = field.getAnnotation(Produce.class);
if (produce != null && postProcessor.matchContext(produce.context())) {
- injectField(field, produce.uri(), produce.ref(), bean);
+ injectField(field, produce.uri(), produce.ref(), bean, beanName);
}
}
});
}
- protected void injectField(Field field, String endpointUri, String endpointRef, Object bean) {
- ReflectionUtils.setField(field, bean, getPostProcessor().getInjectionValue(field.getType(), endpointUri, endpointRef, field.getName()));
+ 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) {
+ protected void injectMethods(final Object bean, final String beanName) {
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
@SuppressWarnings("unchecked")
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
- setterInjection(method, bean);
- getPostProcessor().consumerInjection(method, bean);
+ setterInjection(method, bean, beanName);
+ getPostProcessor().consumerInjection(method, bean, beanName);
}
});
}
- protected void setterInjection(Method method, Object bean) {
+ protected void setterInjection(Method method, Object bean, String beanName) {
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
if (endpointInject != null && postProcessor.matchContext(endpointInject.context())) {
- setterInjection(method, bean, endpointInject.uri(), endpointInject.ref());
+ setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref());
}
Produce produce = method.getAnnotation(Produce.class);
if (produce != null && postProcessor.matchContext(produce.context())) {
- setterInjection(method, bean, produce.uri(), produce.ref());
+ setterInjection(method, bean, beanName, produce.uri(), produce.ref());
}
}
- protected void setterInjection(Method method, Object bean, String endpointUri, String endpointRef) {
+ 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);
+ Object value = getPostProcessor().getInjectionValue(parameterTypes[0], endpointUri, endpointRef, propertyName, bean, beanName);
ObjectHelper.invokeMethod(method, bean, value);
}
}
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java?rev=934731&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java Fri Apr 16 10:22:19 2010
@@ -0,0 +1,36 @@
+/**
+ * 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.config;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Produce;
+import org.apache.camel.Producer;
+
+/**
+ * @version $Revision$
+ */
+public class MyProduceBean {
+
+ @Produce(uri = "direct:start")
+ private Producer producer;
+
+ public void testDoSomething(String body) throws Exception {
+ Exchange exchange = producer.createExchange();
+ exchange.getIn().setBody(body);
+ producer.process(exchange);
+ }
+}
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyProduceBean.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java?rev=934731&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java Fri Apr 16 10:22:19 2010
@@ -0,0 +1,57 @@
+/**
+ * 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.config;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringProduceInjectedPrototypeBeanTest extends SpringTestSupport {
+
+ @Override
+ protected AbstractXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml");
+ }
+
+ public void testProduceInjectedOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Bye World");
+
+ MyProduceBean bean = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+
+ bean.testDoSomething("Hello World");
+ bean.testDoSomething("Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testProduceInjectedTwice() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Bye World");
+
+ MyProduceBean bean = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+
+ bean.testDoSomething("Hello World");
+
+ MyProduceBean bean2 = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+ bean2.testDoSomething("Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java?rev=934731&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java Fri Apr 16 10:22:19 2010
@@ -0,0 +1,57 @@
+/**
+ * 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.config;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringProduceInjectedSingletonBeanTest extends SpringTestSupport {
+
+ @Override
+ protected AbstractXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml");
+ }
+
+ public void testProduceInjectedOnce() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Bye World");
+
+ MyProduceBean bean = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+
+ bean.testDoSomething("Hello World");
+ bean.testDoSomething("Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testProduceInjectedTwice() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Bye World");
+
+ MyProduceBean bean = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+
+ bean.testDoSomething("Hello World");
+
+ MyProduceBean bean2 = context.getRegistry().lookup("myProducerBean", MyProduceBean.class);
+ bean2.testDoSomething("Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
\ No newline at end of file
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml (from r934703, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/AnotherCamelProxyTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/AnotherCamelProxyTest.xml&r1=934703&r2=934731&rev=934731&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/AnotherCamelProxyTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedPrototypeBeanTest.xml Fri Apr 16 10:22:19 2010
@@ -22,24 +22,15 @@
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
- <!-- START SNIPPET: e1 -->
- <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">
+ <bean id="myProducerBean" class="org.apache.camel.spring.config.MyProduceBean" scope="prototype"/>
- <!-- create a proxy that will route to the direct:start endpoint when invoked -->
- <proxy id="myProxySender"
- serviceInterface="org.apache.camel.spring.config.MyProxySender"
- serviceUrl="direct:start"/>
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
- <!-- this is the route that our proxy will routed when invoked
- and the output from this route is returned as reply on the proxy -->
<route>
<from uri="direct:start"/>
- <transform>
- <simple>Bye ${body}</simple>
- </transform>
+ <to uri="mock:result"/>
</route>
</camelContext>
- <!-- END SNIPPET: e1 -->
</beans>
Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml?rev=934731&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml (added)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml Fri Apr 16 10:22:19 2010
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+ ">
+
+ <bean id="myProducerBean" class="org.apache.camel.spring.config.MyProduceBean" scope="singleton"/>
+
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+
+ <route>
+ <from uri="direct:start"/>
+ <to uri="mock:result"/>
+ </route>
+
+ </camelContext>
+
+</beans>
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringProduceInjectedSingletonBeanTest.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml