From 76633720624de1e1142835b04a5eba5a8a634512 Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Wed, 12 Aug 2015 17:05:58 -0700 Subject: [PATCH] ModuleRunner enhancements - Move the launcher property `modules` to `ModuleLauncherProperties` - This will make the `ModuleRunner` to be `Component` bean only; Also, it only needs to enable the configuration properties of `ModuleLauncherProperties` Separate module resolver/launcher properties Add validator dependency to module launcher --- spring-cloud-stream-module-launcher/pom.xml | 4 ++ .../module/launcher/ModuleLauncher.java | 2 +- .../launcher/ModuleLauncherConfiguration.java | 5 +- .../launcher/ModuleLauncherProperties.java | 31 +++------- .../module/launcher/ModuleLauncherRunner.java | 30 ++++------ .../resolver/ModuleResolverProperties.java | 57 +++++++++++++++++++ 6 files changed, 86 insertions(+), 43 deletions(-) create mode 100644 spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/resolver/ModuleResolverProperties.java diff --git a/spring-cloud-stream-module-launcher/pom.xml b/spring-cloud-stream-module-launcher/pom.xml index a0b36443e..deb4b7ab1 100644 --- a/spring-cloud-stream-module-launcher/pom.xml +++ b/spring-cloud-stream-module-launcher/pom.xml @@ -29,6 +29,10 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-configuration-processor diff --git a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncher.java b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncher.java index e4a6a7152..a705364d0 100644 --- a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncher.java +++ b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncher.java @@ -63,7 +63,7 @@ public class ModuleLauncher { * <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version> * * To pass arguments to a module, prefix with the module name and a dot. The arg name will be de-qualified and passed along. - * For example: ---Dorg.springframework.cloud.stream.module:time-source:1.0.0.BUILD-SNAPSHOT.bar=123 becomes --bar=123 and is only passed to the 'org.springframework.cloud.stream.module:time-source:1.0.0.BUILD-SNAPSHOT' module. + * For example: --org.springframework.cloud.stream.module:time-source:1.0.0.BUILD-SNAPSHOT.bar=123 becomes --bar=123 and is only passed to the 'org.springframework.cloud.stream.module:time-source:1.0.0.BUILD-SNAPSHOT' module. * * @param modules a list of modules * @param args a list of arguments, prefixed with the module name diff --git a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherConfiguration.java b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherConfiguration.java index 4672bb391..9449cde18 100644 --- a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherConfiguration.java +++ b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherConfiguration.java @@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.stream.module.resolver.AetherModuleResolver; import org.springframework.cloud.stream.module.resolver.ModuleResolver; +import org.springframework.cloud.stream.module.resolver.ModuleResolverProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,11 +35,11 @@ import org.springframework.context.annotation.Configuration; * @author Ilayaperumal Gopinathan */ @Configuration -@EnableConfigurationProperties(ModuleLauncherProperties.class) +@EnableConfigurationProperties(ModuleResolverProperties.class) public class ModuleLauncherConfiguration { @Autowired - private ModuleLauncherProperties properties; + private ModuleResolverProperties properties; /** * Sets up the default Aether-based module resolver, unless overridden. diff --git a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherProperties.java b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherProperties.java index c81962568..7b80de024 100644 --- a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherProperties.java +++ b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherProperties.java @@ -16,12 +16,12 @@ package org.springframework.cloud.stream.module.launcher; -import java.io.File; +import org.hibernate.validator.constraints.NotEmpty; import org.springframework.boot.context.properties.ConfigurationProperties; /** - * Contains configuration properties for the module launcher. + * Configuration properties for {@link ModuleLauncher}. * * @author Ilayaperumal Gopinathan * @author Marius Bogoevici @@ -30,30 +30,17 @@ import org.springframework.boot.context.properties.ConfigurationProperties; public class ModuleLauncherProperties { /** - * File path to a locally available maven repository, where modules will be downloaded. + * Array of modules that need to be launched. */ - private File localRepository = new File(System.getProperty("user.home") - + File.separator + ".m2" + File.separator + "repository"); + private String[] modules; - /** - * Location of comma separated remote maven repositories from which modules will be downloaded, if not available locally. - */ - private String[] remoteRepositories = new String[] {"https://repo.spring.io/libs-snapshot"}; - - public void setRemoteRepositories(String[] remoteRepositories) { - this.remoteRepositories = remoteRepositories; + public void setModules(String[] modules) { + this.modules = modules; } - protected String[] getRemoteRepositories() { - return remoteRepositories; - } - - public void setLocalRepository(File localRepository) { - this.localRepository = localRepository; - } - - protected File getLocalRepository() { - return localRepository; + @NotEmpty(message = "A list of modules must be specified.") + public String[] getModules() { + return modules; } } diff --git a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherRunner.java b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherRunner.java index 33207fb63..09f2ce288 100644 --- a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherRunner.java +++ b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/launcher/ModuleLauncherRunner.java @@ -18,44 +18,38 @@ package org.springframework.cloud.stream.module.launcher; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.InitializingBean; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; -import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component; -import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** + * Spring boot {@link ApplicationRunner} that triggers {@link ModuleLauncher} to launch the modules. + * * @author Marius Bogoevici + * @author Ilayaperumal Gopinathan */ @Component -@ConfigurationProperties -public class ModuleLauncherRunner implements ApplicationRunner, InitializingBean { +@EnableConfigurationProperties(ModuleLauncherProperties.class) +public class ModuleLauncherRunner implements ApplicationRunner { private final static Log log = LogFactory.getLog(ModuleLauncherRunner.class); + @Autowired + private ModuleLauncherProperties moduleLauncherProperties; + @Autowired private ModuleLauncher moduleLauncher; - private String[] modules; - - public void setModules(String[] modules) { - this.modules = modules; - } - - @Override - public void afterPropertiesSet() throws Exception { - Assert.notEmpty(this.modules, "A list of modules must be specified"); - } - @Override public void run(ApplicationArguments applicationArguments) throws Exception { - String[] launchedModules = this.modules; + String[] launchedModules = moduleLauncherProperties.getModules(); if (log.isInfoEnabled()) { log.info("Launching: " - + StringUtils.arrayToCommaDelimitedString(this.modules) + + StringUtils.arrayToCommaDelimitedString(launchedModules) + " with arguments: " + StringUtils.arrayToCommaDelimitedString(applicationArguments .getSourceArgs())); diff --git a/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/resolver/ModuleResolverProperties.java b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/resolver/ModuleResolverProperties.java new file mode 100644 index 000000000..434c99516 --- /dev/null +++ b/spring-cloud-stream-module-launcher/src/main/java/org/springframework/cloud/stream/module/resolver/ModuleResolverProperties.java @@ -0,0 +1,57 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed 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.springframework.cloud.stream.module.resolver; + +import java.io.File; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for {@link ModuleResolver}. + * + * @author Ilayaperumal Gopinathan + */ +@ConfigurationProperties +public class ModuleResolverProperties { + + /** + * File path to a locally available maven repository, where modules will be downloaded. + */ + private File localRepository = new File(System.getProperty("user.home") + File.separator + ".m2" + + File.separator + "repository"); + + /** + * Location of comma separated remote maven repositories from which modules will be downloaded, if not available locally. + */ + private String[] remoteRepositories = new String[] {"https://repo.spring.io/libs-snapshot"}; + + public void setRemoteRepositories(String[] remoteRepositories) { + this.remoteRepositories = remoteRepositories; + } + + public String[] getRemoteRepositories() { + return remoteRepositories; + } + public void setLocalRepository(File localRepository) { + this.localRepository = localRepository; + } + + public File getLocalRepository() { + return localRepository; + } + +}