camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-10721: Refactored a bit
Date Thu, 19 Jan 2017 11:34:44 GMT
Repository: camel
Updated Branches:
  refs/heads/master db8740ce9 -> 065561d24


CAMEL-10721: Refactored a bit


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

Branch: refs/heads/master
Commit: 065561d24d6f0b06afc46fc042825574045fa5d2
Parents: db8740c
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Jan 19 12:21:47 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Jan 19 12:34:33 2017 +0100

----------------------------------------------------------------------
 .../maven/connector/CollectionStringBuffer.java | 57 ------------
 .../camel/maven/connector/ConnectorMojo.java    | 94 ++++----------------
 .../camel/maven/connector/FileHelper.java       | 44 +++++++++
 .../apache/camel/maven/connector/GitHelper.java |  6 ++
 .../camel/maven/connector/JSonSchemaHelper.java |  3 +
 .../camel/maven/connector/StringHelper.java     | 61 +++++++++++++
 6 files changed, 130 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/CollectionStringBuffer.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/CollectionStringBuffer.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/CollectionStringBuffer.java
deleted file mode 100644
index bd11654..0000000
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/CollectionStringBuffer.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/**
- * 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.connector;
-
-public class CollectionStringBuffer {
-    private final StringBuilder buffer = new StringBuilder();
-    private String separator;
-    private boolean first = true;
-
-    public CollectionStringBuffer() {
-        this(", ");
-    }
-
-    public CollectionStringBuffer(String separator) {
-        this.separator = separator;
-    }
-
-    @Override
-    public String toString() {
-        return buffer.toString();
-    }
-
-    public void append(Object value) {
-        if (first) {
-            first = false;
-        } else {
-            buffer.append(separator);
-        }
-        buffer.append(value);
-    }
-
-    public String getSeparator() {
-        return separator;
-    }
-
-    public void setSeparator(String separator) {
-        this.separator = separator;
-    }
-
-    public boolean isEmpty() {
-        return first;
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/ConnectorMojo.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/ConnectorMojo.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/ConnectorMojo.java
index 0a4637a..208c4e5 100644
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/ConnectorMojo.java
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/ConnectorMojo.java
@@ -19,16 +19,13 @@ package org.apache.camel.maven.connector;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.LineNumberReader;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -63,7 +60,8 @@ public class ConnectorMojo extends AbstractJarMojo {
 
     @Override
     protected String getClassifier() {
-        return "camel-connector";
+        // no classifier
+        return null;
     }
 
     @Override
@@ -157,6 +155,12 @@ public class ConnectorMojo extends AbstractJarMojo {
                     FileOutputStream fos = new FileOutputStream(out, false);
                     fos.write(newJson.getBytes());
                     fos.close();
+
+                    // also write the file in the root folder so its easier to find that
for tooling
+                    out = new File(classesDirectory,"camel-connector-schema.json");
+                    fos = new FileOutputStream(out, false);
+                    fos.write(newJson.getBytes());
+                    fos.close();
                 }
 
                 // build json schema for component that only has the selectable options
@@ -171,7 +175,7 @@ public class ConnectorMojo extends AbstractJarMojo {
     private String extractJavaType(String scheme) throws Exception {
         File file = new File(classesDirectory, "META-INF/services/org/apache/camel/component/"
+ scheme);
         if (file.exists()) {
-            List<String> lines = loadFile(file);
+            List<String> lines = FileHelper.loadFile(file);
             String fqn = extractClass(lines);
             return fqn;
         }
@@ -228,7 +232,6 @@ public class ConnectorMojo extends AbstractJarMojo {
             String key = row.get("name");
             row.remove("name");
 
-            // TODO: if no options should we include all by default instead?
             if (options == null || !options.contains(key)) {
                 continue;
             }
@@ -265,7 +268,7 @@ public class ConnectorMojo extends AbstractJarMojo {
         String baseScheme = (String) dto.get("baseScheme");
         String source = (String) dto.get("source");
         String title = (String) dto.get("name");
-        String scheme = camelCaseToDash(title);
+        String scheme = StringHelper.camelCaseToDash(title);
         String baseSyntax = getOption(rows, "syntax");
         String syntax = baseSyntax.replaceFirst(baseScheme, scheme);
 
@@ -274,11 +277,7 @@ public class ConnectorMojo extends AbstractJarMojo {
         String label = null;
         List<String> labels = (List<String>) dto.get("labels");
         if (labels != null) {
-            CollectionStringBuffer csb = new CollectionStringBuffer(",");
-            for (String s : labels) {
-                csb.append(s);
-            }
-            label = csb.toString();
+            label = labels.stream().collect(Collectors.joining(","));
         }
         String async = getOption(rows, "async");
         String producerOnly = "To".equals(source) ? "true" : null;
@@ -292,10 +291,10 @@ public class ConnectorMojo extends AbstractJarMojo {
         StringBuilder sb = new StringBuilder();
         sb.append("  \"component\": {\n");
         if (gitUrl != null) {
-            sb.append("    \"girUrl\": \"" + nullSafe(gitUrl) + "\",\n");
+            sb.append("    \"girUrl\": \"" + StringHelper.nullSafe(gitUrl) + "\",\n");
         }
         sb.append("    \"kind\": \"component\",\n");
-        sb.append("    \"baseScheme\": \"" + nullSafe(baseScheme) + "\",\n");
+        sb.append("    \"baseScheme\": \"" + StringHelper.nullSafe(baseScheme) + "\",\n");
         sb.append("    \"scheme\": \"" + scheme + "\",\n");
         sb.append("    \"syntax\": \"" + syntax + "\",\n");
         sb.append("    \"title\": \"" + title + "\",\n");
@@ -326,10 +325,6 @@ public class ConnectorMojo extends AbstractJarMojo {
         return sb.toString();
     }
 
-    private static String nullSafe(String text) {
-        return text != null ? text : "";
-    }
-
     /**
      * Finds and embeds the Camel component JSon schema file
      */
@@ -357,7 +352,7 @@ public class ConnectorMojo extends AbstractJarMojo {
 
                             InputStream is = child.getResourceAsStream("META-INF/services/org/apache/camel/component/"
+ scheme);
                             if (is != null) {
-                                List<String> lines = loadFile(is);
+                                List<String> lines = FileHelper.loadFile(is);
                                 String fqn = extractClass(lines);
                                 is.close();
 
@@ -367,7 +362,7 @@ public class ConnectorMojo extends AbstractJarMojo {
 
                                 is = child.getResourceAsStream(name);
                                 if (is != null) {
-                                    List<String> schema = loadFile(is);
+                                    List<String> schema = FileHelper.loadFile(is);
                                     is.close();
 
                                     // write schema to file
@@ -422,61 +417,4 @@ public class ConnectorMojo extends AbstractJarMojo {
         return (String) map.get("baseVersion");
     }
 
-    private List<String> loadFile(File file) throws Exception {
-        List<String> lines = new ArrayList<>();
-        LineNumberReader reader = new LineNumberReader(new FileReader(file));
-
-        String line;
-        do {
-            line = reader.readLine();
-            if (line != null) {
-                lines.add(line);
-            }
-        } while (line != null);
-        reader.close();
-
-        return lines;
-    }
-
-    private List<String> loadFile(InputStream fis) throws Exception {
-        List<String> lines = new ArrayList<>();
-        LineNumberReader reader = new LineNumberReader(new InputStreamReader(fis));
-
-        String line;
-        do {
-            line = reader.readLine();
-            if (line != null) {
-                lines.add(line);
-            }
-        } while (line != null);
-        reader.close();
-
-        return lines;
-    }
-
-    public static String camelCaseToDash(String value) {
-        StringBuilder sb = new StringBuilder(value.length());
-        boolean dash = false;
-
-        for (char c : value.toCharArray()) {
-            // skip dash in start
-            if (sb.length() > 0 & Character.isUpperCase(c)) {
-                dash = true;
-            }
-            if (dash) {
-                sb.append('-');
-                sb.append(Character.toLowerCase(c));
-            } else {
-                // lower case first
-                if (sb.length() == 0) {
-                    sb.append(Character.toLowerCase(c));
-                } else {
-                    sb.append(c);
-                }
-            }
-            dash = false;
-        }
-        return sb.toString();
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/FileHelper.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/FileHelper.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/FileHelper.java
index 30f187a..a7c7502 100644
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/FileHelper.java
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/FileHelper.java
@@ -17,11 +17,18 @@
 package org.apache.camel.maven.connector;
 
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
+import java.util.ArrayList;
+import java.util.List;
 
+/**
+ * Utility methods for files.
+ */
 public final class FileHelper {
 
     private FileHelper() {
@@ -56,4 +63,41 @@ public final class FileHelper {
         }
     }
 
+    /**
+     * Loads the file
+     */
+    public static List<String> loadFile(File file) throws Exception {
+        List<String> lines = new ArrayList<>();
+        LineNumberReader reader = new LineNumberReader(new FileReader(file));
+
+        String line;
+        do {
+            line = reader.readLine();
+            if (line != null) {
+                lines.add(line);
+            }
+        } while (line != null);
+        reader.close();
+
+        return lines;
+    }
+
+    /**
+     * Loads the file
+     */
+    public static List<String> loadFile(InputStream fis) throws Exception {
+        List<String> lines = new ArrayList<>();
+        LineNumberReader reader = new LineNumberReader(new InputStreamReader(fis));
+
+        String line;
+        do {
+            line = reader.readLine();
+            if (line != null) {
+                lines.add(line);
+            }
+        } while (line != null);
+        reader.close();
+
+        return lines;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/GitHelper.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/GitHelper.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/GitHelper.java
index 023a948..bd879f7 100644
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/GitHelper.java
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/GitHelper.java
@@ -24,11 +24,17 @@ import java.io.StringReader;
 import java.util.HashMap;
 import java.util.Map;
 
+/**
+ * Utility methods for git.
+ */
 public final class GitHelper {
 
     private GitHelper() {
     }
 
+    /**
+     * Finds the folder where <tt>.git</tt> is located in the project
+     */
     public static File findGitFolder() {
         File baseDir = new File("").getAbsoluteFile();
         return findGitFolder(baseDir);

http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/JSonSchemaHelper.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/JSonSchemaHelper.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/JSonSchemaHelper.java
index d42e5e3..018d548 100644
--- a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/JSonSchemaHelper.java
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/JSonSchemaHelper.java
@@ -25,6 +25,9 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+/**
+ * Basic JSon parser to read the Camel component JSon schema files.
+ */
 public final class JSonSchemaHelper {
 
     private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"|\\[(.+)\\]");

http://git-wip-us.apache.org/repos/asf/camel/blob/065561d2/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/StringHelper.java
----------------------------------------------------------------------
diff --git a/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/StringHelper.java
b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/StringHelper.java
new file mode 100644
index 0000000..10d514b
--- /dev/null
+++ b/connectors/camel-connector-maven-plugin/src/main/java/org/apache/camel/maven/connector/StringHelper.java
@@ -0,0 +1,61 @@
+/**
+ * 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.connector;
+
+/**
+ * Utility methods for String.
+ */
+public final class StringHelper {
+
+    private StringHelper() {
+    }
+
+    /**
+     * Converts the value to use dash style instead of upper cased
+     */
+    public static String camelCaseToDash(String value) {
+        StringBuilder sb = new StringBuilder(value.length());
+        boolean dash = false;
+
+        for (char c : value.toCharArray()) {
+            // skip dash in start
+            if (sb.length() > 0 & Character.isUpperCase(c)) {
+                dash = true;
+            }
+            if (dash) {
+                sb.append('-');
+                sb.append(Character.toLowerCase(c));
+            } else {
+                // lower case first
+                if (sb.length() == 0) {
+                    sb.append(Character.toLowerCase(c));
+                } else {
+                    sb.append(c);
+                }
+            }
+            dash = false;
+        }
+        return sb.toString();
+    }
+
+    /**
+     * Returns the string value (uses empty string for <tt>null</tt> values)
+     */
+    public static String nullSafe(String text) {
+        return text != null ? text : "";
+    }
+}


Mime
View raw message