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/1c023667
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1c023667
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1c023667

Branch: refs/heads/master
Commit: 1c02366754a56382673ab6923b2cebc4aa93280c
Parents: ae366ed
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Nov 6 21:16:48 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Nov 7 13:25:19 2014 +0100

----------------------------------------------------------------------
 .../camel/karaf/commands/CamelController.java   |  18 +++-
 .../camel/karaf/commands/EndpointExplain.java   | 108 +++++++++++++++++++
 .../camel/karaf/commands/EndpointHelper.java    |  64 +++++++++++
 .../commands/internal/CamelControllerImpl.java  |   8 ++
 .../OSGI-INF/blueprint/camel-commands.xml       |   9 ++
 5 files changed, 203 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1c023667/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
index adce940..aa5bdf8 100644
--- 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
@@ -53,12 +53,12 @@ public interface CamelController {
      * @return the list of the Camel routes.
      */
     List<Route> getRoutes(String camelContextName);
-    
+
     /**
      * Get all routes filtered by the regex.
      *
      * @param camelContextName the Camel context name. If null, all contexts 
are considered.
-     * @param filter the filter which supports * and ? as wildcards
+     * @param filter           the filter which supports * and ? as wildcards
      * @return the list of the Camel routes.
      */
     List<Route> getRoutes(String camelContextName, String filter);
@@ -66,7 +66,7 @@ public interface CamelController {
     /**
      * Return the route with the given route ID.
      *
-     * @param routeId the route ID.
+     * @param routeId          the route ID.
      * @param camelContextName the Camel context name.
      * @return the route.
      */
@@ -75,7 +75,7 @@ public interface CamelController {
     /**
      * Return the definition of a route identified by a ID and a Camel context.
      *
-     * @param routeId the route ID.
+     * @param routeId          the route ID.
      * @param camelContextName the Camel context.
      * @return the <code>RouteDefinition</code>.
      */
@@ -105,4 +105,14 @@ public interface CamelController {
      */
     Map<String, List<RestRegistry.RestService>> getRestServices(String 
camelContextName);
 
+    /**
+     * Explains an endpoint uri
+     *
+     * @param camelContextName the Camel context.
+     * @param uri              the endpoint uri
+     * @param allOptions       whether to explain all options, or only the 
explicit configured options from the uri
+     * @return a JSON schema with explanation of the options
+     */
+    String explainEndpoint(String camelContextName, String uri, boolean 
allOptions) throws Exception;
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1c023667/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
new file mode 100644
index 0000000..e9eb8da
--- /dev/null
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointExplain.java
@@ -0,0 +1,108 @@
+/**
+ * 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.io.PrintStream;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.util.URISupport;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+
+/**
+ * Explain the Camel endpoints available in the Karaf instance.
+ */
+@Command(scope = "camel", name = "endpoint-explain", description = "Explain 
all Camel endpoints available in a CamelContext.")
+public class EndpointExplain extends CamelCommandSupport {
+
+    private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"");
+
+    @Argument(index = 0, name = "name", description = "The Camel context name 
where to look for the endpoints", required = true, multiValued = false)
+    String name;
+
+    @Option(name = "--allOptions", aliases = "-all", description = "Whether to 
include all options",
+            required = false, multiValued = false, valueToShowInHelp = "false")
+    boolean allOptions = false;
+
+    @Option(name = "--scheme", aliases = "-s", description = "To filter 
endpoints by scheme",
+            required = false, multiValued = true)
+    String[] schemes;
+
+    protected Object doExecute() throws Exception {
+        List<Endpoint> endpoints = camelController.getEndpoints(name);
+        if (endpoints == null || endpoints.isEmpty()) {
+            return null;
+        }
+
+        // filter endpoints by scheme
+        if (schemes != null && schemes.length > 0) {
+            Iterator<Endpoint> it = endpoints.iterator();
+            while (it.hasNext()) {
+                Endpoint endpoint = it.next();
+                boolean match = false;
+                for (String scheme : schemes) {
+                    if (endpoint.getEndpointUri().startsWith(scheme)) {
+                        match = true;
+                    }
+                }
+                if (!match) {
+                    it.remove();
+                }
+            }
+        }
+
+        final PrintStream out = System.out;
+
+        for (Endpoint endpoint : endpoints) {
+            String json = camelController.explainEndpoint(name, 
endpoint.getEndpointUri(), allOptions);
+
+            // sanitize and mask uri so we dont see passwords
+            String uri = URISupport.sanitizeUri(endpoint.getEndpointUri());
+            String header = "Uri:            " + uri;
+            out.println(header);
+            for (int i = 0; i < header.length(); i++) {
+                out.print('-');
+            }
+            out.println();
+
+            // use a basic json parser
+            List<String[]> options = 
EndpointHelper.parseEndpointExplainJson(json);
+            for (String[] option : options) {
+                out.print("Option:\t\t");
+                out.println(option[0]);
+                String value = option[1];
+                if (value != null) {
+                    out.print("Value:\t\t");
+                    out.println(value);
+                }
+                String description = option[2];
+                if (description != null) {
+                    out.print("Description:\t");
+                    out.println(description);
+                }
+            }
+            out.println();
+        }
+
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1c023667/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
new file mode 100644
index 0000000..0f6093d
--- /dev/null
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/EndpointHelper.java
@@ -0,0 +1,64 @@
+/**
+ * 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) {
+        // parse line by line
+        List<String[]> answer = new ArrayList<>();
+
+        // 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/1c023667/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
index 148619e..07bfcbb 100644
--- 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
@@ -224,4 +224,12 @@ public class CamelControllerImpl implements 
CamelController {
         }
         return answer;
     }
+
+    public String explainEndpoint(String camelContextName, String uri, boolean 
allOptions) throws Exception {
+        CamelContext context = this.getCamelContext(camelContextName);
+        if (context == null) {
+            return null;
+        }
+        return context.explainEndpointJson(uri, allOptions);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1c023667/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
index 0870a0a..11e3fc4 100644
--- 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
+++ 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
@@ -158,6 +158,15 @@
         <null/>
       </completers>
     </command>
+    <command name="camel/endpoint-explain">
+      <action class="org.apache.camel.karaf.commands.EndpointExplain">
+        <property name="camelController" ref="camelController"/>
+      </action>
+      <completers>
+        <ref component-id="camelContextCompleter"/>
+        <null/>
+      </completers>
+    </command>
     <command name="camel/backlog-tracer-info">
       <action class="org.apache.camel.karaf.commands.BacklogTracerInfo">
         <property name="camelController" ref="camelController"/>

Reply via email to