Skip to content

Commit 0500a97

Browse files
authored
Merge pull request #65 from wanjunlei/java
support multiple functions in one pod
2 parents de5f6e5 + 717625a commit 0500a97

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

builders/java/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Use the follow command to build a java builder.
44

55
```shell
6-
hack/build.sh --with-stack --java-version < java_version > --docker-registry < registry >
6+
hack/build.sh java --with-stack --java-version < java_version > --docker-registry < registry >
77
```
88

99
This command will create three image:

cmd/java/functions_framework/main.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,9 @@ func buildFn(ctx *gcp.Context) error {
6565

6666
ctx.SetFunctionsEnvVars(layer)
6767

68-
// Use javap to check that the class is indeed in the classpath we just determined.
69-
// On success, it will output a description of the class and its public members, which we discard.
70-
// On failure it will output an error saying what's wrong (usually that the class doesn't exist).
71-
// Success here doesn't guarantee that the function will execute. It might not implement one of the
72-
// required interfaces, for example. But it eliminates the commonest problem of specifying the wrong target.
73-
// We use an ExecUser* method so that the time taken by the javap command is counted as user time.
74-
target := os.Getenv(env.FunctionTarget)
75-
if _, err := ctx.ExecWithErr([]string{"javap", "-classpath", classpath, target}, gcp.WithUserAttribution); err != nil {
76-
// The javap error output will typically be "Error: class not found: foo.Bar".
77-
return gcp.UserErrorf("build succeeded but did not produce the class %q specified as the function target: %s", target, (*err).Error())
68+
// Check that the classes are indeed in the classpath we just determined.
69+
if !checkTargets(ctx, classpath) {
70+
return gcp.UserErrorf("build succeeded but did not produce the target classes")
7871
}
7972

8073
launcherSource := filepath.Join(ctx.BuildpackRoot(), "launch.sh")
@@ -85,6 +78,26 @@ func buildFn(ctx *gcp.Context) error {
8578
return nil
8679
}
8780

81+
// checkTargets use javap to check that the class is indeed in the classpath we just determined.
82+
// On success, it will output a description of the class and its public members, which we discard.
83+
// On failure it will output an error saying what's wrong (usually that the class doesn't exist).
84+
// Success here doesn't guarantee that the function will execute. It might not implement one of the
85+
// required interfaces, for example. But it eliminates the commonest problem of specifying the wrong target.
86+
// We use an ExecUser* method so that the time taken by the javap command is counted as user time.
87+
func checkTargets(ctx *gcp.Context, classpath string) bool {
88+
targets := strings.Split(os.Getenv(env.FunctionTarget), ",")
89+
success := true
90+
for _, target := range targets {
91+
cmd := []string{"javap", "-classpath", classpath, target}
92+
if _, err := ctx.ExecWithErr(cmd, gcp.WithUserAttribution); err != nil {
93+
// The javap error output will typically be "Error: class not found: foo.Bar".
94+
success = false
95+
}
96+
}
97+
98+
return success
99+
}
100+
88101
func createLauncher(ctx *gcp.Context, launcherSource, launcherTarget string) {
89102
launcherContents := ctx.ReadFile(launcherSource)
90103
ctx.WriteFile(launcherTarget, launcherContents, 0755)

0 commit comments

Comments
 (0)