You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/01/13 18:04:33 UTC

[07/13] camel git commit: CAMEL-8232: Camel Toolbox - Include documentation about data formats

CAMEL-8232: Camel Toolbox - Include documentation about data formats


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

Branch: refs/heads/master
Commit: e3d6ef4b822ba5b684f42a73fe2a2d6355f850ba
Parents: 3f8efc1
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 13 13:28:15 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 13 17:33:29 2015 +0100

----------------------------------------------------------------------
 .../camel/maven/packaging/JSonSchemaHelper.java |  91 +++++++++
 .../maven/packaging/PackageDataFormatMojo.java  | 196 ++++++++++++++++++-
 .../camel/maven/packaging/PackageHelper.java    |   7 +
 3 files changed, 288 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e3d6ef4b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
new file mode 100644
index 0000000..061ecbd
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.maven.packaging;
+
+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;
+
+public final class JSonSchemaHelper {
+
+    private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"");
+
+    private JSonSchemaHelper() {
+    }
+
+    /**
+     * Parses the json schema to split it into a list or rows, where each row contains key value pairs with the metadata
+     *
+     * @param group the group to parse from such as <tt>component</tt>, <tt>componentProperties</tt>, or <tt>properties</tt>.
+     * @param json the json
+     * @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>> parseJsonSchema(String group, String json, boolean parseProperties) {
+        List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
+        if (json == null) {
+            return answer;
+        }
+
+        boolean found = false;
+
+        // parse line by line
+        String[] lines = json.split("\n");
+        for (String line : lines) {
+            // we need to find the group first
+            if (!found) {
+                String s = line.trim();
+                found = s.startsWith("\"" + group + "\":");
+                continue;
+            }
+
+            // we should stop when we end the group
+            if (line.equals("  },") || line.equals("  }")) {
+                break;
+            }
+
+            Map<String, String> row = new LinkedHashMap<String, String>();
+            Matcher matcher = PATTERN.matcher(line);
+
+            String key;
+            if (parseProperties) {
+                // when parsing properties the first key is given as name, so the first parsed token is the value of the name
+                key = "name";
+            } else {
+                key = null;
+            }
+            while (matcher.find()) {
+                if (key == null) {
+                    key = matcher.group(1);
+                } else {
+                    String value = matcher.group(1);
+                    row.put(key, value);
+                    // reset
+                    key = null;
+                }
+            }
+            if (!row.isEmpty()) {
+                answer.add(row);
+            }
+        }
+
+        return answer;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e3d6ef4b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
index 8157d07..a49590e 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
@@ -18,14 +18,20 @@ package org.apache.camel.maven.packaging;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -33,6 +39,7 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
 
+import static org.apache.camel.maven.packaging.PackageHelper.after;
 import static org.apache.camel.maven.packaging.PackageHelper.loadText;
 import static org.apache.camel.maven.packaging.PackageHelper.parseAsMap;
 
@@ -61,6 +68,13 @@ public class PackageDataFormatMojo extends AbstractMojo {
     protected File outDir;
 
     /**
+     * The output directory for generated dataformats file
+     *
+     * @parameter default-value="${project.build.directory}/classes"
+     */
+    protected File schemaOutDir;
+
+    /**
      * Maven ProjectHelper.
      *
      * @component
@@ -72,13 +86,12 @@ public class PackageDataFormatMojo extends AbstractMojo {
      * Execute goal.
      *
      * @throws org.apache.maven.plugin.MojoExecutionException execution of the main class or one of the
-     *                 threads it generated failed.
-     * @throws org.apache.maven.plugin.MojoFailureException something bad happened...
+     *                                                        threads it generated failed.
+     * @throws org.apache.maven.plugin.MojoFailureException   something bad happened...
      */
     public void execute() throws MojoExecutionException, MojoFailureException {
         File camelMetaDir = new File(outDir, "META-INF/services/org/apache/camel/");
 
-
         Map<String, String> javaTypes = new HashMap<String, String>();
 
         StringBuilder buffer = new StringBuilder();
@@ -89,7 +102,7 @@ public class PackageDataFormatMojo extends AbstractMojo {
                 f = new File(project.getBasedir(), r.getDirectory());
             }
             f = new File(f, "META-INF/services/org/apache/camel/dataformat");
-            
+
             if (f.exists() && f.isDirectory()) {
                 File[] files = f.listFiles();
                 if (files != null) {
@@ -119,10 +132,60 @@ public class PackageDataFormatMojo extends AbstractMojo {
             }
         }
 
-        getLog().debug("Java types found " + javaTypes);
+        // find camel-core and grab the data format model from there, and enrich this model with information from this artifact
+        // and create json schema model file for this data format
+        try {
+            Artifact camelCore = findCamelCoreArtifact(project);
+            if (camelCore != null) {
+                File core = camelCore.getFile();
+                URL url = new URL("file", null, core.getAbsolutePath());
+                URLClassLoader loader = new URLClassLoader(new URL[]{url});
 
-        // TODO: find model from camel-core and enrich to generate a .json
+                for (String name : javaTypes.keySet()) {
+                    InputStream is = loader.getResourceAsStream("org/apache/camel/model/dataformat/" + name + ".json");
+                    if (is != null) {
+                        String json = loadText(is);
+                        if (json != null) {
+                            DataFormatModel dataFormatModel = new DataFormatModel();
+                            dataFormatModel.setName(name);
+                            dataFormatModel.setDescription(project.getDescription());
+                            dataFormatModel.setJavaType(javaTypes.get(name));
+                            dataFormatModel.setGroupId(project.getGroupId());
+                            dataFormatModel.setArtifactId(project.getArtifactId());
+                            dataFormatModel.setVersion(project.getVersion());
 
+                            List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
+                            for (Map<String, String> row : rows) {
+                                if (row.containsKey("label")) {
+                                    dataFormatModel.setLabel(row.get("label"));
+                                } else {
+                                    dataFormatModel.setLabel("");
+                                }
+                            }
+                            getLog().debug("Model " + dataFormatModel);
+
+                            // build json schema for the data format
+                            String properties = after(json, "  \"properties\": {");
+                            String schema = createParameterJsonSchema(dataFormatModel, properties);
+                            getLog().debug("JSon schema\n" + schema);
+
+                            // write this to the directory
+                            File dir = new File(schemaOutDir, schemaSubDirectory(dataFormatModel.getJavaType()));
+                            dir.mkdirs();
+
+                            File out = new File(dir, name + ".json");
+                            FileOutputStream fos = new FileOutputStream(out, false);
+                            fos.write(schema.getBytes());
+                            fos.close();
+
+                            getLog().info("Generated " + out + " containing JSon schema for " + name + " data format");
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new MojoExecutionException("Error loading data formation model from camel-core. Reason: " + e, e);
+        }
 
         if (count > 0) {
             Properties properties = new Properties();
@@ -154,4 +217,125 @@ public class PackageDataFormatMojo extends AbstractMojo {
         }
     }
 
+    private Artifact findCamelCoreArtifact(MavenProject project) {
+        Iterator it = project.getArtifacts().iterator();
+        while (it.hasNext()) {
+            Artifact artifact = (Artifact) it.next();
+            if (artifact.getGroupId().equals("org.apache.camel") && artifact.getArtifactId().equals("camel-core")) {
+                return artifact;
+            }
+        }
+        it = project.getDependencyArtifacts().iterator();
+        while (it.hasNext()) {
+            Artifact artifact = (Artifact) it.next();
+            if (artifact.getGroupId().equals("org.apache.camel") && artifact.getArtifactId().equals("camel-core")) {
+                return artifact;
+            }
+        }
+        return null;
+    }
+
+    private String schemaSubDirectory(String javaType) {
+        int idx = javaType.lastIndexOf('.');
+        String pckName = javaType.substring(0, idx);
+        return pckName.replace('.', '/');
+    }
+
+    private String createParameterJsonSchema(DataFormatModel dataFormatModel, String schema) {
+        StringBuilder buffer = new StringBuilder("{");
+        // component model
+        buffer.append("\n \"dataformat\": {");
+        buffer.append("\n    \"name\": \"").append(dataFormatModel.getName()).append("\",");
+        buffer.append("\n    \"description\": \"").append(dataFormatModel.getDescription()).append("\",");
+        buffer.append("\n    \"label\": \"").append(dataFormatModel.getLabel()).append("\",");
+        buffer.append("\n    \"javaType\": \"").append(dataFormatModel.getJavaType()).append("\",");
+        buffer.append("\n    \"groupId\": \"").append(dataFormatModel.getGroupId()).append("\",");
+        buffer.append("\n    \"artifactId\": \"").append(dataFormatModel.getArtifactId()).append("\",");
+        buffer.append("\n    \"version\": \"").append(dataFormatModel.getVersion()).append("\"");
+        buffer.append("\n  },");
+
+        buffer.append("\n  \"properties\": {");
+        buffer.append(schema);
+        return buffer.toString();
+    }
+
+    private class DataFormatModel {
+        private String name;
+        private String description;
+        private String label;
+        private String javaType;
+        private String groupId;
+        private String artifactId;
+        private String version;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public String getDescription() {
+            return description;
+        }
+
+        public void setDescription(String description) {
+            this.description = description;
+        }
+
+        public String getLabel() {
+            return label;
+        }
+
+        public void setLabel(String label) {
+            this.label = label;
+        }
+
+        public String getJavaType() {
+            return javaType;
+        }
+
+        public void setJavaType(String javaType) {
+            this.javaType = javaType;
+        }
+
+        public String getGroupId() {
+            return groupId;
+        }
+
+        public void setGroupId(String groupId) {
+            this.groupId = groupId;
+        }
+
+        public String getArtifactId() {
+            return artifactId;
+        }
+
+        public void setArtifactId(String artifactId) {
+            this.artifactId = artifactId;
+        }
+
+        public String getVersion() {
+            return version;
+        }
+
+        public void setVersion(String version) {
+            this.version = version;
+        }
+
+        @Override
+        public String toString() {
+            return "DataFormatModel["
+                    + "name='" + name + '\''
+                    + ", description='" + description + '\''
+                    + ", label='" + label + '\''
+                    + ", javaType='" + javaType + '\''
+                    + ", groupId='" + groupId + '\''
+                    + ", artifactId='" + artifactId + '\''
+                    + ", version='" + version + '\''
+                    + ']';
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e3d6ef4b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
index a283637..1ae21c6 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
@@ -61,6 +61,13 @@ public final class PackageHelper {
         }
     }
 
+    public static String after(String text, String after) {
+        if (!text.contains(after)) {
+            return null;
+        }
+        return text.substring(text.indexOf(after) + after.length());
+    }
+
     /**
      * Parses the text as a map (eg key=value)
      * @param data the data