From e27f1e9d97526b8c1706e0c3469c22a246e58a97 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 13 Aug 2019 21:07:34 +0200 Subject: [PATCH] GH-394 Ensured deployer supports old Function properties Resolves #394 --- .../FunctionDeployerConfiguration.java | 24 ++++++++++++++++++- .../function/deployer/FunctionProperties.java | 6 +++++ .../main/resources/META-INF/spring.factories | 3 ++- .../deployer/FunctionDeployerTests.java | 14 +++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionDeployerConfiguration.java b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionDeployerConfiguration.java index bc4db6425..2d4d54362 100644 --- a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionDeployerConfiguration.java +++ b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionDeployerConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2019-2019 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. @@ -23,16 +23,23 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.SpringApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.loader.archive.Archive; import org.springframework.boot.loader.archive.JarFileArchive; import org.springframework.cloud.function.context.FunctionRegistry; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; /** * + * Configuration class which creates an instance of {@link SmartLifecycle} + * which deploys and un-deploys packages archives via it's {@link SmartLifecycle#start()} + * and {@link SmartLifecycle#stop()} operations. + *
* @author Oleg Zhurakousky * * @since 3.0 @@ -97,4 +104,19 @@ public class FunctionDeployerConfiguration { }; } + /** + * Instance of {@link EnvironmentPostProcessor} which ensures that legacy + * Function property names are still honored. + */ + static class LegacyPropertyEnvironmentPostProcessor implements EnvironmentPostProcessor { + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + if (environment.containsProperty("function.name")) { + System.setProperty(FunctionProperties.PREFIX + ".function-name", environment.getProperty("function.name")); + } + if (environment.containsProperty("function.location")) { + System.setProperty(FunctionProperties.PREFIX + ".location", environment.getProperty("function.location")); + } + } + } } diff --git a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionProperties.java b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionProperties.java index c6c612dba..74773abbd 100644 --- a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionProperties.java +++ b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/FunctionProperties.java @@ -26,10 +26,16 @@ import org.springframework.util.StringUtils; * Configuration properties for deciding how to locate the functional class to execute. * * @author Eric Bottard + * @author Oleg Zhurakousky */ @ConfigurationProperties("spring.cloud.function") public class FunctionProperties { + /** + * The name prefix for properties defined by this properties class. + */ + public final static String PREFIX = "spring.cloud.function"; + /** * Location of jar archive containing the supplier/function/consumer class or bean to run. */ diff --git a/spring-cloud-function-deployer/src/main/resources/META-INF/spring.factories b/spring-cloud-function-deployer/src/main/resources/META-INF/spring.factories index 84cb28fe3..d69fbcc61 100644 --- a/spring-cloud-function-deployer/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-function-deployer/src/main/resources/META-INF/spring.factories @@ -1 +1,2 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.function.deployer.FunctionDeployerConfiguration \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.function.deployer.FunctionDeployerConfiguration +org.springframework.boot.env.EnvironmentPostProcessor=org.springframework.cloud.function.deployer.FunctionDeployerConfiguration$LegacyPropertyEnvironmentPostProcessor \ No newline at end of file diff --git a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java index cf7f933b1..2c6c81abd 100644 --- a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java +++ b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java @@ -154,6 +154,20 @@ public class FunctionDeployerTests { assertThat(new String(result.getPayload(), StandardCharsets.UTF_8)).isEqualTo("\"BOB\""); } + @Test + public void testWithLegacyProperties() throws Exception { + String[] args = new String[] { + "--function.location=target/it/bootapp/target/bootapp-1.0.0.RELEASE-exec.jar", + "--function.name=uppercase" }; + ApplicationContext context = SpringApplication.run(DeployerApplication.class, args); + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + Function, Message> function = catalog.lookup("uppercase", "application/json"); + + Message result = function + .apply(MessageBuilder.withPayload("\"bob\"".getBytes(StandardCharsets.UTF_8)).build()); + assertThat(new String(result.getPayload(), StandardCharsets.UTF_8)).isEqualTo("\"BOB\""); + } + /* * Same as above but: * Given that Java 11 does not include 'javax' packages, this test simply validates that