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/57039fbc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/57039fbc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/57039fbc Branch: refs/heads/master Commit: 57039fbcd2a72f12be12c06a2d163d6afa32acb4 Parents: 510c6e5 Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Nov 7 08:10:20 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Nov 7 13:25:20 2014 +0100 ---------------------------------------------------------------------- .../apache/camel/impl/DefaultCamelContext.java | 10 +++--- .../camel/management/mbean/ManagedEndpoint.java | 33 ++++++-------------- .../org/apache/camel/util/JsonSchemaHelper.java | 33 ++++++++++---------- .../management/ManagedEndpointExplainTest.java | 4 +++ .../camel/karaf/commands/EndpointExplain.java | 14 ++++----- .../camel/karaf/commands/EndpointList.java | 10 +++--- 6 files changed, 45 insertions(+), 59 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/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 6293161..b5be232 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 @@ -1162,12 +1162,12 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon if (includeAllOptions) { // include other rows - List<String[]> rows = JsonSchemaHelper.parseEndpointExplainJson(json); - for (String[] row : rows) { - String option = row[0]; - String value = row[1]; + List<Map<String, String>> rows = JsonSchemaHelper.parseEndpointExplainJson(json); + for (Map<String, String> row : rows) { + String option = row.get("name"); + String value = row.get("value"); value = URISupport.sanitizePath(value); - String description = row[2]; + String description = row.get("description"); // add as selected row if (!selected.containsKey(option)) { http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedEndpoint.java b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedEndpoint.java index 7f95254..3594737 100644 --- a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedEndpoint.java @@ -16,7 +16,7 @@ */ package org.apache.camel.management.mbean; -import java.net.URI; +import java.util.List; import java.util.Map; import javax.management.openmbean.CompositeData; import javax.management.openmbean.CompositeDataSupport; @@ -34,7 +34,6 @@ import org.apache.camel.api.management.mbean.ManagedEndpointMBean; import org.apache.camel.spi.ManagementStrategy; import org.apache.camel.util.JsonSchemaHelper; import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.URISupport; @ManagedResource(description = "Managed Endpoint") public class ManagedEndpoint implements ManagedInstance, ManagedEndpointMBean { @@ -87,37 +86,23 @@ public class ManagedEndpoint implements ManagedInstance, ManagedEndpointMBean { @Override public TabularData explain(boolean allOptions) { try { - TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEndpointTabularType()); + String json = endpoint.getCamelContext().explainEndpointJson(getEndpointUri(), allOptions); + List<Map<String, String>> rows = JsonSchemaHelper.parseEndpointExplainJson(json); - // explain all the explicit configured options on the endpoint - URI uri = endpoint.getEndpointConfiguration().getURI(); - Map<String, Object> options = URISupport.parseParameters(uri); - for (Map.Entry<String, Object> entry : options.entrySet()) { - CompositeType ct = CamelOpenMBeanTypes.explainEndpointsCompositeType(); + TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEndpointTabularType()); - String option = entry.getKey(); - String value = ""; - if (entry.getValue() != null) { - value = entry.getValue().toString(); - } - value = URISupport.sanitizePath(value); - - // if we have the json schema then use that to get the descriptions - String description = null; - String json = endpoint.getCamelContext().getComponentParameterJsonSchema(uri.getScheme()); - if (json != null) { - description = JsonSchemaHelper.getDescription(json, option); - } - description = ObjectHelper.isEmpty(description) ? "" : description; + for (Map<String, String> row : rows) { + String option = row.get("name"); + String value = row.get("value") != null ? row.get("value") : ""; + String description = row.get("description") != null ? row.get("description") : ""; + CompositeType ct = CamelOpenMBeanTypes.explainEndpointsCompositeType(); CompositeData data = new CompositeDataSupport(ct, new String[] {"option", "value", "description"}, new Object[]{option, value, description}); answer.put(data); } - // TODO: add support for all options, which requires to parse the json blob - return answer; } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/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 4d006d6..5743e99 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 @@ -17,7 +17,9 @@ package org.apache.camel.util; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -136,10 +138,10 @@ public final class JsonSchemaHelper { * 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> + * @return a list of all the options, where each row is a set of key value pairs with metadata */ - public static List<String[]> parseEndpointExplainJson(String json) { - List<String[]> answer = new ArrayList<>(); + public static List<Map<String, String>> parseEndpointExplainJson(String json) { + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); if (json == null) { return answer; } @@ -150,24 +152,21 @@ public final class JsonSchemaHelper { for (int i = 2; i < lines.length; i++) { String line = lines[i]; + Map<String, String> row = new LinkedHashMap<>(); Matcher matcher = PATTERN.matcher(line); - String option = null; - String value = null; - String description = null; - int count = 0; + // the first key is the name of the option + String key = "name"; 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 (key == null) { + key = matcher.group(1); + } else { + String value = matcher.group(1); + row.put(key, value); + // reset + key = null; } } - - if (option != null) { - String[] row = new String[]{option, value, description}; + if (!row.isEmpty()) { answer.add(row); } } http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointExplainTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointExplainTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointExplainTest.java index 066fe26..3f7908e 100644 --- a/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointExplainTest.java +++ b/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointExplainTest.java @@ -47,6 +47,10 @@ public class ManagedEndpointExplainTest extends ManagementTestSupport { // there should be 2 options TabularData data = (TabularData) mbeanServer.invoke(on, "explain", new Object[]{false}, new String[]{"boolean"}); assertEquals(2, data.size()); + + // there should be 6 options + data = (TabularData) mbeanServer.invoke(on, "explain", new Object[]{true}, new String[]{"boolean"}); + assertEquals(6, data.size()); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/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 5f4c08a..f32b261 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 @@ -19,7 +19,7 @@ package org.apache.camel.karaf.commands; import java.io.PrintStream; import java.util.Iterator; import java.util.List; -import java.util.regex.Pattern; +import java.util.Map; import org.apache.camel.Endpoint; import org.apache.camel.util.JsonSchemaHelper; @@ -34,8 +34,6 @@ import org.apache.felix.gogo.commands.Option; @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; @@ -85,16 +83,16 @@ public class EndpointExplain extends CamelCommandSupport { out.println(); // use a basic json parser - List<String[]> options = JsonSchemaHelper.parseEndpointExplainJson(json); - for (String[] option : options) { + List<Map<String, String>> options = JsonSchemaHelper.parseEndpointExplainJson(json); + for (Map<String, String> option : options) { out.print("Option:\t\t"); - out.println(option[0]); - String value = option[1]; + out.println(option.get("name")); + String value = option.get("value"); if (value != null) { out.print("Value:\t\t"); out.println(value); } - String description = option[2]; + String description = option.get("description"); if (description != null) { out.print("Description:\t"); out.println(description); http://git-wip-us.apache.org/repos/asf/camel/blob/57039fbc/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 4bc5852..db9d7ba 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 @@ -93,11 +93,11 @@ 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 = JsonSchemaHelper.parseEndpointExplainJson(json); - for (String[] option : options) { - String key = option[0]; - String value = option[1]; - String desc = option[2]; + List<Map<String, String>> options = JsonSchemaHelper.parseEndpointExplainJson(json); + for (Map<String, String> option : options) { + String key = option.get("name"); + String value = option.get("value"); + String desc = option.get("description"); if (key != null && value != null) { if (first) { out.println();