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/02/23 10:14:09 UTC

camel git commit: CAMEL-10721: camel-connector improvements

Repository: camel
Updated Branches:
  refs/heads/master 3148b5934 -> f4db9b0bf


CAMEL-10721: camel-connector improvements


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

Branch: refs/heads/master
Commit: f4db9b0bf8d73b2974ab4f578f3180477eee1297
Parents: 3148b59
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Feb 23 10:52:23 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Feb 23 11:11:18 2017 +0100

----------------------------------------------------------------------
 .../component/connector/ConnectorComponent.java |  69 +++++++++++++
 .../connector/DefaultConnectorComponent.java    | 101 ++++++++++++++-----
 2 files changed, 147 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f4db9b0b/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/ConnectorComponent.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/ConnectorComponent.java b/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/ConnectorComponent.java
new file mode 100644
index 0000000..38252d3
--- /dev/null
+++ b/connectors/camel-connector/src/main/java/org/apache/camel/component/connector/ConnectorComponent.java
@@ -0,0 +1,69 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.component.connector;
+
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import org.apache.camel.Component;
+import org.apache.camel.catalog.CamelCatalog;
+
+/**
+ * A component which is based from a Camel Connector.
+ */
+public interface ConnectorComponent extends Component {
+
+    /**
+     * Adds a new option to the existing map of options
+     *
+     * @param options  the existing options
+     * @param name     the name of the option
+     * @param value    the value of the option
+     */
+    void addConnectorOption(Map<String, String> options, String name, String value);
+
+    /**
+     * Creates the endpoint uri based on the options from the connector.
+     *
+     * @param scheme  the component name
+     * @param options the options to use for creating the endpoint
+     * @return the endpoint uri
+     * @throws URISyntaxException is thrown if error creating the endpoint uri.
+     */
+    String createEndpointUri(String scheme, Map<String, String> options) throws URISyntaxException;
+
+    /**
+     * Gets the {@link CamelCatalog} which can be used by the connector to help create the component.
+     */
+    CamelCatalog getCamelCatalog();
+
+    /**
+     * Gets the connector name (title)
+     */
+    String getConnectorName();
+
+    /**
+     * Gets the connector component name (component scheme)
+     */
+    String getComponentName();
+
+    /**
+     * Gets the camel-connector JSon file.
+     */
+    String getCamelConnectorJSon();
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/f4db9b0b/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 e980b2e..15096be 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,6 +19,7 @@ package org.apache.camel.component.connector;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -27,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.catalog.CamelCatalog;
@@ -41,11 +43,12 @@ import org.slf4j.LoggerFactory;
 /**
  * Base class for Camel Connector components.
  */
-public abstract class DefaultConnectorComponent extends DefaultComponent {
+public abstract class DefaultConnectorComponent extends DefaultComponent implements ConnectorComponent {
 
+    private static final Pattern NAME_PATTERN = Pattern.compile("\"name\"\\s?:\\s?\"([\\w|.]+)\".*");
     private static final Pattern JAVA_TYPE_PATTERN = Pattern.compile("\"javaType\"\\s?:\\s?\"([\\w|.]+)\".*");
     private static final Pattern BASE_JAVA_TYPE_PATTERN = Pattern.compile("\"baseJavaType\"\\s?:\\s?\"([\\w|.]+)\".*");
-    private static final Pattern BASE_SCHEMA_PATTERN = Pattern.compile("\"baseScheme\"\\s?:\\s?\"([\\w|.]+)\".*");
+    private static final Pattern BASE_SCHEME_PATTERN = Pattern.compile("\"baseScheme\"\\s?:\\s?\"([\\w|.]+)\".*");
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -54,6 +57,8 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
     private final String componentName;
     private final String className;
     private List<String> lines;
+    private String connectorJSon;
+    private String connectorName;
 
     public DefaultConnectorComponent(String componentName, String className) {
         this.componentName = componentName;
@@ -74,7 +79,7 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
 
         // default options from connector json
         if (!defaultOptions.isEmpty()) {
-            options.putAll(defaultOptions);
+            defaultOptions.forEach((k, v) -> addConnectorOption(options, k, v));
         }
         // options from query parameters
         for (Map.Entry<String, Object> entry : parameters.entrySet()) {
@@ -83,7 +88,7 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
             if (entry.getValue() != null) {
                 value = entry.getValue().toString();
             }
-            options.put(key, value);
+            addConnectorOption(options, key, value);
         }
         parameters.clear();
 
@@ -92,11 +97,11 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
             String targetUri = scheme + ":" + remaining;
             Map<String, String> extra = catalog.endpointProperties(targetUri);
             if (extra != null && !extra.isEmpty()) {
-                options.putAll(extra);
+                extra.forEach((k, v) -> addConnectorOption(options, k, v));
             }
         }
 
-        String delegateUri = catalog.asEndpointUri(scheme, options, false);
+        String delegateUri = createEndpointUri(scheme, options);
         log.debug("Connector resolved: {} -> {}", uri, delegateUri);
 
         Endpoint delegate = getCamelContext().getEndpoint(delegateUri);
@@ -104,27 +109,47 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
         return new DefaultConnectorEndpoint(uri, this, delegate);
     }
 
-    private List<String> findCamelConnectorJSonSchema() throws Exception {
-        Enumeration<URL> urls = getClass().getClassLoader().getResources("camel-connector.json");
-        while (urls.hasMoreElements()) {
-            URL url = urls.nextElement();
-            InputStream is = url.openStream();
-            if (is != null) {
-                List<String> lines = loadFile(is);
-                IOHelper.close(is);
+    @Override
+    public String createEndpointUri(String scheme, Map<String, String> options) throws URISyntaxException {
+        log.trace("Creating endpoint uri with scheme: {}", scheme);
+        return catalog.asEndpointUri(scheme, options, false);
+    }
 
-                String javaType = extractJavaType(lines);
-                log.trace("Found camel-connector.json in classpath with javaType: {}", javaType);
+    @Override
+    public void addConnectorOption(Map<String, String> options, String name, String value) {
+        log.trace("Adding option: {}={}", name, value);
+        options.put(name, value);
+    }
 
-                if (className.equals(javaType)) {
-                    return lines;
-                }
-            }
+    @Override
+    public CamelCatalog getCamelCatalog() {
+        return catalog;
+    }
+
+    @Override
+    public String getCamelConnectorJSon() {
+        if (connectorJSon == null) {
+            connectorJSon = lines.stream().collect(Collectors.joining("\n"));
         }
-        return null;
+        return connectorJSon;
     }
 
     @Override
+    public String getConnectorName() {
+        if (connectorName == null) {
+            connectorName = extractName(lines);
+        }
+        return connectorName;
+    }
+
+    @Override
+    public String getComponentName() {
+        return componentName;
+    }
+
+    // --------------------------------------------------------------
+
+    @Override
     protected void doStart() throws Exception {
         this.lines = findCamelConnectorJSonSchema();
         if (lines == null) {
@@ -170,7 +195,26 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
         super.doStop();
     }
 
-    // --------------------------------------------------------------
+    private List<String> findCamelConnectorJSonSchema() throws Exception {
+        log.debug("Finding camel-connector.json in classpath for connector: {}", componentName);
+        Enumeration<URL> urls = getClass().getClassLoader().getResources("camel-connector.json");
+        while (urls.hasMoreElements()) {
+            URL url = urls.nextElement();
+            InputStream is = url.openStream();
+            if (is != null) {
+                List<String> lines = loadFile(is);
+                IOHelper.close(is);
+
+                String javaType = extractJavaType(lines);
+                log.debug("Found camel-connector.json in classpath with javaType: {}", javaType);
+
+                if (className.equals(javaType)) {
+                    return lines;
+                }
+            }
+        }
+        return null;
+    }
 
     private Map<String, String> extractComponentDefaultValues(List<String> lines) {
         Map<String, String> answer = new LinkedHashMap<>();
@@ -242,6 +286,17 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
         return lines;
     }
 
+    private String extractName(List<String> json) {
+        for (String line : json) {
+            line = line.trim();
+            Matcher matcher = NAME_PATTERN.matcher(line);
+            if (matcher.matches()) {
+                return matcher.group(1);
+            }
+        }
+        return null;
+    }
+
     private String extractJavaType(List<String> json) {
         for (String line : json) {
             line = line.trim();
@@ -267,7 +322,7 @@ public abstract class DefaultConnectorComponent extends DefaultComponent {
     private String extractBaseScheme(List<String> json) {
         for (String line : json) {
             line = line.trim();
-            Matcher matcher = BASE_SCHEMA_PATTERN.matcher(line);
+            Matcher matcher = BASE_SCHEME_PATTERN.matcher(line);
             if (matcher.matches()) {
                 return matcher.group(1);
             }