You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dh...@apache.org on 2014/06/21 04:19:44 UTC

git commit: Updated getApiProxy to pass invocation method and args to decide which API proxy to return

Repository: camel
Updated Branches:
  refs/heads/master f6114d52d -> 12945b849


Updated getApiProxy to pass invocation method and args to decide which API proxy to return


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/12945b84
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/12945b84
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/12945b84

Branch: refs/heads/master
Commit: 12945b849e1edfa2c8bec26c6e92a6054aef1aca
Parents: f6114d5
Author: Dhiraj Bokde <dh...@yahoo.com>
Authored: Fri Jun 20 19:19:38 2014 -0700
Committer: Dhiraj Bokde <dh...@yahoo.com>
Committed: Fri Jun 20 19:19:38 2014 -0700

----------------------------------------------------------------------
 .../util/component/AbstractApiConsumer.java     | 58 +++++++++++---------
 .../util/component/AbstractApiEndpoint.java     | 27 +++++++--
 .../util/component/AbstractApiProducer.java     |  8 ++-
 .../src/main/java/__name__Endpoint.java         |  5 +-
 4 files changed, 61 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/12945b84/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiConsumer.java b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiConsumer.java
index 44fad39..fca429f 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiConsumer.java
@@ -83,7 +83,7 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
         final Set<String> argNames = new HashSet<String>();
         argNames.addAll(propertiesHelper.getEndpointPropertyNames(endpoint.getConfiguration()));
 
-        interceptArgumentNames(argNames);
+        interceptPropertyNames(argNames);
 
         final String[] argNamesArray = argNames.toArray(new String[argNames.size()]);
         List<ApiMethod> filteredMethods = ApiMethodHelper.filterMethods(
@@ -105,20 +105,16 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
         return result;
     }
 
-    /**
-     * Intercept argument names used to find consumer method.
-     * Used to add any custom/hidden method arguments, which MUST be provided in getMethodArguments() override.
-     * @param argNames argument names.
-     */
-    @SuppressWarnings("unused")
-    protected void interceptArgumentNames(Set<String> argNames) {
-        // do nothing by default
-    }
-
     @Override
     protected int poll() throws Exception {
         // invoke the consumer method
-        final Map<String, Object> args = getMethodArguments();
+        final Map<String, Object> args = new HashMap<String, Object>();
+        args.putAll(endpointProperties);
+
+        // let the endpoint and the Consumer intercept properties
+        endpoint.interceptProperties(args);
+        interceptProperties(args);
+
         try {
             Object result = doInvokeMethod(args);
 
@@ -141,6 +137,26 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
     }
 
     /**
+     * Intercept property names used to find Consumer method.
+     * Used to add any custom/hidden method arguments, which MUST be provided in interceptProperties() override.
+     * @param propertyNames argument names.
+     */
+    @SuppressWarnings("unused")
+    protected void interceptPropertyNames(Set<String> propertyNames) {
+        // do nothing by default
+    }
+
+    /**
+     * Intercept method invocation arguments used to find and invoke API method.
+     * Can be overridden to add custom/hidden method arguments.
+     * @param properties method invocation arguments.
+     */
+    @SuppressWarnings("unused")
+    protected void interceptProperties(Map<String, Object> properties) {
+        // do nothing by default
+    }
+
+    /**
      * Invoke the API method.
      * This method can be overridden, for example to synchronize API calls for thread-unsafe proxies.
      * Derived class MUST call super.doInvokeMethod() to invoke the API method.
@@ -148,14 +164,14 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
      * @return method invocation result.
      */
     protected Object doInvokeMethod(Map<String, Object> args) {
-        return ApiMethodHelper.invokeMethod(endpoint.getApiProxy(), method, args);
+        return ApiMethodHelper.invokeMethod(endpoint.getApiProxy(method, args), method, args);
     }
 
     private void processResult(Object result) throws Exception {
         Exchange exchange = getEndpoint().createExchange();
         exchange.getIn().setBody(result);
 
-        doProcessResult(exchange);
+        interceptResult(exchange);
         try {
             // send message to next processor in the route
             getProcessor().process(exchange);
@@ -173,7 +189,7 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
      * @param resultExchange result as a Camel exchange.
      */
     @SuppressWarnings("unused")
-    protected void doProcessResult(Exchange resultExchange) {
+    protected void interceptResult(Exchange resultExchange) {
         // do nothing by default
     }
 
@@ -186,16 +202,4 @@ public abstract class AbstractApiConsumer<E extends Enum<E> & ApiName, T> extend
         Collection<?> collection = (Collection<?>) result;
         return collection.toArray(new Object[collection.size()]);
     }
-
-    /**
-     * Return method arguments to use in doInvokeMethod().
-     * Derived classes can override it to add custom arguments.
-     * Overriding method MUST first call super.getMethodArguments() to get endpoint properties.
-     * @return argument names mapped to argument values
-     */
-    protected Map<String, Object> getMethodArguments() {
-        Map<String, Object> arguments = new HashMap<String, Object>();
-        arguments.putAll(endpointProperties);
-        return arguments;
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/12945b84/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiEndpoint.java b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiEndpoint.java
index 2a4ef13..eb703a0 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiEndpoint.java
@@ -112,7 +112,7 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T> extends DefaultE
         final Set<String> arguments = new HashSet<String>();
         arguments.addAll(getPropertiesHelper().getEndpointPropertyNames(getConfiguration()));
 
-        interceptEndpointArguments(arguments);
+        interceptPropertyNames(arguments);
 
         // add inBody argument for producers
         if (inBody != null) {
@@ -141,11 +141,23 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T> extends DefaultE
     }
 
     /**
-     * Intercept initial endpoint arguments to add custom/hidden arguments for method calls, etc.
-     * @param arguments argument names
+     * Intercept property names used to find Consumer and Producer methods.
+     * Used to add any custom/hidden method arguments, which MUST be provided in interceptProperties() override
+     * either in Endpoint, or Consumer and Producer.
+     * @param propertyNames argument names.
      */
     @SuppressWarnings("unused")
-    protected void interceptEndpointArguments(Set<String> arguments) {
+    protected void interceptPropertyNames(Set<String> propertyNames) {
+        // do nothing by default
+    }
+
+    /**
+     * Intercept method invocation arguments used to find and invoke API method. Called by Consumer and Producer.
+     * Must be overridden if also overriding interceptPropertyName() to add custom/hidden method properties.
+     * @param properties method invocation arguments.
+     */
+    @SuppressWarnings("unused")
+    protected void interceptProperties(Map<String, Object> properties) {
         // do nothing by default
     }
 
@@ -214,11 +226,14 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T> extends DefaultE
     }
 
     /**
-     * Returns an instance of an API Proxy based on apiName.
+     * Returns an instance of an API Proxy based on apiName, method and args.
      * Called by {@link AbstractApiConsumer} or {@link AbstractApiProducer}.
+     *
+     * @param method method about to be invoked
+     * @param args method arguments
      * @return a Java object that implements the method to be invoked.
      * @see AbstractApiProducer
      * @see AbstractApiConsumer
      */
-    public abstract Object getApiProxy();
+    public abstract Object getApiProxy(ApiMethod method, Map<String, Object> args);
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/12945b84/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiProducer.java b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiProducer.java
index cb0d7f1..55e551f 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/AbstractApiProducer.java
@@ -67,6 +67,8 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T> extend
         propertiesHelper.getEndpointProperties(endpoint.getConfiguration(), properties);
         propertiesHelper.getExchangeProperties(exchange, properties);
 
+        // let the endpoint and the Producer intercept properties
+        endpoint.interceptProperties(properties);
         interceptProperties(properties);
 
         // decide which method to invoke
@@ -110,7 +112,7 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T> extend
 
     /**
      * Intercept method invocation arguments used to find and invoke API method.
-     * Can be overridden to add custom method properties.
+     * Can be overridden to add custom/hidden method arguments.
      * @param properties method invocation arguments.
      */
     @SuppressWarnings("unused")
@@ -126,7 +128,7 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T> extend
      * @throws RuntimeCamelException on error. Exceptions thrown by API method are wrapped.
      */
     protected Object doInvokeMethod(ApiMethod method, Map<String, Object> properties) throws RuntimeCamelException {
-        return ApiMethodHelper.invokeMethod(endpoint.getApiProxy(), method, properties);
+        return ApiMethodHelper.invokeMethod(endpoint.getApiProxy(method, properties), method, properties);
     }
 
     /**
@@ -174,7 +176,7 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T> extend
 
             Object value = exchange.getIn().getBody();
             try {
-                value = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(
+                value = endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(
                         endpoint.getConfiguration().getClass().getDeclaredField(inBodyProperty).getType(),
                         exchange, value);
             } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/camel/blob/12945b84/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/src/main/java/__name__Endpoint.java
----------------------------------------------------------------------
diff --git a/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/src/main/java/__name__Endpoint.java b/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/src/main/java/__name__Endpoint.java
index dec1ba3..2db480e 100644
--- a/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/src/main/java/__name__Endpoint.java
+++ b/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/src/main/java/__name__Endpoint.java
@@ -16,11 +16,14 @@
 ## ------------------------------------------------------------------------
 package ${package};
 
+import java.util.Map;
+
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.util.component.AbstractApiEndpoint;
+import org.apache.camel.util.component.ApiMethod;
 import org.apache.camel.util.component.ApiMethodPropertiesHelper;
 
 import ${package}.api.${name}FileHello;
@@ -80,7 +83,7 @@ public class ${name}Endpoint extends AbstractApiEndpoint<${name}ApiName, ${name}
     }
 
     @Override
-    public Object getApiProxy() {
+    public Object getApiProxy(ApiMethod method, Map<String, Object> args) {
         return apiProxy;
     }
 }