camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [4/4] camel git commit: CAMEL-10898: camel-catalog - Allow to add custom component/connector by download JAR via maven
Date Mon, 27 Feb 2017 13:07:58 GMT
CAMEL-10898: camel-catalog - Allow to add custom component/connector by download JAR via maven


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

Branch: refs/heads/master
Commit: eec3f8436d6ef4db8ff3b76fca27c6880d00b1a9
Parents: 59170dd
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Feb 27 13:12:47 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Feb 27 14:07:45 2017 +0100

----------------------------------------------------------------------
 platforms/camel-catalog-maven/pom.xml           |   5 +
 .../catalog/maven/ComponentArtifactHelper.java  |  91 +++++++++++++++++
 .../maven/DefaultMavenArtifactProvider.java     | 101 +++++++++++++++++++
 .../catalog/maven/MavenArtifactProvider.java    |  45 +++++++++
 .../maven/MavenArtifactProviderTest.java        |  40 ++++++++
 5 files changed, 282 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eec3f843/platforms/camel-catalog-maven/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog-maven/pom.xml b/platforms/camel-catalog-maven/pom.xml
index cc82045..e204c6a 100644
--- a/platforms/camel-catalog-maven/pom.xml
+++ b/platforms/camel-catalog-maven/pom.xml
@@ -43,6 +43,11 @@
       <artifactId>camel-catalog</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+
     <!-- use groovy grape to download JARs as that is easy to use -->
     <dependency>
       <groupId>org.codehaus.groovy</groupId>

http://git-wip-us.apache.org/repos/asf/camel/blob/eec3f843/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/ComponentArtifactHelper.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/ComponentArtifactHelper.java
b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/ComponentArtifactHelper.java
new file mode 100644
index 0000000..234ee69
--- /dev/null
+++ b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/ComponentArtifactHelper.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.catalog.maven;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.camel.catalog.CatalogHelper.loadText;
+
+/**
+ * Helper methods for loading content from Camel components that the {@link org.apache.camel.catalog.CamelCatalog}
requires.
+ */
+public final class ComponentArtifactHelper {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ComponentArtifactHelper.class);
+
+    private ComponentArtifactHelper() {
+    }
+
+    public static Properties loadComponentProperties(ClassLoader classLoader) {
+        Properties answer = new Properties();
+        try {
+            // load the component files using the recommended way by a component.properties
file
+            InputStream is = classLoader.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
+            if (is != null) {
+                answer.load(is);
+            }
+        } catch (Throwable e) {
+            LOG.warn("Error loading META-INF/services/org/apache/camel/component.properties
file", e);
+        }
+        return answer;
+    }
+
+    public static String extractComponentJavaType(ClassLoader classLoader, String scheme)
{
+        try {
+            InputStream is = classLoader.getResourceAsStream("META-INF/services/org/apache/camel/component/"
+ scheme);
+            if (is != null) {
+                Properties props = new Properties();
+                props.load(is);
+                return (String) props.get("class");
+            }
+        } catch (Throwable e) {
+            LOG.warn("Error loading META-INF/services/org/apache/camel/component/" + scheme
+ " file", e);
+        }
+
+        return null;
+    }
+
+    public static String loadComponentJSonSchema(ClassLoader classLoader, String scheme)
{
+        String answer = null;
+
+        String path = null;
+        String javaType = extractComponentJavaType(classLoader, scheme);
+        if (javaType != null) {
+            int pos = javaType.lastIndexOf(".");
+            path = javaType.substring(0, pos);
+            path = path.replace('.', '/');
+            path = path + "/" + scheme + ".json";
+        }
+
+        if (path != null) {
+            try {
+                InputStream is = classLoader.getResourceAsStream(path);
+                if (is != null) {
+                    answer = loadText(is);
+                }
+            } catch (Throwable e) {
+                LOG.warn("Error loading " + path + " file", e);
+            }
+        }
+
+        return answer;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/eec3f843/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/DefaultMavenArtifactProvider.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/DefaultMavenArtifactProvider.java
b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/DefaultMavenArtifactProvider.java
new file mode 100644
index 0000000..cafbd4b
--- /dev/null
+++ b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/DefaultMavenArtifactProvider.java
@@ -0,0 +1,101 @@
+/**
+ * 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.catalog.maven;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import groovy.grape.Grape;
+import groovy.lang.GroovyClassLoader;
+import org.apache.camel.catalog.CamelCatalog;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.camel.catalog.maven.ComponentArtifactHelper.extractComponentJavaType;
+import static org.apache.camel.catalog.maven.ComponentArtifactHelper.loadComponentJSonSchema;
+import static org.apache.camel.catalog.maven.ComponentArtifactHelper.loadComponentProperties;
+
+/**
+ * Default {@link MavenArtifactProvider} which uses Groovy Grape to download the artifact.
+ */
+public class DefaultMavenArtifactProvider implements MavenArtifactProvider {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultMavenArtifactProvider.class);
+
+    public void addMavenRepository(String name, String url) {
+        Map<String, Object> repo = new HashMap<>();
+        repo.put("name", name);
+        repo.put("root", url);
+        Grape.addResolver(repo);
+    }
+
+    @Override
+    public boolean addArtifactToCatalog(CamelCatalog camelCatalog, String groupId, String
artifactId, String version) {
+        try {
+            Grape.setEnableAutoDownload(true);
+
+            final ClassLoader classLoader = new GroovyClassLoader();
+
+            Map<String, Object> param = new HashMap<>();
+            param.put("classLoader", classLoader);
+            param.put("group", groupId);
+            param.put("module", artifactId);
+            param.put("version", version);
+
+            LOG.debug("Downloading {}:{}:{}", groupId, artifactId, version);
+            Grape.grab(param);
+
+            // the classloader can load content from the downloaded JAR
+            boolean found = false;
+            found |= scanCamelComponents(camelCatalog, classLoader);
+
+            return found;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    protected boolean scanCamelComponents(CamelCatalog camelCatalog, ClassLoader classLoader)
{
+        boolean found = false;
+        // is there any custom Camel components in this library?
+        Properties properties = loadComponentProperties(classLoader);
+        if (properties != null) {
+            String components = (String) properties.get("components");
+            if (components != null) {
+                String[] part = components.split("\\s");
+                for (String scheme : part) {
+                    if (!camelCatalog.findComponentNames().contains(scheme)) {
+                        // find the class name
+                        String javaType = extractComponentJavaType(classLoader, scheme);
+                        if (javaType != null) {
+                            String json = loadComponentJSonSchema(classLoader, scheme);
+                            if (json != null) {
+                                LOG.debug("Adding component: {}", scheme);
+                                camelCatalog.addComponent(scheme, javaType, json);
+                                found = true;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return found;
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/eec3f843/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/MavenArtifactProvider.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/MavenArtifactProvider.java
b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/MavenArtifactProvider.java
new file mode 100644
index 0000000..a96c2a8
--- /dev/null
+++ b/platforms/camel-catalog-maven/src/main/java/org/apache/camel/catalog/maven/MavenArtifactProvider.java
@@ -0,0 +1,45 @@
+/**
+ * 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.catalog.maven;
+
+import org.apache.camel.catalog.CamelCatalog;
+
+/**
+ * Provider which allows downloading artifact using Maven and add content to the {@link CamelCatalog}.
+ */
+public interface MavenArtifactProvider {
+
+    /**
+     * To add a 3rd party Maven repository.
+     *
+     * @param name the repository name
+     * @param url  the repository url
+     */
+    void addMavenRepository(String name, String url);
+
+    /**
+     * Downloads the artifact using the Maven coordinates and scans the JAR for Camel components
+     * which will be added to the CamelCatalog.
+     *
+     * @param camelCatalog The Camel Catalog
+     * @param groupId      Maven group id
+     * @param artifactId   Maven artifact id
+     * @param version      Maven version
+     * @return <tt>true</tt> if anything was added to the catalog, <tt>false</tt>
if not.
+     */
+    boolean addArtifactToCatalog(CamelCatalog camelCatalog, String groupId, String artifactId,
String version);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/eec3f843/platforms/camel-catalog-maven/src/test/java/org/apache/camel/catalog/maven/MavenArtifactProviderTest.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog-maven/src/test/java/org/apache/camel/catalog/maven/MavenArtifactProviderTest.java
b/platforms/camel-catalog-maven/src/test/java/org/apache/camel/catalog/maven/MavenArtifactProviderTest.java
new file mode 100644
index 0000000..f1e42f0
--- /dev/null
+++ b/platforms/camel-catalog-maven/src/test/java/org/apache/camel/catalog/maven/MavenArtifactProviderTest.java
@@ -0,0 +1,40 @@
+/**
+ * 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.catalog.maven;
+
+import junit.framework.TestCase;
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.junit.Test;
+
+public class MavenArtifactProviderTest extends TestCase {
+
+    @Test
+    public void testAddComponent() {
+        CamelCatalog camelCatalog = new DefaultCamelCatalog();
+        MavenArtifactProvider provider = new DefaultMavenArtifactProvider();
+
+        int before = camelCatalog.findComponentNames().size();
+
+        boolean found = provider.addArtifactToCatalog(camelCatalog, "org.apache.camel", "dummy-component",
camelCatalog.getCatalogVersion());
+        assertTrue(found);
+
+        int after = camelCatalog.findComponentNames().size();
+
+        assertTrue("Should find 1 new component", after - before == 1);
+    }
+}


Mime
View raw message