MCPcopy Index your code
hub / github.com/cloudfoundry/java-buildpack / Finalize

Method Finalize

src/java/frameworks/java_opts.go:52–112  ·  view source on GitHub ↗

Finalize applies the JAVA_OPTS configuration

()

Source from the content-addressed store, hash-verified

50
51// Finalize applies the JAVA_OPTS configuration
52func (j *JavaOptsFramework) Finalize() error {
53 j.context.Log.BeginStep("Configuring Java Opts")
54
55 // Load configuration
56 config, err := j.loadConfig()
57 if err != nil {
58 j.context.Log.Warning("Failed to load java_opts config: %s", err.Error())
59 return nil // Don't fail the build
60 }
61
62 var configuredOpts []string
63
64 // Add configured java_opts from config file
65 if len(config.JavaOpts) > 0 {
66 j.context.Log.Info("Adding configured JAVA_OPTS: %v", config.JavaOpts)
67 configuredOpts = append(configuredOpts, config.JavaOpts...)
68 }
69
70 // Build the configured JAVA_OPTS value
71 // Escape each opt using Ruby buildpack's strategy: backslash-escape special characters
72 // This allows values with spaces to be preserved when passed through shell evaluation
73 var escapedOpts []string
74 for _, opt := range configuredOpts {
75 escapedOpts = append(escapedOpts, rubyStyleEscape(opt))
76 }
77 optsString := strings.Join(escapedOpts, " ")
78
79 // Write user-defined JAVA_OPTS to .opts file with priority 99 (Ruby buildpack line 82)
80 // This ensures user opts run LAST, allowing them to override framework defaults
81 //
82 // Handle from_environment setting (matching Ruby buildpack order):
83 // - If true: configured opts FIRST, then append $JAVA_OPTS (allows environment to override config)
84 // - If false: only use configured opts (ignore environment JAVA_OPTS)
85 //
86 // Ruby buildpack order (lines 39-44):
87 // configured.shellsplit.map {...}.each { |java_opt| @droplet.java_opts << java_opt }
88 // @droplet.java_opts << '$JAVA_OPTS' if from_environment?
89 var finalOpts string
90 if config.FromEnvironment {
91 // Add configured opts first, then environment JAVA_OPTS (Ruby order)
92 if optsString != "" {
93 finalOpts = fmt.Sprintf("%s $JAVA_OPTS", optsString)
94 } else {
95 // No configured opts, use only environment JAVA_OPTS
96 finalOpts = "$JAVA_OPTS"
97 }
98 } else {
99 // Ignore environment JAVA_OPTS, use only configured opts
100 finalOpts = optsString
101 }
102
103 // Write to .opts file (priority 99 = always last)
104 if finalOpts != "" {
105 if err := writeJavaOptsFile(j.context, 99, "user_java_opts", finalOpts); err != nil {
106 return fmt.Errorf("failed to write java_opts file: %w", err)
107 }
108 }
109

Callers

nothing calls this directly

Calls 3

loadConfigMethod · 0.95
rubyStyleEscapeFunction · 0.85
writeJavaOptsFileFunction · 0.85

Tested by

no test coverage detected