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/3aba7d0b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3aba7d0b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3aba7d0b Branch: refs/heads/master Commit: 3aba7d0b7390d468c20c80ffdce04870b9c0fe78 Parents: 57039fb Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Nov 7 08:44:33 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Nov 7 13:25:20 2014 +0100 ---------------------------------------------------------------------- .../management/mbean/CamelOpenMBeanTypes.java | 8 +-- .../apache/camel/impl/DefaultCamelContext.java | 52 +++++++++++++------- .../camel/management/mbean/ManagedEndpoint.java | 7 +-- .../camel/util/CollectionStringBuffer.java | 4 ++ .../org/apache/camel/util/JsonSchemaHelper.java | 34 +++++++++++-- .../camel/karaf/commands/EndpointExplain.java | 7 ++- .../camel/karaf/commands/EndpointList.java | 6 ++- 7 files changed, 87 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java index f728aea..6549e9e 100644 --- a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java +++ b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java @@ -66,13 +66,13 @@ public final class CamelOpenMBeanTypes { public static TabularType explainEndpointTabularType() throws OpenDataException { CompositeType ct = explainEndpointsCompositeType(); - return new TabularType("explainEndpoint", "Explain how this endpoint is configured", ct, new String[]{"option", "value", "description"}); + return new TabularType("explainEndpoint", "Explain how this endpoint is configured", ct, new String[]{"option", "type", "value", "description"}); } public static CompositeType explainEndpointsCompositeType() throws OpenDataException { - return new CompositeType("endpoint", "Explain Endpoint", new String[]{"option", "value", "description"}, - new String[]{"Option", "Value", "Description"}, - new OpenType[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}); + return new CompositeType("endpoint", "Explain Endpoint", new String[]{"option", "type", "value", "description"}, + new String[]{"Option", "Type", "Value", "Description"}, + new OpenType[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}); } } http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/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 b5be232..c627cce 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 @@ -131,6 +131,7 @@ import org.apache.camel.spi.UnitOfWorkFactory; import org.apache.camel.spi.UuidGenerator; import org.apache.camel.support.ServiceSupport; import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.CollectionStringBuffer; import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.EventHelper; import org.apache.camel.util.IOHelper; @@ -1136,42 +1137,47 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon if (json == null) { return null; } + List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema(json); 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()) { - - String option = entry.getKey(); - + String name = 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; - description = JsonSchemaHelper.getDescription(json, option); - description = ObjectHelper.isEmpty(description) ? null : description; + // find type and description from the json schema + String type = null; + String description = null; + for (Map<String, String> row : rows) { + if (name.equals(row.get("name"))) { + type = row.get("type"); + description = row.get("description"); + break; + } + } // add as selected row - selected.put(option, new String[]{option, value, description}); + selected.put(name, new String[]{name, type, value, description}); } if (includeAllOptions) { // include other rows - List<Map<String, String>> rows = JsonSchemaHelper.parseEndpointExplainJson(json); for (Map<String, String> row : rows) { - String option = row.get("name"); + String name = row.get("name"); String value = row.get("value"); + String type = row.get("type"); value = URISupport.sanitizePath(value); String description = row.get("description"); // add as selected row - if (!selected.containsKey(option)) { - selected.put(option, new String[]{option, value, description}); + if (!selected.containsKey(name)) { + selected.put(name, new String[]{name, type, value, description}); } } } @@ -1187,15 +1193,25 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon } buffer.append("\n "); - String option = row[0]; - String value = row[1]; - String description = row[2]; + String name = row[0]; + String type = row[1]; + String value = row[2]; + String description = row[3]; // add json of the option - buffer.append(doubleQuote(option) + ": { "); - buffer.append("\"value\": \"" + value + "\""); + buffer.append(doubleQuote(name) + ": { "); + CollectionStringBuffer csb = new CollectionStringBuffer(); + if (type != null) { + csb.append("\"type\": \"" + type + "\""); + } + if (value != null) { + csb.append("\"value\": \"" + value + "\""); + } if (description != null) { - buffer.append(", \"description\": \"" + description + "\""); + csb.append("\"description\": \"" + description + "\""); + } + if (!csb.isEmpty()) { + buffer.append(csb.toString()); } buffer.append(" }"); } http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/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 3594737..08deefc 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 @@ -87,19 +87,20 @@ public class ManagedEndpoint implements ManagedInstance, ManagedEndpointMBean { public TabularData explain(boolean allOptions) { try { String json = endpoint.getCamelContext().explainEndpointJson(getEndpointUri(), allOptions); - List<Map<String, String>> rows = JsonSchemaHelper.parseEndpointExplainJson(json); + List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema(json); TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEndpointTabularType()); for (Map<String, String> row : rows) { String option = row.get("name"); + String type = row.get("type"); 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}); + {"option", "type", "value", "description"}, + new Object[]{option, type, value, description}); answer.put(data); } http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/camel-core/src/main/java/org/apache/camel/util/CollectionStringBuffer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/CollectionStringBuffer.java b/camel-core/src/main/java/org/apache/camel/util/CollectionStringBuffer.java index e3275ae..4441016 100644 --- a/camel-core/src/main/java/org/apache/camel/util/CollectionStringBuffer.java +++ b/camel-core/src/main/java/org/apache/camel/util/CollectionStringBuffer.java @@ -55,4 +55,8 @@ public class CollectionStringBuffer { public void setSeparator(String separator) { this.separator = separator; } + + public boolean isEmpty() { + return first; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/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 5743e99..98efc3e 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 @@ -109,13 +109,39 @@ public final class JsonSchemaHelper { } /** + * Extracts the type value from the blob of json with the given property name + * + * @param json the blob of json + * @param name the name of the property to extract the type + * @return the value of the type, or <tt>null</tt> if no type exists + */ + public static String extractTypeFromJson(String json, String name) { + // we dont have a json parser, but we know the structure, so just do this simple way + String[] lines = json.split("\n"); + for (String line : lines) { + line = line.trim(); + if (line.startsWith("\"" + name + "\":")) { + // grab text after type + String value = ObjectHelper.after(line, "\"type\": \""); + if (value != null) { + int lastQuote = value.lastIndexOf('"'); + value = value.substring(0, lastQuote); + value = StringHelper.removeLeadingAndEndingQuotes(value); + return value; + } + } + } + return null; + } + + /** * Extracts the description value from the blob of json with the given property name * * @param json the blob of json * @param name the name of the property to extract the description * @return the value of the description, or <tt>null</tt> if no description exists */ - public static String getDescription(String json, String name) { + public static String extractDescriptionFromJson(String json, String name) { // we dont have a json parser, but we know the structure, so just do this simple way String[] lines = json.split("\n"); for (String line : lines) { @@ -135,12 +161,12 @@ public final class JsonSchemaHelper { } /** - * Parses the endpoint explain json + * Parses the json schema to split it into a list or rows, where each row contains key value pairs with the metadata * * @param json the json - * @return a list of all the options, where each row is a set of key value pairs with metadata + * @return a list of all the rows, where each row is a set of key value pairs with metadata */ - public static List<Map<String, String>> parseEndpointExplainJson(String json) { + public static List<Map<String, String>> parseJsonSchema(String json) { List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); if (json == null) { return answer; http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/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 f32b261..9f5cd34 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 @@ -83,10 +83,15 @@ public class EndpointExplain extends CamelCommandSupport { out.println(); // use a basic json parser - List<Map<String, String>> options = JsonSchemaHelper.parseEndpointExplainJson(json); + List<Map<String, String>> options = JsonSchemaHelper.parseJsonSchema(json); for (Map<String, String> option : options) { out.print("Option:\t\t"); out.println(option.get("name")); + String type = option.get("type"); + if (type != null) { + out.print("Type:\t\t"); + out.println(type); + } String value = option.get("value"); if (value != null) { out.print("Value:\t\t"); http://git-wip-us.apache.org/repos/asf/camel/blob/3aba7d0b/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 db9d7ba..f366a05 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,9 +93,10 @@ public class EndpointList extends CamelCommandSupport { boolean first = true; String json = camelController.explainEndpoint(endpoint.getCamelContext().getName(), endpoint.getEndpointUri(), verbose); // use a basic json parser - List<Map<String, String>> options = JsonSchemaHelper.parseEndpointExplainJson(json); + List<Map<String, String>> options = JsonSchemaHelper.parseJsonSchema(json); for (Map<String, String> option : options) { String key = option.get("name"); + String type = option.get("type"); String value = option.get("value"); String desc = option.get("description"); if (key != null && value != null) { @@ -105,6 +106,9 @@ public class EndpointList extends CamelCommandSupport { } String line = "\t" + key + "=" + value; out.println(String.format(rowFormat, "", line, "")); + if (type != null) { + out.println(String.format(rowFormat, "", "\t" + type, "")); + } if (desc != null) { out.println(String.format(rowFormat, "", "\t" + desc, "")); }