From 745851abf2fef532e91dbaacdfa923a71699eca0 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 12 Aug 2019 16:22:21 +0200 Subject: [PATCH] Added support for non-uber jars in new deployer Added integration test for non-uber JARs --- .../src/it/simplestjar/pom.xml | 32 +++++++++++++++++++ .../function/example/UpperCaseFunction.java | 13 ++++++++ .../deployer/FunctionArchiveDeployer.java | 11 ++++++- .../deployer/FunctionDeployerTests.java | 20 ++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-function-deployer-new/src/it/simplestjar/pom.xml create mode 100644 spring-cloud-function-deployer-new/src/it/simplestjar/src/main/java/function/example/UpperCaseFunction.java diff --git a/spring-cloud-function-deployer-new/src/it/simplestjar/pom.xml b/spring-cloud-function-deployer-new/src/it/simplestjar/pom.xml new file mode 100644 index 000000000..fa22805c2 --- /dev/null +++ b/spring-cloud-function-deployer-new/src/it/simplestjar/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + function.example + simplestjar + 0.0.1.BUILD-SNAPSHOT + jar + + simplestjar + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + diff --git a/spring-cloud-function-deployer-new/src/it/simplestjar/src/main/java/function/example/UpperCaseFunction.java b/spring-cloud-function-deployer-new/src/it/simplestjar/src/main/java/function/example/UpperCaseFunction.java new file mode 100644 index 000000000..859a54a58 --- /dev/null +++ b/spring-cloud-function-deployer-new/src/it/simplestjar/src/main/java/function/example/UpperCaseFunction.java @@ -0,0 +1,13 @@ +package function.example; + +import java.util.function.Function; + +public class UpperCaseFunction implements Function { + + @Override + public String apply(String value) { + System.out.println("Uppercasing " + value); + return value.toUpperCase(); + } + +} diff --git a/spring-cloud-function-deployer-new/src/main/java/org/springframework/cloud/function/deployer/FunctionArchiveDeployer.java b/spring-cloud-function-deployer-new/src/main/java/org/springframework/cloud/function/deployer/FunctionArchiveDeployer.java index 0fc5f21c1..635a48bc2 100644 --- a/spring-cloud-function-deployer-new/src/main/java/org/springframework/cloud/function/deployer/FunctionArchiveDeployer.java +++ b/spring-cloud-function-deployer-new/src/main/java/org/springframework/cloud/function/deployer/FunctionArchiveDeployer.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; import java.net.URL; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; @@ -70,7 +71,7 @@ class FunctionArchiveDeployer extends JarLauncher { ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); try { - Thread.currentThread().setContextClassLoader(createClassLoader(getClassPathArchives())); + Thread.currentThread().setContextClassLoader(createClassLoader(discoverClassPathAcrhives())); evalContext.setTypeLocator(new StandardTypeLocator(Thread.currentThread().getContextClassLoader())); if (this.isBootApplicationWithMain()) { @@ -193,6 +194,14 @@ class FunctionArchiveDeployer extends JarLauncher { } } + private List discoverClassPathAcrhives() throws Exception { + List classPathArchives = getClassPathArchives(); + if (CollectionUtils.isEmpty(classPathArchives)) { + classPathArchives.add(this.getArchive()); + } + return classPathArchives; + } + private FunctionRegistration discovereAndLoadFunctionFromClassName(String functionClassName) throws Exception { FunctionRegistration functionRegistration = null; AtomicReference typeRef = new AtomicReference<>(); diff --git a/spring-cloud-function-deployer-new/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java b/spring-cloud-function-deployer-new/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java index 7a80a34e3..a012799e7 100644 --- a/spring-cloud-function-deployer-new/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java +++ b/spring-cloud-function-deployer-new/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java @@ -66,6 +66,26 @@ public class FunctionDeployerTests { assertThat(results.get(1)).isEqualTo("STACY"); } + @Test + public void testWithSimplestJar() throws Exception { + String[] args = new String[] { + "--spring.cloud.function.location=target/it/simplestjar/target/simplestjar-0.0.1.BUILD-SNAPSHOT.jar", + "--spring.cloud.function.function-class=function.example.UpperCaseFunction" }; + + ApplicationContext context = SpringApplication.run(DeployerApplication.class, args); + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + Function function = catalog.lookup("upperCaseFunction"); + + assertThat(function.apply("bob")).isEqualTo("BOB"); + assertThat(function.apply("stacy")).isEqualTo("STACY"); + + Function, Flux> functionAsFlux = catalog.lookup("upperCaseFunction"); + + List results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block(); + assertThat(results.get(0)).isEqualTo("BOB"); + assertThat(results.get(1)).isEqualTo("STACY"); + } + /* * Target function `class UpperCaseFunction implements Function` * No Main/Start class present, no Spring configuration