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 2017/03/15 22:17:55 UTC

camel git commit: CAMEL-10799: camel-connector - Generate spring boot auto configuration

Repository: camel
Updated Branches:
  refs/heads/master 323279362 -> 3e87b23e8


CAMEL-10799: camel-connector - Generate spring boot auto configuration


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

Branch: refs/heads/master
Commit: 3e87b23e8b60b718e6a122e02c7ac325b664866d
Parents: 3232793
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Mar 15 23:17:45 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Mar 15 23:17:45 2017 +0100

----------------------------------------------------------------------
 .../SpringBootAutoConfigurationMojo.java        |   2 +-
 .../connector/DefaultConnectorComponent.java    | 132 +++++++++++--------
 2 files changed, 76 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3e87b23e/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/SpringBootAutoConfigurationMojo.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/SpringBootAutoConfigurationMojo.java b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/SpringBootAutoConfigurationMojo.java
index 8aad172..4de96e1 100644
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/SpringBootAutoConfigurationMojo.java
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/SpringBootAutoConfigurationMojo.java
@@ -185,7 +185,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
             String type = option.getJavaType();
             PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
 
-            // TODO: only include the component options so we can configure them
+            // TODO: only include the component options the connector has defined
 
             if ("true".equals(option.getDeprecated())) {
                 prop.getField().addAnnotation(Deprecated.class);

http://git-wip-us.apache.org/repos/asf/camel/blob/3e87b23e/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/DefaultConnectorComponent.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/DefaultConnectorComponent.java b/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/DefaultConnectorComponent.java
index 182d120..153f24a 100644
--- a/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/DefaultConnectorComponent.java
+++ b/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/DefaultConnectorComponent.java
@@ -19,7 +19,6 @@ package org.apache.camel.component.connector;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.net.URISyntaxException;
-import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
@@ -57,7 +56,7 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        Map<String, String> options = buildEnpointOptions(remaining, parameters);
+        Map<String, String> options = buildEndpointOptions(remaining, parameters);
 
         // clean-up parameters so that validation won't fail later on
         // in DefaultConnectorComponent.validateParameters()
@@ -65,44 +64,17 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
         
         String scheme = model.getBaseScheme();
 
-        // if we have component options then we need to create a new instance of the component
-        // which we then configure before it create the endpoint
-        if (componentOptions != null && !componentOptions.isEmpty()) {
-            String baseClassName = model.getBaseJavaType();
-            if (baseClassName != null) {
-                Class<?> type = Class.forName(baseClassName);
-                Constructor ctr = getPublicDefaultConstructor(type);
-                if (ctr != null) {
-                    // call default no-arg constructor
-                    Object base = ctr.newInstance();
-                    // configure component with extra options
-                    Map<String, Object> copy = new HashMap<>(componentOptions);
-                    IntrospectionSupport.setProperties(getCamelContext(), getCamelContext().getTypeConverter(), base, copy);
-
-                    if (base instanceof Component) {
-                        getCamelContext().removeComponent(scheme);
-                        // ensure component is started and stopped when Camel shutdown
-                        getCamelContext().addService(base, true, true);
-                        getCamelContext().addComponent(scheme, (Component) base);
-                    }
-                }
-            }
-        }
+        // now create the endpoint instance which either happens with a new
+        // base component which has been pre-configured for this connector
+        // or we fallback and use the default component in the camel context
+        createNewBaseComponent(scheme);
 
+        // create the uri of the base component
         String delegateUri = createEndpointUri(scheme, options);
-
-        log.debug("Connector resolved: {} -> {}", uri, delegateUri);
         Endpoint delegate = getCamelContext().getEndpoint(delegateUri);
-        return new DefaultConnectorEndpoint(uri, this, delegate, model.getInputDataType(), model.getOutputDataType());
-    }
+        log.debug("Connector resolved: {} -> {}", uri, delegateUri);
 
-    private static Constructor getPublicDefaultConstructor(Class<?> clazz) {
-        for (Constructor ctr : clazz.getConstructors()) {
-            if (Modifier.isPublic(ctr.getModifiers()) && ctr.getParameterCount() == 0) {
-                return ctr;
-            }
-        }
-        return null;
+        return new DefaultConnectorEndpoint(uri, this, delegate, model.getInputDataType(), model.getOutputDataType());
     }
 
     @Override
@@ -159,7 +131,7 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
                     // A little nasty hack required as verifier uses Map<String, Object>
                     // to be compatible with all the methods in CamelContext whereas
                     // catalog deals with Map<String, String>
-                    options = (Map)buildEnpointOptions(null, map);
+                    options = (Map) buildEndpointOptions(null, map);
                 } catch (URISyntaxException e) {
                     // If a failure is detected while reading the catalog, wrap it
                     // and stop the validation step.
@@ -195,6 +167,12 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
         if (model.getOutputDataType() == null) {
             throw new IllegalArgumentException("Camel connector must have outputDataType defined in camel-connector.json file");
         }
+        if (model.getBaseScheme() == null) {
+            throw new IllegalArgumentException("Camel connector must have baseSchema defined in camel-connector.json file");
+        }
+        if (model.getBaseJavaType() == null) {
+            throw new IllegalArgumentException("Camel connector must have baseJavaType defined in camel-connector.json file");
+        }
 
         // it may be a custom component so we need to register this in the camel catalog also
         String scheme = model.getBaseScheme();
@@ -203,25 +181,6 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
             catalog.addComponent(scheme, javaType);
         }
 
-        // the connector may have default values for the component level also
-        // and if so we need to prepare these values and set on this component before we can start
-        Map<String, String> defaultOptions = model.getDefaultComponentOptions();
-
-        if (!defaultOptions.isEmpty()) {
-            Map<String, Object> parameters = new LinkedHashMap<>();
-            for (Map.Entry<String, String> entry : defaultOptions.entrySet()) {
-                String key = entry.getKey();
-                String value = entry.getValue();
-                if (value != null) {
-                    // also support {{ }} placeholders so resolve those first
-                    value = getCamelContext().resolvePropertyPlaceholders(value);
-                    log.debug("Using component option: {}={}", key, value);
-                    parameters.put(key, value);
-                }
-            }
-            IntrospectionSupport.setProperties(getCamelContext(), getCamelContext().getTypeConverter(), this, parameters);
-        }
-
         log.debug("Starting connector: {}", componentName);
         super.doStart();
     }
@@ -236,7 +195,57 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
     // Helpers
     // ***************************************
 
-    private Map<String, String> buildEnpointOptions(String remaining, Map<String, Object> parameters) throws URISyntaxException {
+    private Component createNewBaseComponent(String scheme) throws Exception {
+        String baseClassName = model.getBaseJavaType();
+
+        if (baseClassName != null) {
+            // create a new instance of this base component
+            Class<?> type = Class.forName(baseClassName);
+            Constructor ctr = getPublicDefaultConstructor(type);
+            if (ctr != null) {
+                // call default no-arg constructor
+                Object base = ctr.newInstance();
+
+                // the connector may have default values for the component level also
+                // and if so we need to prepare these values and set on this component before we can start
+                Map<String, String> defaultOptions = model.getDefaultComponentOptions();
+
+                if (!defaultOptions.isEmpty()) {
+                    Map<String, Object> copy = new LinkedHashMap<>();
+                    for (Map.Entry<String, String> entry : defaultOptions.entrySet()) {
+                        String key = entry.getKey();
+                        String value = entry.getValue();
+                        if (value != null) {
+                            // also support {{ }} placeholders so resolve those first
+                            value = getCamelContext().resolvePropertyPlaceholders(value);
+                            log.debug("Using component option: {}={}", key, value);
+                            copy.put(key, value);
+                        }
+                    }
+                    IntrospectionSupport.setProperties(getCamelContext(), getCamelContext().getTypeConverter(), base, copy);
+                }
+
+                // configure component with extra options
+                if (componentOptions != null && !componentOptions.isEmpty()) {
+                    Map<String, Object> copy = new LinkedHashMap<>(componentOptions);
+                    IntrospectionSupport.setProperties(getCamelContext(), getCamelContext().getTypeConverter(), base, copy);
+                }
+
+                if (base instanceof Component) {
+                    getCamelContext().removeComponent(scheme);
+                    // ensure component is started and stopped when Camel shutdown
+                    getCamelContext().addService(base, true, true);
+                    getCamelContext().addComponent(scheme, (Component) base);
+
+                    return (Component) base;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private Map<String, String> buildEndpointOptions(String remaining, Map<String, Object> parameters) throws URISyntaxException {
         String scheme = model.getBaseScheme();
         Map<String, String> defaultOptions = model.getDefaultEndpointOptions();
 
@@ -268,5 +277,14 @@ public abstract class DefaultConnectorComponent extends DefaultComponent impleme
 
         return options;
     }
+
+    private static Constructor getPublicDefaultConstructor(Class<?> clazz) {
+        for (Constructor ctr : clazz.getConstructors()) {
+            if (Modifier.isPublic(ctr.getModifiers()) && ctr.getParameterCount() == 0) {
+                return ctr;
+            }
+        }
+        return null;
+    }
 }