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 2019/04/25 10:33:17 UTC

[camel] 12/17: CAMEL-13449: camel3 - Move bean component out of camel-core

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch bean
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 958782532e286a5dc7cdd15515020a31386969b1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Apr 25 10:44:15 2019 +0200

    CAMEL-13449: camel3 - Move bean component out of camel-core
---
 .../camel-bean/src/main/docs/bean-component.adoc   | 23 +++----------
 ...oxyHelper.java => DefaultBeanProxyFactory.java} | 24 ++++---------
 .../bean/PojoMessageInvocationHandler.java         |  1 +
 .../camel/component/bean/PojoProxyHelper.java      |  1 +
 .../org/apache/camel/spi/BeanProxyFactory.java     | 39 ++++++++++++++++++++++
 .../org/apache/camel/builder/ProxyBuilder.java     | 11 +++---
 .../camel/impl/CamelPostProcessorHelper.java       | 11 ++++--
 7 files changed, 67 insertions(+), 43 deletions(-)

diff --git a/components/camel-bean/src/main/docs/bean-component.adoc b/components/camel-bean/src/main/docs/bean-component.adoc
index b4277f8..30b96b2 100644
--- a/components/camel-bean/src/main/docs/bean-component.adoc
+++ b/components/camel-bean/src/main/docs/bean-component.adoc
@@ -73,12 +73,9 @@ You can append query options to the URI in the following format,
 
 The object instance that is used to consume messages must be explicitly
 registered with the Registry. For example, if you
-are using Spring you must define the bean in the Spring configuration,
-`spring.xml`; or if you don't use Spring, by registering the bean in
-JNDI.
+are using Spring you must define the bean in the Spring configuration XML file.
 
-Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
-Index: 20, Size: 20
+You can also register beans manually via Camel's `Registry` with the `bind` method.
 
 Once an endpoint has been registered, you can build Camel routes that
 use it to process exchanges.
@@ -90,7 +87,7 @@ using a *direct:* or *queue:* endpoint as the input.
 
 You can use the `createProxy()` methods on
 http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/component/bean/ProxyHelper.html[ProxyHelper]
-to create a proxy that will generate BeanExchanges and send them to any
+to create a proxy that will generate exchanges and send them to any
 endpoint:
 
 And the same route using Spring DSL:
@@ -126,11 +123,11 @@ component. Instead of specifying the bean explicitly as the endpoint
 -------------------------------------------------------
 // Send message to the bean endpoint
 // and invoke method resolved using Bean Binding.
-from("direct:start").beanRef("beanName");
+from("direct:start").bean("beanName");
 
 // Send message to the bean endpoint
 // and invoke given method.
-from("direct:start").beanRef("beanName", "methodName");
+from("direct:start").bean("beanName", "methodName");
 -------------------------------------------------------
 
 Instead of passing name of the reference to the bean (so that Camel will
@@ -157,13 +154,3 @@ Bean Binding mechanism which is used throughout
 all of the various Bean Integration
 mechanisms in Camel.
 
-=== See Also
-
-* Configuring Camel
-* Component
-* Endpoint
-* Getting Started
-
-* <<class-component,Class>> component
-* Bean Binding
-* Bean Integration
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
similarity index 50%
copy from components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java
copy to components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
index 81d096c..421c86d 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
@@ -16,26 +16,14 @@
  */
 package org.apache.camel.component.bean;
 
-import java.lang.reflect.Proxy;
-
 import org.apache.camel.Endpoint;
-import org.apache.camel.Producer;
-import org.apache.camel.support.service.ServiceHelper;
-
-/**
- * Create a dynamic proxy for a given interface and endpoint that sends the parameter object to the endpoint and optionally
- * receives a reply. Unlike the ProxyHelper this works only with methods that have only one parameter.
- */
-public final class PojoProxyHelper {
+import org.apache.camel.spi.BeanProxyFactory;
 
-    private PojoProxyHelper() {
-    }
+public final class DefaultBeanProxyFactory implements BeanProxyFactory {
 
-    @SuppressWarnings("unchecked")
-    public static <T> T createProxy(Endpoint endpoint, Class<?>... interfaceClasses) throws Exception {
-        Producer producer = endpoint.createProducer();
-        // ensure the producer is started
-        ServiceHelper.startService(producer);
-        return (T)Proxy.newProxyInstance(ProxyHelper.getClassLoader(interfaceClasses), interfaceClasses.clone(), new PojoMessageInvocationHandler(endpoint, producer));
+    @SafeVarargs
+    @Override
+    public final <T> T createProxy(Endpoint endpoint, boolean binding, Class<T>... interfaceClasses) throws Exception {
+        return ProxyHelper.createProxy(endpoint, binding, interfaceClasses);
     }
 }
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoMessageInvocationHandler.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoMessageInvocationHandler.java
index 0c255f3..25bc5ab 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoMessageInvocationHandler.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoMessageInvocationHandler.java
@@ -29,6 +29,7 @@ import org.apache.camel.RuntimeCamelException;
  * that as a very open message format especially when combined with e.g. JAXB
  * serialization.
  */
+@Deprecated
 public class PojoMessageInvocationHandler extends AbstractCamelInvocationHandler {
 
     public PojoMessageInvocationHandler(Endpoint endpoint, Producer producer) {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java
index 81d096c..cb123a2 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/PojoProxyHelper.java
@@ -26,6 +26,7 @@ import org.apache.camel.support.service.ServiceHelper;
  * Create a dynamic proxy for a given interface and endpoint that sends the parameter object to the endpoint and optionally
  * receives a reply. Unlike the ProxyHelper this works only with methods that have only one parameter.
  */
+@Deprecated
 public final class PojoProxyHelper {
 
     private PojoProxyHelper() {
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProxyFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProxyFactory.java
new file mode 100644
index 0000000..2878ef0
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProxyFactory.java
@@ -0,0 +1,39 @@
+/*
+ * 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.spi;
+
+import org.apache.camel.Endpoint;
+
+/**
+ * A factory for creating a {@link java.lang.reflect.Proxy} for a bean.
+ * <p/>
+ * This requires to have camel-bean on the classpath.
+ */
+public interface BeanProxyFactory {
+
+    /**
+     * Creates a proxy bean facaded with the interfaces that when invoked will send the data as a message to a Camel endpoint.
+     *
+     * @param endpoint  the endpoint to send to when the proxy is invoked
+     * @param binding   whether to use bean parameter binding which would be needed if invoking a bean method with multiple parameters
+     * @param interfaceClasses the interface(s) to use as bean facade
+     * @throws Exception is thrown if error creating the proxy
+     * @return the created bean proxy
+     */
+    <T> T  createProxy(Endpoint endpoint, boolean binding, Class<T>... interfaceClasses) throws Exception;
+
+}
diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
index ef98fa6..7e0e759 100644
--- a/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
+++ b/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
@@ -18,7 +18,7 @@ package org.apache.camel.builder;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
-import org.apache.camel.component.bean.ProxyHelper;
+import org.apache.camel.spi.BeanProxyFactory;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -26,8 +26,6 @@ import org.apache.camel.util.ObjectHelper;
  */
 public final class ProxyBuilder {
 
-    // TODO: Move this to camel-bean
-
     private final CamelContext camelContext;
     private Endpoint endpoint;
     private boolean binding = true;
@@ -82,7 +80,12 @@ public final class ProxyBuilder {
      */
     public <T> T build(Class<T>... interfaceClasses) throws Exception {
         ObjectHelper.notNull(endpoint, "endpoint");
-        return ProxyHelper.createProxy(endpoint, binding, interfaceClasses);
+        // use proxy service
+        BeanProxyFactory factory = camelContext.hasService(BeanProxyFactory.class);
+        if (factory == null) {
+            throw new IllegalArgumentException("Cannot find BeanProxyFactory service. Make sure camel-bean is on the classpath.");
+        }
+        return factory.createProxy(endpoint, binding, interfaceClasses);
     }
 
 }
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
index 37d609c..467b85f 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
@@ -39,7 +39,7 @@ import org.apache.camel.ProxyInstantiationException;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.Service;
 import org.apache.camel.builder.DefaultFluentProducerTemplate;
-import org.apache.camel.component.bean.ProxyHelper;
+import org.apache.camel.spi.BeanProxyFactory;
 import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.IntrospectionSupport;
 import org.apache.camel.support.service.ServiceHelper;
@@ -229,6 +229,7 @@ public class CamelPostProcessorHelper implements CamelContextAware {
      * {@link org.apache.camel.EndpointInject} or
      * {@link org.apache.camel.Produce} injection point
      */
+    @SuppressWarnings("unchecked")
     public Object getInjectionValue(Class<?> type, String endpointUri, String endpointProperty,
             String injectionPointName, Object bean, String beanName, boolean binding) {
         if (type.isAssignableFrom(ProducerTemplate.class)) {
@@ -249,8 +250,12 @@ public class CamelPostProcessorHelper implements CamelContextAware {
                 } else if (type.isInterface()) {
                     // lets create a proxy
                     try {
-                        // TODO: Requires camel-bean so we need some kind of spi, and to lookup via camel-context/service etc
-                        return ProxyHelper.createProxy(endpoint, binding, type);
+                        // use proxy service
+                        BeanProxyFactory factory = camelContext.hasService(BeanProxyFactory.class);
+                        if (factory == null) {
+                            throw new IllegalArgumentException("Cannot find BeanProxyFactory service. Make sure camel-bean is on the classpath.");
+                        }
+                        return factory.createProxy(endpoint, binding, type);
                     } catch (Exception e) {
                         throw createProxyInstantiationRuntimeException(type, endpoint, e);
                     }