Add mbean operation to explain endpoint options
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ae366ed6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ae366ed6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ae366ed6 Branch: refs/heads/master Commit: ae366ed6fa54729bd6b7c2659ec48e12835ccd31 Parents: 6840309 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Nov 6 13:15:42 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Nov 7 13:25:19 2014 +0100 ---------------------------------------------------------------------- .../java/org/apache/camel/CamelContext.java | 8 ++++ .../mbean/ManagedCamelContextMBean.java | 9 ++++ .../apache/camel/impl/DefaultCamelContext.java | 49 +++++++++++++++++++- .../management/mbean/ManagedCamelContext.java | 4 ++ .../management/ManagedCamelContextTest.java | 21 +++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ae366ed6/camel-core/src/main/java/org/apache/camel/CamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java index d765b54..c8d6ff5 100644 --- a/camel-core/src/main/java/org/apache/camel/CamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java @@ -1356,6 +1356,14 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration { String getComponentParameterJsonSchema(String componentName) throws IOException; /** + * Returns a JSON schema representation of the endpoint parameters for the given endpoint uri + * + * @param uri the endpoint uri + * @param includeAllOptions whether to include non configured options also (eg default options) + */ + String explainEndpointJson(String uri, boolean includeAllOptions) throws Exception; + + /** * Creates a JSON representation of all the <b>static</b> and <b>dynamic</b> configured endpoints defined in the given route(s). * * @param routeId for a particular route, or <tt>null</tt> for all routes http://git-wip-us.apache.org/repos/asf/camel/blob/ae366ed6/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java index 0050b32..f6fb6de 100644 --- a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java +++ b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java @@ -231,6 +231,15 @@ public interface ManagedCamelContextMBean extends ManagedPerformanceCounterMBean String componentParameterJsonSchema(String componentName) throws Exception; /** + * Returns a JSON schema representation of the endpoint parameters for the given endpoint uri + * + * @param uri the endpoint uri + * @param includeAllOptions whether to include non configured options also (eg default options) + */ + @ManagedOperation(description = " Returns a JSON schema representation of the endpoint parameters for the given endpoint uri") + String explainEndpointJson(String uri, boolean includeAllOptions) throws Exception; + + /** * Resets all the performance counters. * * @param includeRoutes whether to reset all routes as well. http://git-wip-us.apache.org/repos/asf/camel/blob/ae366ed6/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 77c6668..b049b4a 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 @@ -18,6 +18,7 @@ package org.apache.camel.impl; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -31,12 +32,12 @@ 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; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import javax.naming.Context; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; @@ -135,6 +136,7 @@ import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.EventHelper; import org.apache.camel.util.IOHelper; import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.JsonSchemaHelper; import org.apache.camel.util.LoadPropertiesException; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ServiceHelper; @@ -145,6 +147,8 @@ import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.camel.util.StringQuoteHelper.doubleQuote; + /** * Represents the context used to configure routes and the policies to use. * @@ -1126,6 +1130,49 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon return componentName.replaceAll("-", ""); } + public String explainEndpointJson(String uri, boolean includeAllOptions) throws Exception { + URI u = new URI(uri); + + String json = getComponentParameterJsonSchema(u.getScheme()); + if (json == null) { + return null; + } + + StringBuilder buffer = new StringBuilder("{\n \"properties\": {"); + boolean first = true; + 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(); + } + value = URISupport.sanitizePath(value); + + // if we have the json schema then use that to get the descriptions + String description = null; + description = JsonSchemaHelper.getDescription(json, option); + description = ObjectHelper.isEmpty(description) ? null : description; + + // add json of the option + buffer.append(doubleQuote(option) + ": { "); + buffer.append("\"value\": \"" + value + "\""); + if (description != null) { + buffer.append(", \"description\": \"" + description + "\""); + } + buffer.append(" }"); + } + buffer.append("\n }\n}\n"); + return buffer.toString(); + } + public String createRouteStaticEndpointJson(String routeId) { // lets include dynamic as well as we want as much data as possible return createRouteStaticEndpointJson(routeId, true); http://git-wip-us.apache.org/repos/asf/camel/blob/ae366ed6/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java index deb215b..a0fe26b 100644 --- a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java @@ -503,6 +503,10 @@ public class ManagedCamelContext extends ManagedPerformanceCounter implements Ti return json; } + public String explainEndpointJson(String uri, boolean includeAllOptions) throws Exception { + return context.explainEndpointJson(uri, includeAllOptions); + } + public void reset(boolean includeRoutes) throws Exception { reset(); http://git-wip-us.apache.org/repos/asf/camel/blob/ae366ed6/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java index 6fc3dd1..85ef9f7 100644 --- a/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java +++ b/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTest.java @@ -227,6 +227,27 @@ public class ManagedCamelContextTest extends ManagementTestSupport { assertTrue(json.contains("{ \"uri\": \"direct://foo\" }")); } + public void testManagedCamelContextExplainEndpointUri() throws Exception { + // JMX tests dont work well on AIX CI servers (hangs them) + if (isPlatform("aix")) { + return; + } + + MBeanServer mbeanServer = getMBeanServer(); + ObjectName on = ObjectName.getInstance("org.apache.camel:context=19-camel-1,type=context,name=\"camel-1\""); + + // get the json + String json = (String) mbeanServer.invoke(on, "explainEndpointJson", new Object[]{"log:foo?groupDelay=2000&groupSize=5", false}, + new String[]{"java.lang.String", "boolean"}); + assertNotNull(json); + System.out.println(json); + + assertEquals(4, StringHelper.countChar(json, '{')); + assertEquals(4, StringHelper.countChar(json, '}')); + assertTrue(json.contains("\"groupDelay\": { \"value\": \"2000\", \"description\": \"Set the initial delay for stats in millis\" },")); + assertTrue(json.contains("\"groupSize\": { \"value\": \"5\", \"description\": \"An integer that specifies a group size for throughput logging.\" }")); + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() {