Skip to content

Commit 15385e5

Browse files
committed
Import of Cloud Functions JVM from Git-on-Borg.
- 101c6e2f72af2496f6fb6fc6e67d32cfab3ffced Update the Converter for better ClassLoader separation. by Éamonn McManus <[email protected]> PiperOrigin-RevId: 289176564
1 parent cf383fb commit 15385e5

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

invoker-core/core/pom.xml

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,18 @@
130130

131131
<build>
132132
<plugins>
133-
<!-- Rename dependencies used by invoker-core so that they don't interfere with user dependencies -->
134133
<plugin>
135-
<artifactId>maven-shade-plugin</artifactId>
136-
<version>3.2.1</version>
137-
<executions>
138-
<execution>
139-
<phase>package</phase>
140-
<goals>
141-
<goal>shade</goal>
142-
</goals>
143-
<configuration>
144-
<relocations>
145-
<relocation>
146-
<pattern>com</pattern>
147-
<shadedPattern>shaded.com.google.cloud.functions</shadedPattern>
148-
<excludes>
149-
<exclude>com.google.cloud.functions.**</exclude>
150-
<exclude>com.google.gson.**</exclude>
151-
</excludes>
152-
</relocation>
153-
</relocations>
154-
<transformers>
155-
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
156-
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
157-
<mainClass>com.google.cloud.functions.invoker.runner.Invoker</mainClass>
158-
</transformer>
159-
</transformers>
160-
</configuration>
161-
</execution>
162-
</executions>
134+
<artifactId>maven-jar-plugin</artifactId>
135+
<version>3.1.2</version>
136+
<configuration>
137+
<archive>
138+
<manifest>
139+
<mainClass>com.google.cloud.functions.invoker.runner.Invoker</mainClass>
140+
<addClasspath>true</addClasspath>
141+
<classpathPrefix>lib</classpathPrefix>
142+
</manifest>
143+
</archive>
144+
</configuration>
163145
</plugin>
164146
</plugins>
165147
</build>

invoker-core/core/src/main/java/com/google/cloud/functions/invoker/runner/Invoker.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import java.lang.reflect.Method;
1515
import java.net.URL;
1616
import java.net.URLClassLoader;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.nio.file.Paths;
1720
import java.util.Arrays;
1821
import java.util.Objects;
1922
import java.util.Optional;
@@ -85,8 +88,13 @@ public static void main(String[] args) throws Exception {
8588
.filter(Objects::nonNull)
8689
.findFirst()
8790
.orElse("TestFunction.function");
91+
Path standardFunctionJarPath = Paths.get("function/function.jar");
8892
Optional<String> functionJarPath =
89-
Arrays.asList(line.getOptionValue("jar"), System.getenv("FUNCTION_JAR")).stream()
93+
Arrays.asList(
94+
line.getOptionValue("jar"),
95+
System.getenv("FUNCTION_JAR"),
96+
Files.exists(standardFunctionJarPath) ? standardFunctionJarPath.toString() : null)
97+
.stream()
9098
.filter(Objects::nonNull)
9199
.findFirst();
92100
Invoker invoker =
@@ -221,7 +229,8 @@ private static class OnlyApiClassLoader extends ClassLoader {
221229
@Override
222230
protected Class<?> findClass(String name) throws ClassNotFoundException {
223231
String prefix = "com.google.cloud.functions.";
224-
if (name.startsWith(prefix) && Character.isUpperCase(name.charAt(prefix.length()))) {
232+
if ((name.startsWith(prefix) && Character.isUpperCase(name.charAt(prefix.length())))
233+
|| name.startsWith("javax.servlet.")) {
225234
return runtimeClassLoader.loadClass(name);
226235
}
227236
return super.findClass(name); // should throw ClassNotFoundException

invoker-core/core/src/test/java/com/google/cloud/functions/invoker/IntegrationTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,10 @@ private ServerProcess startServer(
350350
Future<?> outputMonitorResult = EXECUTOR.submit(
351351
() -> monitorOutput(serverProcess.getInputStream(), ready));
352352
boolean serverReady = ready.await(5, TimeUnit.SECONDS);
353-
assertWithMessage("Waiting for server to be ready").that(serverReady).isTrue();
353+
if (!serverReady) {
354+
serverProcess.destroy();
355+
throw new AssertionError("Server never became ready");
356+
}
354357
return ServerProcess.of(serverProcess, outputMonitorResult);
355358
}
356359

invoker-core/functionjar/pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<archive>
4949
<manifest>
5050
<addClasspath>true</addClasspath>
51-
<classpathPrefix>dependency</classpathPrefix>
51+
<classpathPrefix>lib</classpathPrefix>
5252
</manifest>
5353
</archive>
5454
</configuration>
@@ -76,6 +76,9 @@
7676
<goals>
7777
<goal>copy-dependencies</goal>
7878
</goals>
79+
<configuration>
80+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
81+
</configuration>
7982
</execution>
8083
</executions>
8184
</plugin>

0 commit comments

Comments
 (0)