camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-10828: camel-catalog-nexus - more work
Date Mon, 20 Feb 2017 13:54:15 GMT
Repository: camel
Updated Branches:
  refs/heads/master a97db7cd5 -> eb3f69493


CAMEL-10828: camel-catalog-nexus - more work


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

Branch: refs/heads/master
Commit: eb3f694936ca95343ad58cb8376346731c09eba9
Parents: a97db7c
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Feb 20 14:53:45 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Feb 20 14:53:45 2017 +0100

----------------------------------------------------------------------
 platforms/catalog-nexus/pom.xml                 |  7 ++
 .../camel/catalog/nexus/ConnectorDataStore.java | 12 +--
 .../ConnectorDataStoreNexusRepository.java      | 62 ++++++++--------
 .../camel/catalog/nexus/ConnectorDto.java       | 19 ++++-
 .../catalog/nexus/MemoryConnectorDataStore.java | 78 ++++++++++++++++++--
 .../LocalFileConnectorNexusRepository.java      |  5 +-
 6 files changed, 136 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/pom.xml b/platforms/catalog-nexus/pom.xml
index bb0d970..071f334 100644
--- a/platforms/catalog-nexus/pom.xml
+++ b/platforms/catalog-nexus/pom.xml
@@ -42,6 +42,13 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-catalog</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>

http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStore.java
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStore.java
b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStore.java
index b1daa7a..e71099d 100644
--- a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStore.java
+++ b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStore.java
@@ -21,6 +21,11 @@ import java.util.List;
 public interface ConnectorDataStore {
 
     /**
+     * Number of connectors in the data store.
+     */
+    int size();
+
+    /**
      * Adds a connector to the data store.
      */
     void addConnector(ConnectorDto connector);
@@ -32,11 +37,6 @@ public interface ConnectorDataStore {
      * @param latestVersionOnly to only include the latest version of a given Maven <tt>groupId:artifactId</tt>
      * @return the found connectors, or an empty list if none found
      */
-    List<ConnectorDto> searchArtifacts(String filter, boolean latestVersionOnly);
-
-    /**
-     * Number of connectors in the data store.
-     */
-    int size();
+    List<ConnectorDto> searchConnectors(String filter, boolean latestVersionOnly);
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStoreNexusRepository.java
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStoreNexusRepository.java
b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStoreNexusRepository.java
index 3e7426a..b2177b7 100644
--- a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStoreNexusRepository.java
+++ b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDataStoreNexusRepository.java
@@ -20,9 +20,16 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.camel.catalog.CatalogHelper;
+import org.apache.camel.catalog.CollectionStringBuffer;
 
 import static org.apache.camel.catalog.CatalogHelper.loadText;
 
@@ -73,13 +80,16 @@ public class ConnectorDataStoreNexusRepository extends BaseNexusRepository
{
      * Adds the connector to the data store
      *
      * @param dto                 the artifact
-     * @param name                name of connector
+     * @param name                the name of connector
+     * @param description         the description of connector
+     * @param labels              the labels of connector
      * @param connectorJson       camel-connector JSon
      * @param connectorSchemaJson camel-connector-schema JSon
      */
-    protected void addConnector(NexusArtifactDto dto, String name, String connectorJson,
String connectorSchemaJson) {
+    protected void addConnector(NexusArtifactDto dto, String name, String description, String
labels,
+                                String connectorJson, String connectorSchemaJson) {
         if (connectorDataStore != null) {
-            ConnectorDto connector = new ConnectorDto(dto, name, connectorJson, connectorSchemaJson);
+            ConnectorDto connector = new ConnectorDto(dto, name, description, labels, connectorJson,
connectorSchemaJson);
             log.info("Added connector: {}:{}:{}", dto.getGroupId(), dto.getArtifactId(),
dto.getVersion());
             connectorDataStore.addConnector(connector);
         }
@@ -92,7 +102,20 @@ public class ConnectorDataStoreNexusRepository extends BaseNexusRepository
{
         try (URLClassLoader classLoader = new URLClassLoader(new URL[] {jarUrl});) {
             String[] json = loadConnectorJSonSchema(classLoader);
             if (json != null) {
-                addConnector(dto, json[0], json[1], json[2]);
+
+                ObjectMapper mapper = new ObjectMapper();
+                JsonNode tree = mapper.readTree(json[0]);
+                String name = tree.get("name").textValue();
+                String description = tree.get("description").textValue();
+                Iterator<JsonNode> it = tree.withArray("labels").iterator();
+
+                CollectionStringBuffer csb = new CollectionStringBuffer(",");
+                while (it.hasNext()) {
+                    String text = it.next().textValue();
+                    csb.append(text);
+                }
+
+                addConnector(dto, name, description, csb.toString(), json[0], json[1]);
             }
         } catch (IOException e) {
             log.warn("Error scanning JAR for custom Camel components", e);
@@ -100,13 +123,13 @@ public class ConnectorDataStoreNexusRepository extends BaseNexusRepository
{
     }
 
     private String[] loadConnectorJSonSchema(URLClassLoader classLoader) {
-        String[] answer = new String[3];
+        String[] answer = new String[2];
 
         String path = "camel-connector.json";
         try {
             InputStream is = classLoader.getResourceAsStream(path);
             if (is != null) {
-                answer[1] = loadText(is);
+                answer[0] = loadText(is);
             }
         } catch (Throwable e) {
             log.warn("Error loading " + path + " file", e);
@@ -116,38 +139,13 @@ public class ConnectorDataStoreNexusRepository extends BaseNexusRepository
{
         try {
             InputStream is = classLoader.getResourceAsStream(path);
             if (is != null) {
-                answer[2] = loadText(is);
+                answer[1] = loadText(is);
             }
         } catch (Throwable e) {
             log.warn("Error loading " + path + " file", e);
         }
 
-        String name = extractConnectorName(answer[1]);
-        answer[0] = name;
-
         return answer;
     }
 
-    /**
-     * Extracts the name of the connector from the json blob
-     */
-    private static String extractConnectorName(String json) {
-        String[] lines = json.split("\n");
-        for (String line : lines) {
-            line = line.trim();
-            if (line.startsWith("\"name\":")) {
-                String name = CatalogHelper.after(line, ":");
-                if (name != null) {
-                    name = name.trim();
-                    if (name.endsWith(",")) {
-                        // skip last comma
-                        name = name.substring(0, name.length() - 1);
-                    }
-                    return CatalogHelper.removeLeadingAndEndingQuotes(name);
-                }
-            }
-        }
-        return null;
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDto.java
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDto.java
b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDto.java
index 0c66ba8..33bea4a 100644
--- a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDto.java
+++ b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/ConnectorDto.java
@@ -22,12 +22,17 @@ public class ConnectorDto implements Serializable {
 
     private NexusArtifactDto nexusArtifactDto;
     private String name;
+    private String description;
+    private String labels;
     private String connectorJson;
     private String connectorSchemaJson;
 
-    public ConnectorDto(NexusArtifactDto nexusArtifactDto, String name, String connectorJson,
String connectorSchemaJson) {
+    public ConnectorDto(NexusArtifactDto nexusArtifactDto, String name, String description,
String labels,
+                        String connectorJson, String connectorSchemaJson) {
         this.nexusArtifactDto = nexusArtifactDto;
         this.name = name;
+        this.description = description;
+        this.labels = labels;
         this.connectorJson = connectorJson;
         this.connectorSchemaJson = connectorSchemaJson;
     }
@@ -40,6 +45,14 @@ public class ConnectorDto implements Serializable {
         return name;
     }
 
+    public String getDescription() {
+        return description;
+    }
+
+    public String getLabels() {
+        return labels;
+    }
+
     public String getConnectorJson() {
         return connectorJson;
     }
@@ -48,6 +61,10 @@ public class ConnectorDto implements Serializable {
         return connectorSchemaJson;
     }
 
+    public String getMavenGav() {
+        return nexusArtifactDto.getGroupId() + ":" + nexusArtifactDto.getArtifactId() + ":"
+ nexusArtifactDto.getVersion();
+    }
+
     @Override
     public boolean equals(Object o) {
         if (this == o) {

http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/MemoryConnectorDataStore.java
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/MemoryConnectorDataStore.java
b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/MemoryConnectorDataStore.java
index e3d5995..5be41b2 100644
--- a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/MemoryConnectorDataStore.java
+++ b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/MemoryConnectorDataStore.java
@@ -16,8 +16,11 @@
  */
 package org.apache.camel.catalog.nexus;
 
+import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
 
 /**
@@ -28,17 +31,80 @@ public class MemoryConnectorDataStore implements ConnectorDataStore {
     private final Set<ConnectorDto> connectors = new LinkedHashSet<>();
 
     @Override
-    public void addConnector(ConnectorDto connector) {
-        connectors.add(connector);
+    public int size() {
+        return connectors.size();
     }
 
     @Override
-    public List<ConnectorDto> searchArtifacts(String filter, boolean latestVersionOnly)
{
-        return null;
+    public void addConnector(ConnectorDto connector) {
+        connectors.add(connector);
     }
 
     @Override
-    public int size() {
-        return connectors.size();
+    public List<ConnectorDto> searchConnectors(String filter, boolean latestVersionOnly)
{
+        List<ConnectorDto> answer = new ArrayList<>();
+
+        if (filter == null || filter.isEmpty()) {
+            // return all of them
+            answer.addAll(connectors);
+        } else {
+            // search ignore case
+            filter = filter.toLowerCase(Locale.US);
+            for (ConnectorDto dto : connectors) {
+                if (dto.getName().toLowerCase(Locale.US).contains(filter)) {
+                    answer.add(dto);
+                } else if (dto.getDescription().toLowerCase(Locale.US).contains(filter))
{
+                    answer.add(dto);
+                } else if (dto.getNexusArtifactDto().getGroupId().toLowerCase(Locale.US).contains(filter))
{
+                    answer.add(dto);
+                } else if (dto.getNexusArtifactDto().getArtifactId().toLowerCase(Locale.US).contains(filter))
{
+                    answer.add(dto);
+                } else if (dto.getNexusArtifactDto().getVersion().toLowerCase(Locale.US).contains(filter))
{
+                    answer.add(dto);
+                } else {
+                    String labels = dto.getLabels();
+                    if (labels != null) {
+                        String[] arr = labels.split(",");
+                        for (String lab : arr) {
+                            lab = lab.toLowerCase(Locale.US);
+                            if (lab.contains(filter)) {
+                                answer.add(dto);
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        // filter only latest version
+        if (latestVersionOnly && answer.size() > 1) {
+            // sort first
+            answer.sort(Comparator.comparing(ConnectorDto::getMavenGav));
+
+            // keep only latest in each group
+            List<ConnectorDto> unique = new ArrayList<>();
+            ConnectorDto prev = null;
+
+            for (ConnectorDto dto : answer) {
+                if (prev == null
+                    || (prev.getNexusArtifactDto().getGroupId().equals(dto.getNexusArtifactDto().getGroupId())
+                        && prev.getNexusArtifactDto().getArtifactId().equals(dto.getNexusArtifactDto().getArtifactId()))
) {
+                    prev = dto;
+                } else {
+                    unique.add(prev);
+                    prev = dto;
+                }
+            }
+            if (prev != null) {
+                // special for last element
+                unique.add(prev);
+            }
+
+            answer = unique;
+        }
+
+        return answer;
+
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/eb3f6949/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileConnectorNexusRepository.java
----------------------------------------------------------------------
diff --git a/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileConnectorNexusRepository.java
b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileConnectorNexusRepository.java
index aa0f93d..2a1ae96 100644
--- a/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileConnectorNexusRepository.java
+++ b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileConnectorNexusRepository.java
@@ -45,11 +45,12 @@ public class LocalFileConnectorNexusRepository extends ConnectorDataStoreNexusRe
     }
 
     @Override
-    protected void addConnector(NexusArtifactDto dto, String name, String connectorJson,
String connectorSchemaJson) {
-        super.addConnector(dto, name, connectorJson, connectorSchemaJson);
+    protected void addConnector(NexusArtifactDto dto, String name, String description, String
labels, String connectorJson, String connectorSchemaJson) {
+        super.addConnector(dto, name, description, labels, connectorJson, connectorSchemaJson);
 
         if (onAddConnector != null) {
             onAddConnector.run();
         }
     }
+
 }


Mime
View raw message