Updated Branches:
  refs/heads/master 9e31679fc -> 0bd7c3826

fixes CAMEL-6394 to add a createParameterJsonSchema() method on 
ComponentConfiguration along with a componentParameterJsonSchema() method on 
the MBean; so its easy for tools to enquire what parameters are available on an 
endpoint


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0bd7c382
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0bd7c382
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0bd7c382

Branch: refs/heads/master
Commit: 0bd7c3826efb53a00a75a345274828a1c33ba8a1
Parents: 9e31679
Author: James Strachan <james.strac...@gmail.com>
Authored: Fri May 24 14:08:25 2013 +0100
Committer: James Strachan <james.strac...@gmail.com>
Committed: Fri May 24 14:08:25 2013 +0100

----------------------------------------------------------------------
 .../org/apache/camel/ComponentConfiguration.java   |    8 ++++
 .../management/mbean/ManagedCamelContextMBean.java |   10 +++++
 .../camel/impl/ComponentConfigurationSupport.java  |   21 +++++++++++
 .../apache/camel/impl/ParameterConfiguration.java  |   11 ++++++
 .../management/mbean/ManagedCamelContext.java      |    8 ++++
 .../org/apache/camel/util/StringQuoteHelper.java   |   27 ++++++++++++++
 .../camel/management/EndpointCompletionTest.java   |   29 ++++++++++++++-
 7 files changed, 112 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/camel-core/src/main/java/org/apache/camel/ComponentConfiguration.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/ComponentConfiguration.java 
b/camel-core/src/main/java/org/apache/camel/ComponentConfiguration.java
index 4873ad4..9e0deb5 100644
--- a/camel-core/src/main/java/org/apache/camel/ComponentConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/ComponentConfiguration.java
@@ -149,5 +149,13 @@ public interface ComponentConfiguration {
      * @return a list of matches
      */
     List<String> completeEndpointPath(String completionText);
+
+    /**
+     * Creates a <a href="http://json-schema.org/";>JSON schema</a> 
representation of the
+     * configuration parameters for this endpoint and the types and validation 
rules.
+     *
+     * @return a JSON string which represents the JSON schema for this 
endpoints configuration parameters.
+     */
+    String createParameterJsonSchema();
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/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 4adbc30..829eede 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
@@ -181,6 +181,16 @@ public interface ManagedCamelContextMBean extends 
ManagedPerformanceCounterMBean
     @ManagedOperation(description = "Find all Camel components names available 
in the classpath")
     List<String> findComponentNames() throws Exception;
 
+
+    /**
+     * Returns the JSON schema representation of the endpoint parameters for 
the given component name
+     *
+     * @param componentName the name of the component to lookup
+     * @throws Exception is thrown if error occurred
+     */
+    @ManagedOperation(description = "Returns the JSON schema representation of 
the endpoint parameters for the given component name")
+    String componentParameterJsonSchema(String componentName) throws Exception;
+
     /**
      * Resets all the performance counters.
      *

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/camel-core/src/main/java/org/apache/camel/impl/ComponentConfigurationSupport.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/ComponentConfigurationSupport.java
 
b/camel-core/src/main/java/org/apache/camel/impl/ComponentConfigurationSupport.java
index 027a610..bbe8c0c 100644
--- 
a/camel-core/src/main/java/org/apache/camel/impl/ComponentConfigurationSupport.java
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/ComponentConfigurationSupport.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.SortedMap;
 
 import org.apache.camel.Component;
 import org.apache.camel.ComponentConfiguration;
@@ -163,6 +164,26 @@ public abstract class ComponentConfigurationSupport 
implements ComponentConfigur
         return new ArrayList<String>();
     }
 
+    public String createParameterJsonSchema() {
+        SortedMap<String,ParameterConfiguration> map = 
getParameterConfigurationMap();
+        Set<Map.Entry<String, ParameterConfiguration>> entries = 
map.entrySet();
+        StringBuilder buffer = new StringBuilder("{\n  \"properties\": {");
+        boolean first = true;
+        for (Map.Entry<String, ParameterConfiguration> entry : entries) {
+            String key = entry.getKey();
+            if (first) {
+                first = false;
+            } else {
+                buffer.append(",");
+            }
+            buffer.append("\n    ");
+            ParameterConfiguration value = entry.getValue();
+            buffer.append(value.toJson());
+        }
+        buffer.append("\n  }\n}\n");
+        return buffer.toString();
+    }
+
     /**
      * Allow implementations to validate whether a property name is valid
      * and either throw an exception or log a warning of an unknown property 
being used

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/camel-core/src/main/java/org/apache/camel/impl/ParameterConfiguration.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/ParameterConfiguration.java 
b/camel-core/src/main/java/org/apache/camel/impl/ParameterConfiguration.java
index 73e641e..5ce3d88 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/ParameterConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/ParameterConfiguration.java
@@ -20,6 +20,8 @@ import java.lang.reflect.Field;
 
 import org.apache.camel.spi.UriParam;
 
+import static org.apache.camel.util.StringQuoteHelper.doubleQuote;
+
 /**
  * Represents the configuration of a URI query parameter value to allow type 
conversion
  * and better validation of the configuration of URIs and Endpoints
@@ -59,4 +61,13 @@ public class ParameterConfiguration {
         return new AnnotatedParameterConfiguration(name, field.getType(), 
field);
     }
 
+    /**
+     * Returns the JSON format of this parameter configuration
+     */
+    public String toJson() {
+        String typeName = parameterType.getCanonicalName();
+        // TODO would be nice to add a description; wonder if we can find that 
from somewhere
+        // generated by the APT tool?
+        return doubleQuote(name) + ": { \"typeName\": " + 
doubleQuote(typeName) + " }";
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/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 cc6c10e..4229965 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
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.SortedMap;
 import java.util.concurrent.TimeUnit;
 import javax.management.MBeanServer;
 import javax.management.MBeanServerInvocationHandler;
@@ -40,6 +41,7 @@ import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
 import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
 import org.apache.camel.api.management.mbean.ManagedRouteMBean;
+import org.apache.camel.impl.ParameterConfiguration;
 import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.model.ModelHelper;
 import org.apache.camel.model.RouteDefinition;
@@ -355,6 +357,12 @@ public class ManagedCamelContext extends 
ManagedPerformanceCounter implements Ti
         return configuration.completeEndpointPath(completionText);
     }
 
+    public String componentParameterJsonSchema(String componentName) throws 
Exception {
+        Component component = context.getComponent(componentName);
+        ComponentConfiguration configuration = 
component.createComponentConfiguration();
+        return configuration.createParameterJsonSchema();
+    }
+
     public void reset(boolean includeRoutes) throws Exception {
         reset();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java 
b/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
index ea491cb..b07a198 100644
--- a/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
@@ -28,6 +28,33 @@ public final class StringQuoteHelper {
     }
 
     /**
+     * Returns the text wrapped double quotes
+     */
+    public static String doubleQuote(String text) {
+        return quote(text, "\"");
+    }
+
+    /**
+     * Returns the text wrapped single quotes
+     */
+    public static String singleQuote(String text) {
+        return quote(text, "'");
+    }
+
+    /**
+     * Wraps the text in the given quote text
+     *
+     * @param text the text to wrap in quotes
+     * @param quote the quote text added to the prefix and postfix of the text
+     *
+     * @return the text wrapped in the given quotes
+     */
+    public static String quote(String text, String quote) {
+        return quote + text + quote;
+    }
+
+
+    /**
      * Splits the input safely honoring if values is enclosed in quotes.
      * <p/>
      * Though this method does not support double quoting values. A quoted 
value

http://git-wip-us.apache.org/repos/asf/camel/blob/0bd7c382/camel-core/src/test/java/org/apache/camel/management/EndpointCompletionTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/EndpointCompletionTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/EndpointCompletionTest.java
index 51a8044..701a132 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/EndpointCompletionTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/EndpointCompletionTest.java
@@ -31,7 +31,8 @@ public class EndpointCompletionTest extends 
ManagementTestSupport {
     @SuppressWarnings("unchecked")
     public void testEndpointCompletion() throws Exception {
         MBeanServer mbeanServer = getMBeanServer();
-        ObjectName on = 
ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=context,name=\"camel-1\"");
+        ObjectName on = ObjectName.getInstance(
+                
"org.apache.camel:context=localhost/camel-1,type=context,name=\"camel-1\"");
         assertNotNull(on);
         mbeanServer.isRegistered(on);
 
@@ -45,10 +46,22 @@ public class EndpointCompletionTest extends 
ManagementTestSupport {
         completions = assertCompletion(mbeanServer, on, componentName, 
properties, "/usr/local/b");
     }
 
+
+    public void testEndpointConfigurationJson() throws Exception {
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = ObjectName.getInstance(
+                
"org.apache.camel:context=localhost/camel-1,type=context,name=\"camel-1\"");
+        assertNotNull(on);
+        mbeanServer.isRegistered(on);
+
+        assertParameterJsonSchema(mbeanServer, on, "bean");
+        assertParameterJsonSchema(mbeanServer, on, "timer");
+    }
+
     private List<String> assertCompletion(MBeanServer mbeanServer, ObjectName 
on, String componentName,
                                           HashMap<String, Object> properties, 
String completionText) throws Exception {
         Object[] params = {componentName, properties, completionText};
-        String[] signature = {"java.lang.String", "java.util.Map", 
"java.lang.String"};
+        String[] signature = { "java.lang.String",  "java.util.Map",  
"java.lang.String" };
 
         List completions = assertIsInstanceOf(List.class,
                 mbeanServer.invoke(on, "completeEndpointPath", params, 
signature));
@@ -57,6 +70,18 @@ public class EndpointCompletionTest extends 
ManagementTestSupport {
         return completions;
     }
 
+    private String assertParameterJsonSchema(MBeanServer mbeanServer, 
ObjectName on, String componentName)
+            throws Exception {
+        Object[] params = {componentName};
+        String[] signature = { "java.lang.String" };
+
+        String answer = assertIsInstanceOf(String.class,
+                mbeanServer.invoke(on, "componentParameterJsonSchema", params, 
signature));
+
+        LOG.info("Component " + componentName + " returned JSON: " + answer);
+        return answer;
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

Reply via email to