Author: jstrachan
Date: Tue Oct 21 11:34:15 2008
New Revision: 706711
URL: http://svn.apache.org/viewvc?rev=706711&view=rev
Log:
initial refactor to more easily support CAMEL-1015 by reusing most of the same
code
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java
(with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java?rev=706711&view=auto
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java
(added)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java
Tue Oct 21 11:34:15 2008
@@ -0,0 +1,175 @@
+/**
+ *
+ * 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 org.apache.camel.MessageDriven;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.Consume;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.Consumer;
+import org.apache.camel.Service;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Producer;
+import org.apache.camel.PollingConsumer;
+import org.apache.camel.component.bean.BeanProcessor;
+import org.apache.camel.component.bean.ProxyHelper;
+import org.apache.camel.util.CamelContextHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.BeanInstantiationException;
+
+import javax.xml.bind.annotation.XmlTransient;
+import java.lang.reflect.Method;
+
+/**
+ * A helper class for Camel based injector or post processing hooks which can
be reused by
+ * both the <a href="http://activemq.apache.org/camel/spring.html">Spring</a>
+ * and <a href="http://activemq.apache.org/camel/guice.html">Guice</a> support.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class CamelPostProcessorSupport implements CamelContextAware {
+ private static final transient Log LOG =
LogFactory.getLog(CamelPostProcessorSupport.class);
+
+ @XmlTransient
+ private CamelContext camelContext;
+
+ public CamelPostProcessorSupport(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ public void consumerInjection(Method method, Object bean) {
+ MessageDriven annotation = method.getAnnotation(MessageDriven.class);
+ if (annotation != null) {
+ LOG.info("Creating a consumer for: " + annotation);
+ subscribeMethod(method, bean, annotation.uri(), annotation.name());
+ }
+
+ Consume consume = method.getAnnotation(Consume.class);
+ if (consume != null) {
+ LOG.info("Creating a consumer for: " + consume);
+ subscribeMethod(method, bean, consume.uri(), consume.ref());
+ }
+ }
+
+ protected void subscribeMethod(Method method, Object bean, String
endpointUri, String endpointName) {
+ // lets bind this method to a listener
+ String injectionPointName = method.getName();
+ Endpoint endpoint = getEndpointInjection(endpointUri, endpointName,
injectionPointName);
+ if (endpoint != null) {
+ try {
+ Processor processor = createConsumerProcessor(bean, method,
endpoint);
+ LOG.info("Created processor: " + processor);
+ Consumer consumer = endpoint.createConsumer(processor);
+ startService(consumer);
+ } catch (Exception e) {
+ LOG.warn(e);
+ throw
org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(e);
+ }
+ }
+ }
+
+ public void startService(Service service) throws Exception {
+ if (camelContext instanceof DefaultCamelContext) {
+ DefaultCamelContext defaultCamelContext = (DefaultCamelContext)
camelContext;
+ defaultCamelContext.addService(service);
+ }
+ else {
+ service.start();
+ }
+ }
+
+ /**
+ * Create a processor which invokes the given method when an incoming
+ * message exchange is received
+ */
+ protected Processor createConsumerProcessor(final Object pojo, final
Method method, final Endpoint endpoint) {
+ BeanProcessor answer = new BeanProcessor(pojo, getCamelContext());
+ answer.setMethodObject(method);
+ return answer;
+ }
+
+ protected Endpoint getEndpointInjection(String uri, String name, String
injectionPointName) {
+ return CamelContextHelper.getEndpointInjection(camelContext, uri,
name, injectionPointName);
+ }
+
+ /**
+ * Creates the object to be injected for an [EMAIL PROTECTED]
org.apache.camel.EndpointInject} or [EMAIL PROTECTED] org.apache.camel.Produce}
injection point
+ */
+ public Object getInjectionValue(Class<?> type, String endpointUri, String
endpointRef, String injectionPointName) {
+ Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef,
injectionPointName);
+ if (endpoint != null) {
+ if (type.isInstance(endpoint)) {
+ return endpoint;
+ } else if (type.isAssignableFrom(Producer.class)) {
+ return createInjectionProducer(endpoint);
+ } else if (type.isAssignableFrom(DefaultProducerTemplate.class)) {
+ return new DefaultProducerTemplate(getCamelContext(),
endpoint);
+ } else if (type.isAssignableFrom(PollingConsumer.class)) {
+ return createInjectionPollingConsumer(endpoint);
+ } else if (type.isInterface()) {
+ // lets create a proxy
+ try {
+ return ProxyHelper.createProxy(endpoint, type);
+ } catch (Exception e) {
+ throw new BeanInstantiationException(type, "Could not
instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
+ }
+ } else {
+ throw new IllegalArgumentException("Invalid type: " +
type.getName() + " which cannot be injected via @EndpointInject for " +
endpoint);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Factory method to create a started [EMAIL PROTECTED]
org.apache.camel.PollingConsumer} to be injected
+ * into a POJO
+ */
+ protected PollingConsumer createInjectionPollingConsumer(Endpoint
endpoint) {
+ try {
+ PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
+ startService(pollingConsumer);
+ return pollingConsumer;
+ } catch (Exception e) {
+ throw
org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(e);
+ }
+ }
+
+ /**
+ * A Factory method to create a started [EMAIL PROTECTED]
org.apache.camel.Producer} to be injected into
+ * a POJO
+ */
+ protected Producer createInjectionProducer(Endpoint endpoint) {
+ try {
+ Producer producer = endpoint.createProducer();
+ startService(producer);
+ return producer;
+ } catch (Exception e) {
+ throw
org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(e);
+ }
+ }
+}
Propchange:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java?rev=706711&r1=706710&r2=706711&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
Tue Oct 21 11:34:15 2008
@@ -30,6 +30,9 @@
import org.apache.camel.spi.Language;
import org.apache.camel.spi.Registry;
import static org.apache.camel.util.ObjectHelper.notNull;
+import static org.apache.camel.util.ObjectHelper.isNotNullAndNonEmpty;
+import static org.apache.camel.util.ObjectHelper.isNullOrBlank;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
/**
* A number of helper methods
@@ -182,4 +185,21 @@
}
return expression;
}
+
+ /**
+ * Evaluates the @EndpointInject annotation using the given context
+ */
+ public static Endpoint getEndpointInjection(CamelContext camelContext,
String uri, String name, String injectionPointName) {
+ Endpoint endpoint = null;
+ if (isNotNullAndNonEmpty(uri)) {
+ endpoint = camelContext.getEndpoint(uri);
+ } else {
+ if (isNullOrBlank(name)) {
+ name = injectionPointName;
+ }
+ endpoint = mandatoryLookup(camelContext, name, Endpoint.class);
+ }
+ return endpoint;
+ }
+
}
Modified:
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java?rev=706711&r1=706710&r2=706711&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
(original)
+++
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
Tue Oct 21 11:34:15 2008
@@ -24,33 +24,20 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
-import org.apache.camel.CamelContextAware;
-import org.apache.camel.Consume;
-import org.apache.camel.Consumer;
-import org.apache.camel.Endpoint;
-import org.apache.camel.EndpointInject;
-import org.apache.camel.MessageDriven;
-import org.apache.camel.PollingConsumer;
-import org.apache.camel.Processor;
-import org.apache.camel.Produce;
-import org.apache.camel.Producer;
-import org.apache.camel.Service;
-import org.apache.camel.component.bean.BeanProcessor;
+import org.apache.camel.*;
import org.apache.camel.component.bean.ProxyHelper;
import org.apache.camel.impl.DefaultProducerTemplate;
+import org.apache.camel.impl.CamelPostProcessorSupport;
import org.apache.camel.spring.util.ReflectionUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanInstantiationException;
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 static org.apache.camel.util.ObjectHelper.isNotNullAndNonEmpty;
-import static org.apache.camel.util.ObjectHelper.isNullOrBlank;
import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
/**
@@ -78,6 +65,8 @@
private SpringCamelContext camelContext;
@XmlTransient
private ApplicationContext applicationContext;
+ @XmlTransient
+ private CamelPostProcessorSupport postProcessor;
public CamelBeanPostProcessor() {
}
@@ -113,6 +102,7 @@
public void setCamelContext(SpringCamelContext camelContext) {
this.camelContext = camelContext;
+ postProcessor = new CamelPostProcessorSupport(camelContext);
}
// Implementation methods
@@ -140,7 +130,7 @@
}
protected void injectField(Field field, String endpointUri, String
endpointRef, Object bean) {
- ReflectionUtils.setField(field, bean,
getInjectionValue(field.getType(), endpointUri, endpointRef, field.getName()));
+ ReflectionUtils.setField(field, bean,
getPostProcessor().getInjectionValue(field.getType(), endpointUri, endpointRef,
field.getName()));
}
protected void injectMethods(final Object bean) {
@@ -148,7 +138,7 @@
@SuppressWarnings("unchecked")
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
setterInjection(method, bean);
- consumerInjection(method, bean);
+ getPostProcessor().consumerInjection(method, bean);
}
});
}
@@ -171,14 +161,15 @@
LOG.warn("Ignoring badly annotated method for injection due to
incorrect number of parameters: " + method);
} else {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = getInjectionValue(parameterTypes[0],
endpointUri, endpointRef, propertyName);
+ Object value =
getPostProcessor().getInjectionValue(parameterTypes[0], endpointUri,
endpointRef, propertyName);
ObjectHelper.invokeMethod(method, bean, value);
}
}
}
+
protected void consumerInjection(final Object bean) {
- ReflectionUtils.doWithMethods(bean.getClass(), new
ReflectionUtils.MethodCallback() {
+
org.springframework.util.ReflectionUtils.doWithMethods(bean.getClass(), new
org.springframework.util.ReflectionUtils.MethodCallback() {
@SuppressWarnings("unchecked")
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
/*
@@ -202,122 +193,8 @@
});
}
- protected void consumerInjection(Method method, Object bean) {
- MessageDriven annotation = method.getAnnotation(MessageDriven.class);
- if (annotation != null) {
- LOG.info("Creating a consumer for: " + annotation);
- subscribeMethod(method, bean, annotation.uri(), annotation.name());
- }
-
- Consume consume = method.getAnnotation(Consume.class);
- if (consume != null) {
- LOG.info("Creating a consumer for: " + consume);
- subscribeMethod(method, bean, consume.uri(), consume.ref());
- }
+ public CamelPostProcessorSupport getPostProcessor() {
+ ObjectHelper.notNull(postProcessor, "postProcessor");
+ return postProcessor;
}
-
- protected void subscribeMethod(Method method, Object bean, String
endpointUri, String endpointName) {
- // lets bind this method to a listener
- String injectionPointName = method.getName();
- Endpoint endpoint = getEndpointInjection(endpointUri, endpointName,
injectionPointName);
- if (endpoint != null) {
- try {
- Processor processor = createConsumerProcessor(bean, method,
endpoint);
- LOG.info("Created processor: " + processor);
- Consumer consumer = endpoint.createConsumer(processor);
- startService(consumer);
- } catch (Exception e) {
- LOG.warn(e);
- throw wrapRuntimeCamelException(e);
- }
- }
- }
-
- protected void startService(Service service) throws Exception {
- camelContext.addService(service);
- }
-
- /**
- * Create a processor which invokes the given method when an incoming
- * message exchange is received
- */
- protected Processor createConsumerProcessor(final Object pojo, final
Method method, final Endpoint endpoint) {
- BeanProcessor answer = new BeanProcessor(pojo, getCamelContext());
- answer.setMethodObject(method);
- return answer;
- }
-
-
- /**
- * Creates the object to be injected for an [EMAIL PROTECTED]
org.apache.camel.EndpointInject} or [EMAIL PROTECTED] Produce} injection point
- */
- protected Object getInjectionValue(Class<?> type, String endpointUri,
String endpointRef, String injectionPointName) {
- Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef,
injectionPointName);
- if (endpoint != null) {
- if (type.isInstance(endpoint)) {
- return endpoint;
- } else if (type.isAssignableFrom(Producer.class)) {
- return createInjectionProducer(endpoint);
- } else if (type.isAssignableFrom(DefaultProducerTemplate.class)) {
- return new DefaultProducerTemplate(getCamelContext(),
endpoint);
- } else if (type.isAssignableFrom(PollingConsumer.class)) {
- return createInjectionPollingConsumer(endpoint);
- } else if (type.isInterface()) {
- // lets create a proxy
- try {
- return ProxyHelper.createProxy(endpoint, type);
- } catch (Exception e) {
- throw new BeanInstantiationException(type, "Could not
instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
- }
- } else {
- throw new IllegalArgumentException("Invalid type: " +
type.getName() + " which cannot be injected via @EndpointInject for " +
endpoint);
- }
- }
- return null;
- }
-
- /**
- * Factory method to create a started [EMAIL PROTECTED] PollingConsumer}
to be injected
- * into a POJO
- */
- protected PollingConsumer createInjectionPollingConsumer(Endpoint
endpoint) {
- try {
- PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
- startService(pollingConsumer);
- return pollingConsumer;
- } catch (Exception e) {
- throw wrapRuntimeCamelException(e);
- }
- }
-
- /**
- * A Factory method to create a started [EMAIL PROTECTED] Producer} to be
injected into
- * a POJO
- */
- protected Producer createInjectionProducer(Endpoint endpoint) {
- try {
- Producer producer = endpoint.createProducer();
- startService(producer);
- return producer;
- } catch (Exception e) {
- throw wrapRuntimeCamelException(e);
- }
- }
-
- protected Endpoint getEndpointInjection(String uri, String name, String
injectionPointName) {
- Endpoint endpoint = null;
- if (isNotNullAndNonEmpty(uri)) {
- endpoint = camelContext.getEndpoint(uri);
- } else {
- if (isNullOrBlank(name)) {
- name = injectionPointName;
- }
- endpoint = (Endpoint) applicationContext.getBean(name);
- if (endpoint == null) {
- throw new NoSuchBeanDefinitionException(name);
- }
- }
- return endpoint;
- }
-
}