camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [08/21] camel git commit: Add karaf commands to explain endpoints
Date Fri, 07 Nov 2014 12:35:51 GMT
Add karaf commands to explain endpoints


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

Branch: refs/heads/master
Commit: 510c6e5d9558ac2899e4d1b9edca95e200ddc88c
Parents: cfed648
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Nov 6 21:54:51 2014 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Nov 7 13:25:19 2014 +0100

----------------------------------------------------------------------
 .../apache/camel/impl/DefaultCamelContext.java  | 51 ++++++++++++---
 .../org/apache/camel/util/JsonSchemaHelper.java | 50 +++++++++++++++
 .../camel/karaf/commands/EndpointExplain.java   |  3 +-
 .../camel/karaf/commands/EndpointHelper.java    | 67 --------------------
 .../camel/karaf/commands/EndpointList.java      |  3 +-
 5 files changed, 95 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/510c6e5d/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index b049b4a..6293161 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -32,7 +32,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
-import java.util.SortedMap;
 import java.util.TreeMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ScheduledExecutorService;
@@ -1138,18 +1137,14 @@ public class DefaultCamelContext extends ServiceSupport implements
ModelCamelCon
             return null;
         }
 
-        StringBuilder buffer = new StringBuilder("{\n  \"properties\": {");
-        boolean first = true;
+        Map<String, String[]> selected = new LinkedHashMap<>();
+
+        // insert values from uri
         Map<String, Object> options = URISupport.parseParameters(u);
         for (Map.Entry<String, Object> entry : options.entrySet()) {
-            if (first) {
-                first = false;
-            } else {
-                buffer.append(",");
-            }
-            buffer.append("\n    ");
 
             String option = entry.getKey();
+
             String value = "";
             if (entry.getValue() != null) {
                 value = entry.getValue().toString();
@@ -1157,10 +1152,45 @@ public class DefaultCamelContext extends ServiceSupport implements
ModelCamelCon
             value = URISupport.sanitizePath(value);
 
             // if we have the json schema then use that to get the descriptions
-            String description = null;
+            String description;
             description = JsonSchemaHelper.getDescription(json, option);
             description = ObjectHelper.isEmpty(description) ? null : description;
 
+            // add as selected row
+            selected.put(option, new String[]{option, value, description});
+        }
+
+        if (includeAllOptions) {
+            // include other rows
+            List<String[]> rows = JsonSchemaHelper.parseEndpointExplainJson(json);
+            for (String[] row : rows) {
+                String option = row[0];
+                String value = row[1];
+                value = URISupport.sanitizePath(value);
+                String description = row[2];
+
+                // add as selected row
+                if (!selected.containsKey(option)) {
+                    selected.put(option, new String[]{option, value, description});
+                }
+            }
+        }
+
+        StringBuilder buffer = new StringBuilder("{\n  \"properties\": {");
+
+        boolean first = true;
+        for (String[] row : selected.values()) {
+            if (first) {
+                first = false;
+            } else {
+                buffer.append(",");
+            }
+            buffer.append("\n    ");
+
+            String option = row[0];
+            String value = row[1];
+            String description = row[2];
+
             // add json of the option
             buffer.append(doubleQuote(option) + ": { ");
             buffer.append("\"value\": \"" + value + "\"");
@@ -1169,6 +1199,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
             }
             buffer.append(" }");
         }
+
         buffer.append("\n  }\n}\n");
         return buffer.toString();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/510c6e5d/camel-core/src/main/java/org/apache/camel/util/JsonSchemaHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/JsonSchemaHelper.java b/camel-core/src/main/java/org/apache/camel/util/JsonSchemaHelper.java
index 4849e22..4d006d6 100644
--- a/camel-core/src/main/java/org/apache/camel/util/JsonSchemaHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/JsonSchemaHelper.java
@@ -16,11 +16,18 @@
  */
 package org.apache.camel.util;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  * A helper class for <a href="http://json-schema.org/">JSON schema</a>.
  */
 public final class JsonSchemaHelper {
 
+    private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"");
+
     private JsonSchemaHelper() {
     }
 
@@ -125,4 +132,47 @@ public final class JsonSchemaHelper {
         return null;
     }
 
+    /**
+     * Parses the endpoint explain json
+     *
+     * @param json the json
+     * @return a list of all the options, where each row contains: <tt>key, value,
description</tt>
+     */
+    public static List<String[]> parseEndpointExplainJson(String json) {
+        List<String[]> answer = new ArrayList<>();
+        if (json == null) {
+            return answer;
+        }
+
+        // parse line by line
+        // skip first 2 lines as they are leading
+        String[] lines = json.split("\n");
+        for (int i = 2; i < lines.length; i++) {
+            String line = lines[i];
+
+            Matcher matcher = PATTERN.matcher(line);
+            String option = null;
+            String value = null;
+            String description = null;
+            int count = 0;
+            while (matcher.find()) {
+                count++;
+                if (count == 1) {
+                    option = matcher.group(1);
+                } else if (count == 3) {
+                    value = matcher.group(1);
+                } else if (count == 5) {
+                    description = matcher.group(1);
+                }
+            }
+
+            if (option != null) {
+                String[] row = new String[]{option, value, description};
+                answer.add(row);
+            }
+        }
+
+        return answer;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/510c6e5d/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
index e9eb8da..5f4c08a 100644
--- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
+++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.regex.Pattern;
 
 import org.apache.camel.Endpoint;
+import org.apache.camel.util.JsonSchemaHelper;
 import org.apache.camel.util.URISupport;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
@@ -84,7 +85,7 @@ public class EndpointExplain extends CamelCommandSupport {
             out.println();
 
             // use a basic json parser
-            List<String[]> options = EndpointHelper.parseEndpointExplainJson(json);
+            List<String[]> options = JsonSchemaHelper.parseEndpointExplainJson(json);
             for (String[] option : options) {
                 out.print("Option:\t\t");
                 out.println(option[0]);

http://git-wip-us.apache.org/repos/asf/camel/blob/510c6e5d/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointHelper.java
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointHelper.java
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointHelper.java
deleted file mode 100644
index 8a7537d..0000000
--- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointHelper.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * 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.karaf.commands;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public final class EndpointHelper {
-
-    private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"");
-
-    private EndpointHelper() {
-    }
-
-    public static List<String[]> parseEndpointExplainJson(String json) {
-        List<String[]> answer = new ArrayList<>();
-        if (json == null) {
-            return answer;
-        }
-
-        // parse line by line
-        // skip first 2 lines as they are leading
-        String[] lines = json.split("\n");
-        for (int i = 2; i < lines.length; i++) {
-            String line = lines[i];
-
-            Matcher matcher = PATTERN.matcher(line);
-            String option = null;
-            String value = null;
-            String description = null;
-            int count = 0;
-            while (matcher.find()) {
-                count++;
-                if (count == 1) {
-                    option = matcher.group(1);
-                } else if (count == 3) {
-                    value = matcher.group(1);
-                } else if (count == 5) {
-                    description = matcher.group(1);
-                }
-            }
-
-            if (option != null) {
-                String[] row = new String[]{option, value, description};
-                answer.add(row);
-            }
-        }
-
-        return answer;
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/510c6e5d/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointList.java
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointList.java
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointList.java
index 090a38f..4bc5852 100644
--- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointList.java
+++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointList.java
@@ -25,6 +25,7 @@ import java.util.Map;
 import org.apache.camel.Endpoint;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.StatefulService;
+import org.apache.camel.util.JsonSchemaHelper;
 import org.apache.camel.util.URISupport;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
@@ -92,7 +93,7 @@ public class EndpointList extends CamelCommandSupport {
                     boolean first = true;
                     String json = camelController.explainEndpoint(endpoint.getCamelContext().getName(),
endpoint.getEndpointUri(), verbose);
                     // use a basic json parser
-                    List<String[]> options = EndpointHelper.parseEndpointExplainJson(json);
+                    List<String[]> options = JsonSchemaHelper.parseEndpointExplainJson(json);
                     for (String[] option : options) {
                         String key = option[0];
                         String value = option[1];


Mime
View raw message