CAMEL-7999: Maven plugin to generate a list of all Camel components.

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

Branch: refs/heads/master
Commit: 908b57523a6956d48e8cdcab0c01a580f3c06514
Parents: fa6e03b
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Nov 13 16:16:02 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Nov 13 16:16:02 2014 +0100

----------------------------------------------------------------------
 platforms/commands/commands-core/pom.xml        |  21 +++-
 .../camel/commands/AbstractCamelController.java |   4 +
 .../commands/catalog/CamelComponentCatalog.java |  35 ++++++
 .../catalog/CamelComponentCatalogService.java   | 120 +++++++++++++++++++
 .../catalog/CamelComponentCatalogTest.java      |  63 ++++++++++
 .../maven/packaging/CommandsPrepareMojo.java    | 120 +++++++++++++++++++
 6 files changed, 359 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/platforms/commands/commands-core/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/pom.xml 
b/platforms/commands/commands-core/pom.xml
index d94ad2a..bb616c7 100644
--- a/platforms/commands/commands-core/pom.xml
+++ b/platforms/commands/commands-core/pom.xml
@@ -86,16 +86,16 @@
         <version>1.7</version>
         <executions>
           <execution>
-            <phase>generate-sources</phase>
+            <phase>process-resources</phase>
             <configuration>
               <target>
                 <echo>Copying component json descriptors</echo>
-                <copy todir="${project.basedir}/target/classes/components/" 
flatten="true">
+                <copy 
todir="${project.basedir}/target/classes/org/apache/camel/commands/catalog/components/"
 flatten="true">
                   <fileset dir="${camel.core.dir}">
                     <include 
name="**/target/classes/org/apache/camel/**/*.json"/>
                   </fileset>
                 </copy>
-                <copy todir="${project.basedir}/target/classes/components/" 
flatten="true">
+                <copy 
todir="${project.basedir}/target/classes/org/apache/camel/commands/catalog/components/"
 flatten="true">
                   <fileset dir="${components.dir}">
                     <include 
name="**/target/classes/org/apache/camel/**/*.json"/>
                   </fileset>
@@ -111,13 +111,26 @@
       </plugin>
 
       <plugin>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>camel-package-maven-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <goals>
+              <goal>include-components-list</goal>
+            </goals>
+            <phase>process-resources</phase>
+          </execution>
+        </executions>
+      </plugin>
+
+      <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
         <extensions>true</extensions>
         <configuration>
           <instructions>
             <Private-Package>
-              components,
               org.apache.camel.commands.internal
             </Private-Package>
           </instructions>

http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
index e96a069..5781342 100644
--- 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
@@ -28,6 +28,8 @@ import java.util.Properties;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Route;
+import org.apache.camel.commands.catalog.CamelComponentCatalog;
+import org.apache.camel.commands.catalog.CamelComponentCatalogService;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.spi.RestRegistry;
@@ -38,6 +40,8 @@ import org.apache.camel.util.JsonSchemaHelper;
  */
 public abstract class AbstractCamelController implements CamelController {
 
+    private CamelComponentCatalog catalog = new CamelComponentCatalogService();
+
     public CamelContext getCamelContext(String name) {
         for (CamelContext camelContext : this.getCamelContexts()) {
             if (camelContext.getName().equals(name)) {

http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java
new file mode 100644
index 0000000..65ba2af
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java
@@ -0,0 +1,35 @@
+/**
+ * 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.commands.catalog;
+
+import java.util.List;
+
+public interface CamelComponentCatalog {
+
+    /**
+     * Find all the component names from the Camel catalog
+     */
+    List<String> findComponentNames();
+
+    /**
+     * Returns the component information as JSon format.
+     *
+     * @param name the component name
+     * @return component details in JSon
+     */
+    String componentJSonSchema(String name);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalogService.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalogService.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalogService.java
new file mode 100644
index 0000000..a163a86
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalogService.java
@@ -0,0 +1,120 @@
+/**
+ * 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.commands.catalog;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CamelComponentCatalogService implements CamelComponentCatalog {
+
+    private static String COMPONENTS_CATALOG = 
"org/apache/camel/commands/catalog/components-catalog";
+    private static String COMPONENTS_JSON = 
"org/apache/camel/commands/catalog/components";
+
+    @Override
+    public List<String> findComponentNames() {
+        List<String> names = new ArrayList<String>();
+
+        InputStream is = 
CamelComponentCatalogService.class.getClassLoader().getResourceAsStream(COMPONENTS_CATALOG);
+        if (is != null) {
+            try {
+                loadLines(is, names);
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+        return names;
+    }
+
+    @Override
+    public String componentJSonSchema(String name) {
+        String file = COMPONENTS_JSON + "/" + name + ".json";
+
+        InputStream is = 
CamelComponentCatalogService.class.getClassLoader().getResourceAsStream(file);
+        if (is != null) {
+            try {
+                return loadText(is);
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Loads the entire stream into memory as a String and returns it.
+     * <p/>
+     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
+     * terminator at the of the text.
+     * <p/>
+     * Warning, don't use for crazy big streams :)
+     */
+    private static void loadLines(InputStream in, List<String> lines) throws 
IOException {
+        InputStreamReader isr = new InputStreamReader(in);
+        try {
+            BufferedReader reader = new LineNumberReader(isr);
+            while (true) {
+                String line = reader.readLine();
+                if (line != null) {
+                    lines.add(line);
+                } else {
+                    break;
+                }
+            }
+        } finally {
+            isr.close();
+            in.close();
+        }
+    }
+
+    /**
+     * Loads the entire stream into memory as a String and returns it.
+     * <p/>
+     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
+     * terminator at the of the text.
+     * <p/>
+     * Warning, don't use for crazy big streams :)
+     */
+    public static String loadText(InputStream in) throws IOException {
+        StringBuilder builder = new StringBuilder();
+        InputStreamReader isr = new InputStreamReader(in);
+        try {
+            BufferedReader reader = new LineNumberReader(isr);
+            while (true) {
+                String line = reader.readLine();
+                if (line != null) {
+                    builder.append(line);
+                    builder.append("\n");
+                } else {
+                    break;
+                }
+            }
+            return builder.toString();
+        } finally {
+            isr.close();
+            in.close();
+        }
+    }
+
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
 
b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
new file mode 100644
index 0000000..1090670
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.commands.catalog;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CamelComponentCatalogTest extends TestCase {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CamelComponentCatalogTest.class);
+
+    @Test
+    public void testFindComponentNames() {
+        CamelComponentCatalog catalog = new CamelComponentCatalogService();
+        List<String> names = catalog.findComponentNames();
+
+        assertNotNull(names);
+
+        LOG.info("Found {} names", names.size());
+        assertTrue("Should find some components", names.size() > 0);
+    }
+
+    @Test
+    public void testCoreComponentJson() {
+        CamelComponentCatalog catalog = new CamelComponentCatalogService();
+        String json = catalog.componentJSonSchema("bean");
+
+        assertNotNull(json);
+        LOG.info(json);
+
+        assertTrue("Should find bean component", json.contains("bean"));
+    }
+
+    @Test
+    public void testFtpComponentJson() {
+        CamelComponentCatalog catalog = new CamelComponentCatalogService();
+        String json = catalog.componentJSonSchema("ftp");
+
+        assertNotNull(json);
+        LOG.info(json);
+
+        assertTrue("Should find ftp component", json.contains("ftp"));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/908b5752/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java
----------------------------------------------------------------------
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java
 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java
new file mode 100644
index 0000000..6f2ff69
--- /dev/null
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java
@@ -0,0 +1,120 @@
+/**
+ * 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.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectHelper;
+
+/**
+ * Analyses the Camel plugins in a project and generates extra descriptor 
information for easier auto-discovery in Camel.
+ *
+ * @goal include-components-list
+ * @execute phase="process-resources"
+ */
+public class CommandsPrepareMojo extends AbstractMojo {
+
+    /**
+     * The maven project.
+     *
+     * @parameter property="project"
+     * @required
+     * @readonly
+     */
+    protected MavenProject project;
+
+    /**
+     * The output directory for generated components file
+     *
+     * @parameter 
default-value="${project.build.directory}/classes/org/apache/camel/commands/catalog/components"
+     */
+    protected File outDir;
+
+    /**
+     * The components directory where all the Apache Camel components are
+     *
+     * @parameter 
default-value="${project.build.directory}/../../../components"
+     */
+    protected File componentsDir;
+
+    /**
+     * The camel-core directory where camel-core components are
+     *
+     * @parameter 
default-value="${project.build.directory}/../../../camel-core"
+     */
+    protected File coreDir;
+
+    /**
+     * Maven ProjectHelper.
+     *
+     * @component
+     * @readonly
+     */
+    private MavenProjectHelper projectHelper;
+
+    /**
+     * 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...
+     */
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        getLog().info("Copying all Camel component json descriptors");
+
+        // TODO: add the scan and copy code here
+
+        File all = new File(outDir, "../components-catalog");
+        try {
+            FileOutputStream fos = new FileOutputStream(all, false);
+
+            String[] names = outDir.list();
+            List<String> components = new ArrayList<String>();
+            // sort the names
+            for (String name : names) {
+                if (name.endsWith(".json")) {
+                    // strip out .json from the name
+                    String componentName = name.substring(0, name.length() - 
5);
+                    components.add(componentName);
+                }
+            }
+
+            Collections.sort(components);
+            for (String name : components) {
+                fos.write(name.getBytes());
+                fos.write("\n".getBytes());
+            }
+
+            fos.close();
+
+            getLog().info("Found " + components.size() + " components to 
include in file: " + all);
+
+        } catch (IOException e) {
+            throw new MojoFailureException("Error writing to file " + all);
+        }
+    }
+
+}
\ No newline at end of file

Reply via email to