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

Branch: refs/heads/master
Commit: 68403097d1dbc6d2cdf00c20053e3e32c571c877
Parents: e501b5e
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Nov 6 12:45:22 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Nov 7 13:25:19 2014 +0100

----------------------------------------------------------------------
 .../management/mbean/ManagedEndpointMBean.java  |  2 +-
 .../apache/camel/component/log/LogEndpoint.java | 48 +++++++++++++++++++-
 .../camel/management/mbean/ManagedEndpoint.java | 22 ++++++---
 .../org/apache/camel/util/JsonSchemaHelper.java | 26 +++++++++++
 .../management/ManagedEndpointExplainTest.java  |  6 +--
 5 files changed, 93 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/68403097/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedEndpointMBean.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedEndpointMBean.java
 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedEndpointMBean.java
index fecaf8e..fd49aee 100644
--- 
a/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedEndpointMBean.java
+++ 
b/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedEndpointMBean.java
@@ -39,6 +39,6 @@ public interface ManagedEndpointMBean {
     String getState();
 
     @ManagedOperation(description = "Explain how this endpoint is configured 
(option, value, description)")
-    TabularData explain();
+    TabularData explain(boolean allOptions);
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/68403097/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java 
b/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
index 7277642..c5e96b5 100644
--- a/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/log/LogEndpoint.java
@@ -70,7 +70,7 @@ public class LogEndpoint extends ProcessorEndpoint {
     protected void doStart() throws Exception {
         if (logger == null) {
             // setup a new logger here
-            CamelLogger camelLogger = null;
+            CamelLogger camelLogger;
             LoggingLevel loggingLevel = LoggingLevel.INFO;
             if (level != null) {
                 loggingLevel = LoggingLevel.valueOf(level);
@@ -121,50 +121,90 @@ public class LogEndpoint extends ProcessorEndpoint {
         return "log:" + logger.toString();
     }
 
+    /**
+     * Logging level to use. Possible values: ERROR, WARN, INFO, DEBUG, TRACE, 
OFF.
+     * <p/>
+     * The default value is INFO.
+     */
     public String getLevel() {
         return level;
     }
 
+    /**
+     * Logging level to use. Possible values: ERROR, WARN, INFO, DEBUG, TRACE, 
OFF.
+     * <p/>
+     * The default value is INFO.
+     */
     public void setLevel(String level) {
         this.level = level;
     }
 
+    /**
+     * An optional Marker name to use.
+     */
     public String getMarker() {
         return marker;
     }
 
+    /**
+     * An optional Marker name to use.
+     */
     public void setMarker(String marker) {
         this.marker = marker;
     }
 
+    /**
+     * An integer that specifies a group size for throughput logging.
+     */
     public Integer getGroupSize() {
         return groupSize;
     }
 
+    /**
+     * An integer that specifies a group size for throughput logging.
+     */
     public void setGroupSize(Integer groupSize) {
         this.groupSize = groupSize;
     }
 
+    /**
+     * If specified will group message stats by this time interval (in millis)
+     */
     public Long getGroupInterval() {
         return groupInterval;
     }
 
+    /**
+     * If specified will group message stats by this time interval (in millis)
+     */
     public void setGroupInterval(Long groupInterval) {
         this.groupInterval = groupInterval;
     }
 
+    /**
+     * If true, will hide stats when no new messages have been received for a 
time interval, if false, show stats regardless of message traffic.
+     */
     public Boolean getGroupActiveOnly() {
         return groupActiveOnly;
     }
 
+    /**
+     * If true, will hide stats when no new messages have been received for a 
time interval, if false, show stats regardless of message traffic.
+     */
     public void setGroupActiveOnly(Boolean groupActiveOnly) {
         this.groupActiveOnly = groupActiveOnly;
     }
 
+    /**
+     * Set the initial delay for stats (in millis)
+     */
     public Long getGroupDelay() {
         return groupDelay;
     }
 
+    /**
+     * Set the initial delay for stats (in millis)
+     */
     public void setGroupDelay(Long groupDelay) {
         this.groupDelay = groupDelay;
     }
@@ -185,10 +225,16 @@ public class LogEndpoint extends ProcessorEndpoint {
         this.providedLogger = providedLogger;
     }
 
+    /**
+     * The logger name to use
+     */
     public String getLoggerName() {
         return loggerName;
     }
 
+    /**
+     * The logger name to use
+     */
     public void setLoggerName(String loggerName) {
         this.loggerName = loggerName;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/68403097/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 d9d569b..7f95254 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,6 +16,7 @@
  */
 package org.apache.camel.management.mbean;
 
+import java.net.URI;
 import java.util.Map;
 import javax.management.openmbean.CompositeData;
 import javax.management.openmbean.CompositeDataSupport;
@@ -31,6 +32,7 @@ import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
 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;
 
@@ -83,31 +85,39 @@ public class ManagedEndpoint implements ManagedInstance, 
ManagedEndpointMBean {
     }
 
     @Override
-    public TabularData explain() {
+    public TabularData explain(boolean allOptions) {
         try {
             TabularData answer = new 
TabularDataSupport(CamelOpenMBeanTypes.explainEndpointTabularType());
 
-            Map<String, Object> options = 
URISupport.parseParameters(endpoint.getEndpointConfiguration().getURI());
+            // 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();
 
                 String option = entry.getKey();
-
                 String value = "";
                 if (entry.getValue() != null) {
                     value = entry.getValue().toString();
                 }
                 value = URISupport.sanitizePath(value);
 
-                // TODO: get from json schema
-                String description = "";
+                // 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;
 
                 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/68403097/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 b7298da..4849e22 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
@@ -99,4 +99,30 @@ public final class JsonSchemaHelper {
         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) {
+        // 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 description
+                String value = ObjectHelper.after(line, "\"description\": \"");
+                if (value != null) {
+                    int lastQuote = value.lastIndexOf('"');
+                    value = value.substring(0, lastQuote);
+                    value = StringHelper.removeLeadingAndEndingQuotes(value);
+                    return value;
+                }
+            }
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/68403097/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 d1042b5..066fe26 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
@@ -41,11 +41,11 @@ public class ManagedEndpointExplainTest extends 
ManagementTestSupport {
         on = 
ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"mock://result\"");
         assertTrue(mbeanServer.isRegistered(on));
 
-        on = 
ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"log://foo\\?maxChars=50&showExchangeId=true\"");
+        on = 
ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"log://foo\\?groupDelay=2000&groupSize=5\"");
         assertTrue(mbeanServer.isRegistered(on));
 
         // there should be 2 options
-        TabularData data = (TabularData) mbeanServer.invoke(on, "explain", 
null, null);
+        TabularData data = (TabularData) mbeanServer.invoke(on, "explain", new 
Object[]{false}, new String[]{"boolean"});
         assertEquals(2, data.size());
     }
 
@@ -55,7 +55,7 @@ public class ManagedEndpointExplainTest extends 
ManagementTestSupport {
             @Override
             public void configure() throws Exception {
                 from("seda:test")
-                    .to("log:foo?showExchangeId=true&maxChars=50")
+                    .to("log:foo?groupDelay=2000&groupSize=5")
                     .to("mock:result");
             }
         };

Reply via email to