From 0133504dce41e251c1b0f6321883ffcee8687980 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 22 May 2020 13:09:49 +0200 Subject: [PATCH] Fix the classpath archive API call in GcfJarLauncher --- .../function/adapter/gcp/GcfJarLauncher.java | 16 +++++++++++++++- .../FunctionSampleGcpIntegrationTest.java | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/GcfJarLauncher.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/GcfJarLauncher.java index 6588fd890..fc7b6b5cb 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/GcfJarLauncher.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-gcp/src/main/java/org/springframework/cloud/function/adapter/gcp/GcfJarLauncher.java @@ -16,6 +16,8 @@ package org.springframework.cloud.function.adapter.gcp; +import java.util.List; + import com.google.cloud.functions.Context; import com.google.cloud.functions.HttpFunction; import com.google.cloud.functions.HttpRequest; @@ -23,7 +25,9 @@ import com.google.cloud.functions.HttpResponse; import com.google.cloud.functions.RawBackgroundFunction; import org.springframework.boot.loader.JarLauncher; +import org.springframework.boot.loader.archive.Archive; import org.springframework.boot.loader.jar.JarFile; +import org.springframework.util.CollectionUtils; /** * The launcher class written at the top-level of the output JAR to be deployed to @@ -31,6 +35,7 @@ import org.springframework.boot.loader.jar.JarFile; * * @author Ray Tsang * @author Daniel Zou + * @author Oleg Zhurakousky */ public class GcfJarLauncher extends JarLauncher implements HttpFunction, RawBackgroundFunction { @@ -41,12 +46,13 @@ public class GcfJarLauncher extends JarLauncher implements HttpFunction, RawBack public GcfJarLauncher() throws Exception { JarFile.registerUrlProtocolHandler(); - this.loader = createClassLoader(getClassPathArchivesIterator()); + this.loader = createClassLoader(discoverClassPathAcrhives()); Class clazz = this.loader .loadClass("org.springframework.cloud.function.adapter.gcp.FunctionInvoker"); this.delegate = clazz.getConstructor().newInstance(); } + @Override public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception { Thread.currentThread().setContextClassLoader(this.loader); @@ -58,5 +64,13 @@ public class GcfJarLauncher extends JarLauncher implements HttpFunction, RawBack Thread.currentThread().setContextClassLoader(this.loader); ((RawBackgroundFunction) delegate).accept(json, context); } + + private List discoverClassPathAcrhives() throws Exception { + List classPathArchives = getClassPathArchives(); + if (CollectionUtils.isEmpty(classPathArchives)) { + classPathArchives.add(this.getArchive()); + } + return classPathArchives; + } } diff --git a/spring-cloud-function-samples/function-sample-gcp-http/src/test/java/com/example/FunctionSampleGcpIntegrationTest.java b/spring-cloud-function-samples/function-sample-gcp-http/src/test/java/com/example/FunctionSampleGcpIntegrationTest.java index 86d47e9b9..0e7b1cbae 100644 --- a/spring-cloud-function-samples/function-sample-gcp-http/src/test/java/com/example/FunctionSampleGcpIntegrationTest.java +++ b/spring-cloud-function-samples/function-sample-gcp-http/src/test/java/com/example/FunctionSampleGcpIntegrationTest.java @@ -75,7 +75,7 @@ public class FunctionSampleGcpIntegrationTest { String line; while ((line = reader.readLine()) != null) { System.out.println(line); - if (line.equals("INFO: URL: http://localhost:8080/")) { + if (line.endsWith("URL: http://localhost:8080/")) { startedSuccessfully.countDown(); } }