You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/05/08 18:08:36 UTC
svn commit: r536237 - in /activemq/camel/trunk:
camel-core/src/main/java/org/apache/camel/
camel-core/src/test/java/org/apache/camel/
camel-spring/src/main/java/org/apache/camel/spring/
camel-spring/src/main/java/org/apache/camel/spring/xml/ camel-spri...
Author: jstrachan
Date: Tue May 8 09:08:33 2007
New Revision: 536237
URL: http://svn.apache.org/viewvc?view=rev&rev=536237
Log:
added a default endpoint to the CamelTemplate; also added more test cases for the annotation based injection of Endpoint, Producer and CamelTemplate objects into POJOs
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelException.java
activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBean.java
activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBeanTest.java
activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/MySender.java
activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/PojoSenderTest.java
activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoSender.xml
activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/injectedBeanTest.xml
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd
activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelException.java?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelException.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelException.java Tue May 8 09:08:33 2007
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+/**
+ * Base class for all Camel checked exceptions typically thrown by a
+ * {@link Processor}
+ *
+ * @version $Revision: $
+ */
+public class CamelException extends Exception {
+
+ public CamelException() {
+ }
+
+ public CamelException(String message) {
+ super(message);
+ }
+
+ public CamelException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CamelException(Throwable cause) {
+ super(cause);
+ }
+}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java Tue May 8 09:08:33 2007
@@ -18,8 +18,12 @@
package org.apache.camel;
import org.apache.camel.impl.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ProducerCache;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* A client helper object (named like Spring's TransactionTemplate & JmsTemplate et al)
* for working with Camel and sending {@link Message} instances in an {@link Exchange}
@@ -30,11 +34,20 @@
public class CamelTemplate<E extends Exchange> extends ServiceSupport {
private CamelContext context;
private ProducerCache<E> producerCache = new ProducerCache<E>();
+ private boolean useEndpointCache = true;
+ private Map<String, Endpoint<E>> endpointCache = new HashMap<String, Endpoint<E>>();
+ private Endpoint<E> defaultEndpoint;
+
public CamelTemplate(CamelContext context) {
this.context = context;
}
+ public CamelTemplate(CamelContext context, Endpoint defaultEndpoint) {
+ this(context);
+ this.defaultEndpoint = defaultEndpoint;
+ }
+
/**
* Sends the exchange to the given endpoint
*
@@ -83,6 +96,23 @@
/**
* Send the body to an endpoint
*
+ * @param endpoint
+ * @param body = the payload
+ * @return the result
+ */
+ public Object sendBody(Endpoint<E> endpoint, final Object body) {
+ E result = send(endpoint, new Processor() {
+ public void process(Exchange exchange) {
+ Message in = exchange.getIn();
+ in.setBody(body);
+ }
+ });
+ return extractResultBody(result);
+ }
+
+ /**
+ * Send the body to an endpoint
+ *
* @param endpointUri
* @param body = the payload
* @return the result
@@ -117,6 +147,41 @@
return extractResultBody(result);
}
+ // Methods using the default endpoint
+ //-----------------------------------------------------------------------
+
+ /**
+ * Sends the body to the default endpoint and returns the result content
+ *
+ * @param body the body to send
+ * @return the returned message body
+ */
+ public Object sendBody(Object body) {
+ return sendBody(getMandatoryDefaultEndpoint(), body);
+ }
+
+ /**
+ * Sends the exchange to the default endpoint
+ *
+ * @param exchange the exchange to send
+ */
+ public E send(E exchange) {
+ return send(getMandatoryDefaultEndpoint(), exchange);
+ }
+
+ /**
+ * Sends an exchange to the default endpoint
+ * using a supplied @{link Processor} to populate the exchange
+ *
+ * @param processor the transformer used to populate the new exchange
+ */
+ public E send(Processor processor) {
+ return send(getMandatoryDefaultEndpoint(), processor);
+ }
+
+
+ // Properties
+ //-----------------------------------------------------------------------
public Producer<E> getProducer(Endpoint<E> endpoint) {
return producerCache.getProducer(endpoint);
}
@@ -125,12 +190,59 @@
return context;
}
+ public Endpoint<E> getDefaultEndpoint() {
+ return defaultEndpoint;
+ }
+
+ public void setDefaultEndpoint(Endpoint<E> defaultEndpoint) {
+ this.defaultEndpoint = defaultEndpoint;
+ }
+
+ /**
+ * Sets the default endpoint to use if none is specified
+ */
+ public void setDefaultEndpointUri(String endpointUri) {
+ setDefaultEndpoint(getContext().getEndpoint(endpointUri));
+ }
+
+ public boolean isUseEndpointCache() {
+ return useEndpointCache;
+ }
+
+ public void setUseEndpointCache(boolean useEndpointCache) {
+ this.useEndpointCache = useEndpointCache;
+ }
+
+ // Implementation methods
+ //-----------------------------------------------------------------------
+
protected Endpoint resolveMandatoryEndpoint(String endpointUri) {
- Endpoint endpoint = context.getEndpoint(endpointUri);
+ Endpoint endpoint = null;
+
+ if (isUseEndpointCache()) {
+ synchronized (endpointCache) {
+ endpoint = endpointCache.get(endpointUri);
+ if (endpoint == null) {
+ endpoint = context.getEndpoint(endpointUri);
+ if (endpoint != null) {
+ endpointCache.put(endpointUri, endpoint);
+ }
+ }
+ }
+ }
+ else {
+ endpoint = context.getEndpoint(endpointUri);
+ }
if (endpoint == null) {
throw new NoSuchEndpointException(endpointUri);
}
return endpoint;
+ }
+
+ protected Endpoint<E> getMandatoryDefaultEndpoint() {
+ Endpoint<E> answer = getDefaultEndpoint();
+ ObjectHelper.notNull(answer, "defaultEndpoint");
+ return answer;
}
protected void doStart() throws Exception {
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java Tue May 8 09:08:33 2007
@@ -39,6 +39,7 @@
}
protected void assertEndpointUri(Endpoint<Exchange> endpoint, String uri) {
+ assertNotNull("Endpoint is null when expecting endpoint for: " + uri, endpoint);
assertEquals("Endoint uri for: " + endpoint, uri, endpoint.getEndpointUri());
}
Modified: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java (original)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java Tue May 8 09:08:33 2007
@@ -17,26 +17,30 @@
*/
package org.apache.camel.spring;
+import org.aopalliance.intercept.MethodInvocation;
import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
-import org.apache.camel.Producer;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.Exchange;
import org.apache.camel.MessageDriven;
import org.apache.camel.Processor;
-import org.apache.camel.Consumer;
-import org.apache.camel.Exchange;
-import org.apache.camel.spring.util.ReflectionUtils;
+import org.apache.camel.Producer;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.CamelTemplate;
import org.apache.camel.spring.util.BeanInfo;
-import org.apache.camel.spring.util.MethodInvocationStrategy;
import org.apache.camel.spring.util.DefaultMethodInvocationStrategy;
+import org.apache.camel.spring.util.MethodInvocationStrategy;
+import org.apache.camel.spring.util.ReflectionUtils;
+import org.apache.camel.util.ObjectHelper;
+import static org.apache.camel.util.ObjectHelper.isNotNullOrBlank;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
-import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -54,12 +58,12 @@
private ApplicationContext applicationContext;
private MethodInvocationStrategy invocationStrategy = new DefaultMethodInvocationStrategy();
- public CamelBeanPostProcessor(CamelContext camelContext) {
- this.camelContext = camelContext;
+ public CamelBeanPostProcessor() {
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- injectBean(bean);
+ injectFields(bean);
+ injectMethods(bean);
return bean;
}
@@ -82,6 +86,14 @@
this.invocationStrategy = invocationStrategy;
}
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
// Implementation methods
//-------------------------------------------------------------------------
@@ -90,36 +102,47 @@
*
* @param bean the bean to be injected
*/
- protected void injectBean(final Object bean) {
+ protected void injectFields(final Object bean) {
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
EndpointInject annotation = field.getAnnotation(EndpointInject.class);
if (annotation != null) {
- ReflectionUtils.setField(field, bean, getEndpointInjectionValue(field, annotation));
+ ReflectionUtils.setField(field, bean, getEndpointInjectionValue(annotation, field.getType()));
}
}
});
}
- protected void evaluateCallbacks(final Object bean) {
+ protected void injectMethods(final Object bean) {
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
@SuppressWarnings("unchecked")
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
- MessageDriven annotation = method.getAnnotation(MessageDriven.class);
- if (annotation != null) {
- // lets bind this method to a listener
- Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name());
- if (endpoint != null) {
- try {
- Processor processor = createConsumerProcessor(bean, method, endpoint);
- Consumer consumer = endpoint.createConsumer(processor);
- addConsumer(consumer);
- }
- catch (Exception e) {
- throw new RuntimeCamelException(e);
- }
- }
+ setterInjection(method, bean);
+ consumerInjection(method, bean);
+ }
+ });
+ }
+
+ protected void setterInjection(Method method, Object bean) {
+ EndpointInject annoation = method.getAnnotation(EndpointInject.class);
+ if (annoation != null) {
+ 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 {
+ Object value = getEndpointInjectionValue(annoation, parameterTypes[0]);
+ ObjectHelper.invokeMethod(method, bean, value);
}
+ }
+ }
+ }
+
+ protected void consumerInjection(final Object bean) {
+ ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
+ @SuppressWarnings("unchecked")
+ public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
/*
TODO support callbacks?
@@ -150,6 +173,24 @@
});
}
+ protected void consumerInjection(Method method, Object bean) {
+ MessageDriven annotation = method.getAnnotation(MessageDriven.class);
+ if (annotation != null) {
+ // lets bind this method to a listener
+ Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name());
+ if (endpoint != null) {
+ try {
+ Processor processor = createConsumerProcessor(bean, method, endpoint);
+ Consumer consumer = endpoint.createConsumer(processor);
+ addConsumer(consumer);
+ }
+ catch (Exception e) {
+ throw new RuntimeCamelException(e);
+ }
+ }
+ }
+ }
+
/**
* Create a processor which invokes the given method when an incoming message exchange is received
*/
@@ -179,13 +220,13 @@
/**
* Creates the value for the injection point for the given annotation
*/
- protected Object getEndpointInjectionValue(Field field, EndpointInject annotation) {
+ protected Object getEndpointInjectionValue(EndpointInject annotation, Class<?> type) {
Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name());
if (endpoint != null) {
- if (field.getType().isInstance(endpoint)) {
+ if (type.isInstance(endpoint)) {
return endpoint;
}
- else if (field.getType().isAssignableFrom(Producer.class)) {
+ else if (type.isAssignableFrom(Producer.class)) {
try {
return endpoint.createProducer();
}
@@ -193,18 +234,27 @@
throw new RuntimeCamelException(e);
}
}
+ else if (type.isAssignableFrom(CamelTemplate.class)) {
+ return new CamelTemplate(getCamelContext(), endpoint);
+ }
}
return null;
}
protected Endpoint getEndpointInjection(String uri, String name) {
Endpoint endpoint = null;
- if (uri != null) {
+ if (isNotNullOrBlank(uri)) {
endpoint = camelContext.getEndpoint(uri);
}
else {
- if (name != null) {
+ if (isNotNullOrBlank(name)) {
endpoint = (Endpoint) applicationContext.getBean(name);
+ if (endpoint == null) {
+ throw new NoSuchBeanDefinitionException(name);
+ }
+ }
+ else {
+ log.warn("No uri or name specified on @EndpointInject annotation!");
}
}
return endpoint;
Modified: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java (original)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java Tue May 8 09:08:33 2007
@@ -2,6 +2,7 @@
import org.apache.camel.spring.CamelContextFactoryBean;
import org.apache.camel.spring.EndpointFactoryBean;
+import org.apache.camel.spring.CamelBeanPostProcessor;
import static org.apache.camel.util.ObjectHelper.isNotNullOrBlank;
import org.apache.camel.builder.xml.XPathBuilder;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -21,6 +22,7 @@
public class CamelNamespaceHandler extends NamespaceHandlerSupport {
protected CamelBeanDefinitionParser routesParser = new CamelBeanDefinitionParser(this);
protected BeanDefinitionParser endpointParser = new BeanDefinitionParser(EndpointFactoryBean.class);
+ protected BeanDefinitionParser beanPostProcessorParser = new BeanDefinitionParser(CamelBeanPostProcessor.class);
protected Set<String> parserElementNames = new HashSet<String>();
public void init() {
@@ -41,8 +43,17 @@
for (int size = list.getLength(), i = 0; i < size; i++) {
Node child = list.item(i);
if (child instanceof Element) {
- element.removeChild(child);
- routes.appendChild(child);
+ Element childElement = (Element) child;
+ if (child.getLocalName().equals("beanPostProcessor")) {
+ String beanPostProcessorId = contextId + ":beanPostProcessor";
+ childElement.setAttribute("id", beanPostProcessorId);
+ BeanDefinition definition = beanPostProcessorParser.parse(childElement, parserContext);
+ definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
+ }
+ else {
+ element.removeChild(child);
+ routes.appendChild(child);
+ }
}
}
String routeId = contextId + ":routes";
Modified: activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd (original)
+++ activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd Tue May 8 09:08:33 2007
@@ -28,6 +28,7 @@
<xs:complexContent>
<xs:extension base="s:identifiedType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
+ <xs:element minOccurs="0" maxOccurs="1" ref="c:beanPostProcessor"/>
<xs:element minOccurs="0" maxOccurs="1" ref="c:endpoint"/>
<xs:element minOccurs="0" maxOccurs="1" ref="c:route"/>
</xs:choice>
@@ -51,6 +52,16 @@
</xs:extension>
</xs:complexContent>
</xs:complexType>
+ </xs:element>
+
+ <xs:element name="beanPostProcessor">
+ <xs:annotation>
+ <xs:documentation><![CDATA[
+ Enables the Camel bean post processor for auto-injecting Camel resources into
+ beans which have the Camel annotations on them as well as auto-activating
+ @MessageDriven POJOs
+ ]]></xs:documentation>
+ </xs:annotation>
</xs:element>
<!--
Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBean.java?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBean.java (added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBean.java Tue May 8 09:08:33 2007
@@ -0,0 +1,88 @@
+/*
+ * 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.apache.camel.Endpoint;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Producer;
+import org.apache.camel.CamelTemplate;
+
+/**
+ * @version $Revision: $
+ */
+public class InjectedBean {
+
+ // Endpoint
+ //-----------------------------------------------------------------------
+ @EndpointInject(uri = "direct:fieldInjectedEndpoint")
+ private Endpoint fieldInjectedEndpoint;
+ private Endpoint propertyInjectedEndpoint;
+
+ public Endpoint getFieldInjectedEndpoint() {
+ return fieldInjectedEndpoint;
+ }
+
+ public Endpoint getPropertyInjectedEndpoint() {
+ return propertyInjectedEndpoint;
+ }
+
+ @EndpointInject(name = "namedEndpoint1")
+ public void setPropertyInjectedEndpoint(Endpoint propertyInjectedEndpoint) {
+ this.propertyInjectedEndpoint = propertyInjectedEndpoint;
+ }
+
+
+ // Producer
+ //-----------------------------------------------------------------------
+ @EndpointInject(uri = "direct:fieldInjectedProducer")
+ private Producer fieldInjectedProducer;
+ private Producer propertyInjectedProducer;
+
+
+ public Producer getFieldInjectedProducer() {
+ return fieldInjectedProducer;
+ }
+
+ public Producer getPropertyInjectedProducer() {
+ return propertyInjectedProducer;
+ }
+
+ @EndpointInject(uri = "direct:propertyInjectedProducer")
+ public void setPropertyInjectedProducer(Producer propertyInjectedProducer) {
+ this.propertyInjectedProducer = propertyInjectedProducer;
+ }
+
+
+ // CamelTemplate
+ //-----------------------------------------------------------------------
+ @EndpointInject(uri = "direct:fieldInjectedCamelTemplate")
+ private CamelTemplate fieldInjectedCamelTemplate;
+ private CamelTemplate propertyInjectedCamelTemplate;
+
+ public CamelTemplate getFieldInjectedCamelTemplate() {
+ return fieldInjectedCamelTemplate;
+ }
+
+ public CamelTemplate getPropertyInjectedCamelTemplate() {
+ return propertyInjectedCamelTemplate;
+ }
+
+ @EndpointInject(uri = "direct:propertyInjectedCamelTemplate")
+ public void setPropertyInjectedCamelTemplate(CamelTemplate propertyInjectedCamelTemplate) {
+ this.propertyInjectedCamelTemplate = propertyInjectedCamelTemplate;
+ }
+}
Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBeanTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBeanTest.java?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBeanTest.java (added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/InjectedBeanTest.java Tue May 8 09:08:33 2007
@@ -0,0 +1,62 @@
+/*
+ * 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.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision: $
+ */
+public class InjectedBeanTest extends SpringTestSupport {
+ protected InjectedBean bean;
+
+ public void testInjectionPoints() throws Exception {
+ log.info("getFieldInjectedEndpoint() = " + bean.getFieldInjectedEndpoint());
+ log.info("getPropertyInjectedEndpoint() = " + bean.getPropertyInjectedEndpoint());
+
+ log.info("getFieldInjectedProducer() = " + bean.getFieldInjectedProducer());
+ log.info("getPropertyInjectedProducer() = " + bean.getPropertyInjectedProducer());
+
+ log.info("getFieldInjectedCamelTemplate() = " + bean.getFieldInjectedCamelTemplate());
+ log.info("getPropertyInjectedCamelTemplate() = " + bean.getPropertyInjectedCamelTemplate());
+
+ assertEndpointUri(bean.getFieldInjectedEndpoint(), "direct:fieldInjectedEndpoint");
+ assertEndpointUri(bean.getPropertyInjectedEndpoint(), "direct:namedEndpoint1");
+
+ assertNotNull("No Producer injected for getFieldInjectedProducer()", bean.getFieldInjectedProducer());
+ assertNotNull("No Producer injected for getPropertyInjectedProducer()", bean.getPropertyInjectedProducer());
+
+ assertNotNull("No CamelTemplate injected for getFieldInjectedCamelTemplate()", bean.getFieldInjectedCamelTemplate());
+ assertNotNull("No CamelTemplate injected for getPropertyInjectedCamelTemplate()", bean.getPropertyInjectedCamelTemplate());
+ }
+
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ bean = getMandatoryBean(InjectedBean.class, "injectedBean");
+ }
+
+ protected ClassPathXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/injectedBeanTest.xml");
+ }
+
+ protected int getExpectedRouteCount() {
+ return 0;
+ }
+}
Modified: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java?view=diff&rev=536237&r1=536236&r2=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java (original)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java Tue May 8 09:08:33 2007
@@ -83,8 +83,12 @@
List<Route> routes = context.getRoutes();
assertNotNull("Should have some routes defined", routes);
- assertTrue("Should have at least one route", routes.size() > 0);
+ assertTrue("Should have at least one route", routes.size() >= getExpectedRouteCount());
log.debug("Camel Routes: " + routes);
+ }
+
+ protected int getExpectedRouteCount() {
+ return 1;
}
protected SpringCamelContext createCamelContext() throws Exception {
Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/MySender.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/MySender.java?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/MySender.java (added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/MySender.java Tue May 8 09:08:33 2007
@@ -0,0 +1,48 @@
+/*
+ * 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.example;
+
+import org.apache.camel.CamelTemplate;
+import org.apache.camel.EndpointInject;
+import static org.apache.camel.util.ObjectHelper.notNull;
+
+/**
+ * An example POJO which is injected with a CamelTemplate
+ *
+ * @version $Revision: $
+ */
+public class MySender {
+ @EndpointInject(uri="mock:a")
+ private CamelTemplate successDesetination;
+
+ @EndpointInject(uri="mock:b")
+ private CamelTemplate failureDesetination;
+
+ public void doSomething(String name) {
+ notNull(successDesetination, "successDesetination");
+ notNull(failureDesetination, "failureDesetination");
+
+ String body = "<hello>" + name + "</hello>";
+
+ if (name.equals("James")) {
+ successDesetination.sendBody(body);
+ }
+ else {
+ failureDesetination.sendBody(body);
+ }
+ }
+}
Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/PojoSenderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/PojoSenderTest.java?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/PojoSenderTest.java (added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/PojoSenderTest.java Tue May 8 09:08:33 2007
@@ -0,0 +1,68 @@
+/*
+ * 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.example;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision: $
+ */
+public class PojoSenderTest extends SpringTestSupport {
+ protected MockEndpoint matchedEndpoint;
+ protected MockEndpoint notMatchedEndpoint;
+ protected MySender mySender;
+
+ public void testMatchesPredicate() throws Exception {
+ matchedEndpoint.expectedMessageCount(1);
+ notMatchedEndpoint.expectedMessageCount(0);
+
+ mySender.doSomething("James");
+
+ assertIsSatisfied(matchedEndpoint, notMatchedEndpoint);
+ }
+
+ public void testDoesNotMatchPredicate() throws Exception {
+ matchedEndpoint.expectedMessageCount(0);
+ notMatchedEndpoint.expectedMessageCount(1);
+
+ mySender.doSomething("Rob");
+
+ assertIsSatisfied(matchedEndpoint, notMatchedEndpoint);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ matchedEndpoint = (MockEndpoint) resolveMandatoryEndpoint(camelContext, "mock:a");
+ notMatchedEndpoint = (MockEndpoint) resolveMandatoryEndpoint(camelContext, "mock:b");
+
+ mySender = getMandatoryBean(MySender.class, "mySender");
+ }
+
+ protected ClassPathXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/example/pojoSender.xml");
+ }
+
+ protected int getExpectedRouteCount() {
+ return 0;
+ }
+}
+
Added: activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoSender.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoSender.xml?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoSender.xml (added)
+++ activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/example/pojoSender.xml Tue May 8 09:08:33 2007
@@ -0,0 +1,32 @@
+<?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.0.xsd
+ http://activemq.apache.org/camel/schema/camel-1.0.xsd http://activemq.apache.org/camel/schema/camel-1.0.xsd
+ ">
+
+ <!-- START SNIPPET: example -->
+ <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/camel-1.0.xsd">
+ <beanPostProcessor/>
+ </camelContext>
+ <!-- END SNIPPET: example -->
+
+ <bean id="mySender" class="org.apache.camel.spring.example.MySender"/>
+</beans>
Added: activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/injectedBeanTest.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/injectedBeanTest.xml?view=auto&rev=536237
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/injectedBeanTest.xml (added)
+++ activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/injectedBeanTest.xml Tue May 8 09:08:33 2007
@@ -0,0 +1,31 @@
+<?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.0.xsd
+ http://activemq.apache.org/camel/schema/camel-1.0.xsd http://activemq.apache.org/camel/schema/camel-1.0.xsd
+ ">
+
+ <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/camel-1.0.xsd">
+ <beanPostProcessor/>
+ <endpoint id="namedEndpoint1" uri="direct:namedEndpoint1"/>
+ </camelContext>
+
+ <bean id="injectedBean" class="org.apache.camel.spring.InjectedBean"/>
+</beans>