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 2016/06/07 12:50:36 UTC

[8/8] camel git commit: Experiment with generating spring-boot auto configuration for the Camel components.

Experiment with generating spring-boot auto configuration for the Camel components.


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

Branch: refs/heads/boot-generate
Commit: 3ccd92dfbf4400e5d031ce1c7a0a5d0d578a271d
Parents: ee83515
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jun 7 14:26:36 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jun 7 14:49:54 2016 +0200

----------------------------------------------------------------------
 .../AhcComponentAutoConfiguration.java          |  47 -------
 .../springboot/AhcComponentConfiguration.java   | 116 -----------------
 .../main/resources/META-INF/spring.factories    |   2 -
 components/pom.xml                              |   2 +-
 .../SpringBootAutoConfigurationMojo.java        | 126 ++++++++++---------
 .../src/main/resources/license-header-java.txt  |   2 +-
 .../src/main/resources/license-header.txt       |   1 -
 7 files changed, 71 insertions(+), 225 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentAutoConfiguration.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentAutoConfiguration.java
deleted file mode 100644
index 560fa9b..0000000
--- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentAutoConfiguration.java
+++ /dev/null
@@ -1,47 +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.component.ahc.springboot;
-
-import org.springframework.context.annotation.Configuration;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.camel.component.ahc.AhcComponent;
-import org.apache.camel.CamelContext;
-import org.apache.camel.util.IntrospectionSupport;
-import org.springframework.context.annotation.Bean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-
-@Configuration
-@EnableConfigurationProperties(AhcComponentConfiguration.class)
-public class AhcComponentAutoConfiguration {
-
-	@Bean
-	@ConditionalOnClass(CamelContext.class)
-	@ConditionalOnMissingBean(AhcComponent.class)
-	public AhcComponent configureComponent(CamelContext camelContext,
-			AhcComponentConfiguration configuration) throws Exception {
-		AhcComponent component = new AhcComponent();
-		component.setCamelContext(camelContext);
-		Map<String, Object> parameters = new HashMap<>();
-		IntrospectionSupport.getProperties(configuration, parameters, null);
-		IntrospectionSupport.setProperties(camelContext,
-				camelContext.getTypeConverter(), component, parameters);
-		return component;
-	}
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentConfiguration.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentConfiguration.java
deleted file mode 100644
index 1743b48..0000000
--- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/springboot/AhcComponentConfiguration.java
+++ /dev/null
@@ -1,116 +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.component.ahc.springboot;
-
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import com.ning.http.client.AsyncHttpClient;
-import org.apache.camel.component.ahc.AhcBinding;
-import com.ning.http.client.AsyncHttpClientConfig;
-import org.apache.camel.util.jsse.SSLContextParameters;
-import org.apache.camel.spi.HeaderFilterStrategy;
-
-/**
- * To call external HTTP services using Async Http Client.
- */
-@ConfigurationProperties(prefix = "camel.component.ahc")
-public class AhcComponentConfiguration {
-
-	/**
-	 * To use a custom AsyncHttpClient
-	 */
-	private AsyncHttpClient client;
-	/**
-	 * To use a custom AhcBinding which allows to control how to bind between
-	 * AHC and Camel.
-	 */
-	private AhcBinding binding;
-	/**
-	 * To configure the AsyncHttpClient to use a custom
-	 * com.ning.http.client.AsyncHttpClientConfig instance.
-	 */
-	private AsyncHttpClientConfig clientConfig;
-	/**
-	 * Reference to a org.apache.camel.util.jsse.SSLContextParameters in the
-	 * Registry. Note that configuring this option will override any SSL/TLS
-	 * configuration options provided through the clientConfig option at the
-	 * endpoint or component level.
-	 */
-	private SSLContextParameters sslContextParameters;
-	/**
-	 * Whether to allow java serialization when a request uses
-	 * context-type=application/x-java-serialized-object This is by default
-	 * turned off. If you enable this then be aware that Java will deserialize
-	 * the incoming data from the request to Java and that can be a potential
-	 * security risk.
-	 */
-	private boolean allowJavaSerializedObject;
-	/**
-	 * To use a custom HeaderFilterStrategy to filter header to and from Camel
-	 * message.
-	 */
-	private HeaderFilterStrategy headerFilterStrategy;
-
-	public AsyncHttpClient getClient() {
-		return client;
-	}
-
-	public void setClient(AsyncHttpClient client) {
-		this.client = client;
-	}
-
-	public AhcBinding getBinding() {
-		return binding;
-	}
-
-	public void setBinding(AhcBinding binding) {
-		this.binding = binding;
-	}
-
-	public AsyncHttpClientConfig getClientConfig() {
-		return clientConfig;
-	}
-
-	public void setClientConfig(AsyncHttpClientConfig clientConfig) {
-		this.clientConfig = clientConfig;
-	}
-
-	public SSLContextParameters getSslContextParameters() {
-		return sslContextParameters;
-	}
-
-	public void setSslContextParameters(
-			SSLContextParameters sslContextParameters) {
-		this.sslContextParameters = sslContextParameters;
-	}
-
-	public boolean isAllowJavaSerializedObject() {
-		return allowJavaSerializedObject;
-	}
-
-	public void setAllowJavaSerializedObject(boolean allowJavaSerializedObject) {
-		this.allowJavaSerializedObject = allowJavaSerializedObject;
-	}
-
-	public HeaderFilterStrategy getHeaderFilterStrategy() {
-		return headerFilterStrategy;
-	}
-
-	public void setHeaderFilterStrategy(
-			HeaderFilterStrategy headerFilterStrategy) {
-		this.headerFilterStrategy = headerFilterStrategy;
-	}
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/components/camel-ahc/src/main/resources/META-INF/spring.factories
----------------------------------------------------------------------
diff --git a/components/camel-ahc/src/main/resources/META-INF/spring.factories b/components/camel-ahc/src/main/resources/META-INF/spring.factories
deleted file mode 100644
index 978cb12..0000000
--- a/components/camel-ahc/src/main/resources/META-INF/spring.factories
+++ /dev/null
@@ -1,2 +0,0 @@
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-org.apache.camel.component.ahc.springboot.AhcComponentAutoConfiguration

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/components/pom.xml
----------------------------------------------------------------------
diff --git a/components/pom.xml b/components/pom.xml
index 892c564..09c56aa 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -346,7 +346,7 @@
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <version>${spring-boot-version}</version>
-          <optional>provided</optional>
+          <scope>provided</scope>
         </dependency>
       </dependencies>
     </profile>

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java
index 4f9ead2..3506c93 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java
@@ -126,42 +126,6 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         }
     }
 
-    private void createSpringFactorySource(String packageName, ComponentModel model) throws MojoFailureException {
-        StringBuilder sb = new StringBuilder();
-        sb.append("org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\n");
-
-        int pos = model.getJavaType().lastIndexOf(".");
-        String name = model.getJavaType().substring(pos + 1);
-        name = name.replace("Component", "ComponentAutoConfiguration");
-        sb.append(packageName).append(".").append(name).append("\n");
-
-        String code = sb.toString();
-
-        String fileName = "META-INF/spring.factories";
-        File target = new File(resourcesDir, fileName);
-
-        try {
-            if (target.exists()) {
-                String existing = FileUtils.readFileToString(target);
-                if (!code.equals(existing)) {
-                    // update
-                    FileUtils.write(target, code);
-                    getLog().info("Updated existing file: " + target);
-                } else {
-                    getLog().info("No changes to existing file: " + target);
-                }
-            } else {
-                InputStream is = getClass().getClassLoader().getResourceAsStream("license-header.txt");
-                String header = loadText(is);
-                FileUtils.write(target, header);
-                FileUtils.write(target, code, true);
-                getLog().info("Created file: " + target);
-            }
-        } catch (Exception e) {
-            throw new MojoFailureException("IOError with file " + target, e);
-        }
-    }
-
     private void createComponentConfigurationSource(String packageName, ComponentModel model) throws MojoFailureException {
         final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
 
@@ -170,9 +134,11 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         name = name.replace("Component", "ComponentConfiguration");
         javaClass.setPackage(packageName).setName(name);
 
+        String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
         if (!Strings.isBlank(model.getDescription())) {
-            javaClass.getJavaDoc().setFullText(model.getDescription());
+            doc = model.getDescription() + "\n\n" + doc;
         }
+        javaClass.getJavaDoc().setFullText(doc);
 
         String prefix = "camel.component." + model.getScheme();
         javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
@@ -187,27 +153,26 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
             }
         }
 
-        String code = javaClass.toString();
-        getLog().debug("Source code generated:\n" + code);
-
         String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
         File target = new File(srcDir, fileName);
 
         try {
+            InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
+            String header = loadText(is);
+            String code = sourceToString(javaClass);
+            code = header + code;
+            getLog().debug("Source code generated:\n" + code);
+
             if (target.exists()) {
                 String existing = FileUtils.readFileToString(target);
                 if (!code.equals(existing)) {
-                    // update
-                    FileUtils.write(target, code);
+                    FileUtils.write(target, code, false);
                     getLog().info("Updated existing file: " + target);
                 } else {
                     getLog().info("No changes to existing file: " + target);
                 }
             } else {
-                InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
-                String header = loadText(is);
-                FileUtils.write(target, header);
-                FileUtils.write(target, code, true);
+                FileUtils.write(target, code);
                 getLog().info("Created file: " + target);
             }
         } catch (Exception e) {
@@ -223,6 +188,9 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         name = name.replace("Component", "ComponentAutoConfiguration");
         javaClass.setPackage(packageName).setName(name);
 
+        String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
+        javaClass.getJavaDoc().setFullText(doc);
+
         javaClass.addAnnotation(Configuration.class);
 
         String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
@@ -253,27 +221,64 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
         method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
 
-        String code = javaClass.toString();
-        getLog().debug("Source code generated:\n" + code);
-
         String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
         File target = new File(srcDir, fileName);
 
         try {
+            InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
+            String header = loadText(is);
+            String code = sourceToString(javaClass);
+            code = header + code;
+            getLog().debug("Source code generated:\n" + code);
+
+            if (target.exists()) {
+                String existing = FileUtils.readFileToString(target);
+                if (!code.equals(existing)) {
+                    FileUtils.write(target, code, false);
+                    getLog().info("Updated existing file: " + target);
+                } else {
+                    getLog().info("No changes to existing file: " + target);
+                }
+            } else {
+                FileUtils.write(target, code);
+                getLog().info("Created file: " + target);
+            }
+        } catch (Exception e) {
+            throw new MojoFailureException("IOError with file " + target, e);
+        }
+    }
+
+    private void createSpringFactorySource(String packageName, ComponentModel model) throws MojoFailureException {
+        StringBuilder sb = new StringBuilder();
+        sb.append("org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\n");
+
+        int pos = model.getJavaType().lastIndexOf(".");
+        String name = model.getJavaType().substring(pos + 1);
+        name = name.replace("Component", "ComponentAutoConfiguration");
+        sb.append(packageName).append(".").append(name).append("\n");
+
+        String fileName = "META-INF/spring.factories";
+        File target = new File(resourcesDir, fileName);
+
+        try {
+            InputStream is = getClass().getClassLoader().getResourceAsStream("license-header.txt");
+            String header = loadText(is);
+            String code = sb.toString();
+            // add empty new line after header
+            code = header + "\n" + code;
+            getLog().debug("Source code generated:\n" + code);
+
             if (target.exists()) {
                 String existing = FileUtils.readFileToString(target);
                 if (!code.equals(existing)) {
                     // update
-                    FileUtils.write(target, code);
+                    FileUtils.write(target, code, false);
                     getLog().info("Updated existing file: " + target);
                 } else {
                     getLog().info("No changes to existing file: " + target);
                 }
             } else {
-                InputStream is = getClass().getClassLoader().getResourceAsStream("license-header-java.txt");
-                String header = loadText(is);
-                FileUtils.write(target, header);
-                FileUtils.write(target, code, true);
+                FileUtils.write(target, code);
                 getLog().info("Created file: " + target);
             }
         } catch (Exception e) {
@@ -281,7 +286,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         }
     }
 
-    private String createBody(String shortJavaType) {
+    private static String createBody(String shortJavaType) {
         StringBuilder sb = new StringBuilder();
         sb.append(shortJavaType).append(" component = new ").append(shortJavaType).append("();").append("\n");
         sb.append("component.setCamelContext(camelContext);\n");
@@ -295,7 +300,14 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         return sb.toString();
     }
 
-    private String loadComponentJson(Set<File> jsonFiles, String componentName) {
+    private static String sourceToString(JavaClassSource javaClass) {
+        String code = javaClass.toString();
+        // convert tabs to 4 spaces
+        code = code.replaceAll("\\t", "    ");
+        return code;
+    }
+
+    private static String loadComponentJson(Set<File> jsonFiles, String componentName) {
         try {
             for (File file : jsonFiles) {
                 if (file.getName().equals(componentName + ".json")) {
@@ -312,7 +324,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo {
         return null;
     }
 
-    private ComponentModel generateComponentModel(String componentName, String json) {
+    private static ComponentModel generateComponentModel(String componentName, String json) {
         List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
 
         ComponentModel component = new ComponentModel();

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header-java.txt
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header-java.txt b/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header-java.txt
index 366cda7..0f49ea9 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header-java.txt
+++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header-java.txt
@@ -13,4 +13,4 @@
  * 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.
- */
+ */
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3ccd92df/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header.txt
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header.txt b/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header.txt
index 384def4..12bdf0d 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header.txt
+++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/license-header.txt
@@ -14,4 +14,3 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-