Author: jstrachan
Date: Thu Aug 14 07:05:55 2008
New Revision: 685900
URL: http://svn.apache.org/viewvc?rev=685900&view=rev
Log:
refactored the ProxyHelper class a little to clean it up a bit and to also
allow a Producer to be passed in - then added a destroy() to shut down proxies
properly when they are completed
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ProxyHelper.java
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelProxyFactoryBean.java
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ProxyHelper.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ProxyHelper.java?rev=685900&r1=685899&r2=685900&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ProxyHelper.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ProxyHelper.java
Thu Aug 14 07:05:55 2008
@@ -16,11 +16,11 @@
*/
package org.apache.camel.component.bean;
-import java.lang.reflect.Proxy;
-
import org.apache.camel.Endpoint;
import org.apache.camel.Producer;
+import java.lang.reflect.Proxy;
+
/**
* A helper class for creating proxies which delegate to Camel
*
@@ -34,25 +34,24 @@
private ProxyHelper() {
}
+
/**
* Creates a Proxy which sends PojoExchange to the endpoint.
- *
- * @throws Exception
*/
- public static Object createProxy(final Endpoint endpoint, ClassLoader cl,
Class interfaces[])
- throws Exception {
- MethodInfoCache methodCache = new
MethodInfoCache(endpoint.getCamelContext());
- return createProxy(endpoint, cl, interfaces, methodCache);
+ @SuppressWarnings("unchecked")
+ public static Object createProxyObject(Endpoint endpoint, Producer
producer, ClassLoader classLoader, Class[] interfaces, MethodInfoCache
methodCache) {
+ return Proxy.newProxyInstance(classLoader, interfaces.clone(), new
CamelInvocationHandler(endpoint, producer, methodCache));
}
+
/**
* Creates a Proxy which sends PojoExchange to the endpoint.
*
* @throws Exception
*/
- public static Object createProxy(Endpoint endpoint, ClassLoader cl,
Class[] interfaces, MethodInfoCache methodCache) throws Exception {
- final Producer producer = endpoint.createProducer();
- return Proxy.newProxyInstance(cl, interfaces, new
CamelInvocationHandler(endpoint, producer, methodCache));
+ @SuppressWarnings("unchecked")
+ public static <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class[]
interfaces, MethodInfoCache methodCache) throws Exception {
+ return (T) createProxyObject(endpoint, endpoint.createProducer(), cl,
interfaces, methodCache);
}
/**
@@ -60,22 +59,20 @@
*
* @throws Exception
*/
- public static Object createProxy(Endpoint endpoint, Class interfaces[])
throws Exception {
- if (interfaces.length < 1) {
- throw new IllegalArgumentException("You must provide at least 1
interface class.");
- }
- return createProxy(endpoint, interfaces[0].getClassLoader(),
interfaces);
+ @SuppressWarnings("unchecked")
+ public static <T> T createProxy(Endpoint endpoint, ClassLoader cl,
Class<T>... interfaceClasses) throws Exception {
+ return (T) createProxy(endpoint, cl, interfaceClasses,
createMethodInfoCache(endpoint));
}
+
/**
* Creates a Proxy which sends PojoExchange to the endpoint.
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
- public static <T> T createProxy(Endpoint endpoint, ClassLoader cl,
Class<T> interfaceClass)
- throws Exception {
- return (T)createProxy(endpoint, cl, new Class[] {interfaceClass});
+ public static <T> T createProxy(Endpoint endpoint, Class<T>...
interfaceClasses) throws Exception {
+ return (T) createProxy(endpoint, getClassLoader(interfaceClasses),
interfaceClasses);
}
/**
@@ -84,8 +81,26 @@
* @throws Exception
*/
@SuppressWarnings("unchecked")
- public static <T> T createProxy(Endpoint endpoint, Class<T>
interfaceClass) throws Exception {
- return (T)createProxy(endpoint, new Class[] {interfaceClass});
+ public static <T> T createProxy(Endpoint endpoint, Producer producer,
Class<T>... interfaceClasses) throws Exception {
+ return (T) createProxyObject(endpoint, producer,
getClassLoader(interfaceClasses), interfaceClasses,
createMethodInfoCache(endpoint));
+ }
+
+
+ /**
+ * Returns the class loader of the first interface or throws [EMAIL
PROTECTED] IllegalArgumentException} if there are no interfaces specified
+ *
+ * @return
+ */
+ protected static ClassLoader getClassLoader(Class... interfaces) {
+ if (interfaces == null || interfaces.length < 1) {
+ throw new IllegalArgumentException("You must provide at least 1
interface class.");
+ }
+ return interfaces[0].getClassLoader();
+ }
+
+
+ protected static MethodInfoCache createMethodInfoCache(Endpoint endpoint) {
+ return new MethodInfoCache(endpoint.getCamelContext());
}
}
Modified:
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelProxyFactoryBean.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelProxyFactoryBean.java?rev=685900&r1=685899&r2=685900&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelProxyFactoryBean.java
(original)
+++
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelProxyFactoryBean.java
Thu Aug 14 07:05:55 2008
@@ -19,20 +19,22 @@
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Endpoint;
+import org.apache.camel.Producer;
import org.apache.camel.component.bean.ProxyHelper;
-
+import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.remoting.support.UrlBasedRemoteAccessor;
/**
* A [EMAIL PROTECTED] FactoryBean} to create a Proxy to a a Camel Pojo
Endpoint.
- *
+ *
* @author chirino
*/
-public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements
FactoryBean, CamelContextAware {
+public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements
FactoryBean, CamelContextAware, DisposableBean {
private CamelContext camelContext;
private Endpoint endpoint;
private Object serviceProxy;
+ private Producer producer;
@Override
public void afterPropertiesSet() {
@@ -49,12 +51,21 @@
}
}
- this.serviceProxy = ProxyHelper.createProxy(endpoint,
getServiceInterface());
+ this.producer = endpoint.createProducer();
+ this.producer.start();
+ this.serviceProxy = ProxyHelper.createProxy(endpoint, producer,
getServiceInterface());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
+ public void destroy() throws Exception {
+ this.producer.stop();
+ this.producer = null;
+ this.serviceProxy = null;
+ }
+
+
public Class getServiceInterface() {
return super.getServiceInterface();
}
@@ -90,4 +101,5 @@
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
+
}
Modified:
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java?rev=685900&r1=685899&r2=685900&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java
(original)
+++
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java
Thu Aug 14 07:05:55 2008
@@ -86,6 +86,7 @@
}
Endpoint endpoint =
CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
+ notNull(camelContext, "service");
Object proxy = getProxyForService();
consumer = endpoint.createConsumer(new BeanProcessor(proxy,
camelContext));