-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathtomcat.go
More file actions
652 lines (537 loc) · 24.1 KB
/
tomcat.go
File metadata and controls
652 lines (537 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package containers
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"github.com/cloudfoundry/java-buildpack/src/java/resources"
"github.com/cloudfoundry/libbuildpack"
)
// TomcatContainer handles servlet/WAR applications
type TomcatContainer struct {
context *common.Context
config *tomcatConfig
}
// NewTomcatContainer creates a new Tomcat container
func NewTomcatContainer(ctx *common.Context) *TomcatContainer {
return &TomcatContainer{
context: ctx,
}
}
// Detect checks if this is a Tomcat/servlet application
func (t *TomcatContainer) Detect() (string, error) {
buildDir := t.context.Stager.BuildDir()
// Check for WEB-INF directory (exploded WAR)
webInf := filepath.Join(buildDir, "WEB-INF")
if _, err := os.Stat(webInf); err == nil {
t.context.Log.Debug("Detected WAR application via WEB-INF directory")
return "Tomcat", nil
}
// Check for WAR files
matches, err := filepath.Glob(filepath.Join(buildDir, "*.war"))
if err == nil && len(matches) > 0 {
t.context.Log.Debug("Detected WAR file: %s", matches[0])
return "Tomcat", nil
}
return "", nil
}
// Supply installs Tomcat and dependencies
func (t *TomcatContainer) Supply() error {
t.context.Log.BeginStep("Supplying Tomcat")
// Determine Java version to select appropriate Tomcat version
// Tomcat 10.x requires Java 11+, Tomcat 9.x supports Java 8-22
javaHome := os.Getenv("JAVA_HOME")
var dep libbuildpack.Dependency
var err error
t.config, err = t.loadConfig()
if err != nil {
return fmt.Errorf("failed to load tomcat config: %w", err)
}
if javaHome != "" {
javaMajorVersion, versionErr := common.DetermineJavaVersion(javaHome)
if versionErr == nil {
tomcatVersion := DetermineTomcatVersion(t.config.Tomcat.Version)
t.context.Log.Debug("Detected Java major version: %d", javaMajorVersion)
// Select Tomcat version pattern based on Java version
var versionPattern string
if tomcatVersion == "" {
t.context.Log.Info("Tomcat version not specified")
if javaMajorVersion >= 11 {
// Java 11+: Use Tomcat 10.x (Jakarta EE 9+)
versionPattern = "10.x"
t.context.Log.Info("Using Tomcat 10.x for Java %d", javaMajorVersion)
} else {
// Java 8-10: Use Tomcat 9.x (Java EE 8)
versionPattern = "9.x"
t.context.Log.Info("Using Tomcat 9.x for Java %d", javaMajorVersion)
}
} else {
versionPattern = tomcatVersion
t.context.Log.Info("Using Tomcat %s for Java %d", versionPattern, javaMajorVersion)
}
if strings.HasPrefix(versionPattern, "10.") && javaMajorVersion < 11 {
return fmt.Errorf("Tomcat 10.x requires Java 11+, but Java %d detected", javaMajorVersion)
}
// Resolve the version pattern to actual version using libbuildpack
allVersions := t.context.Manifest.AllDependencyVersions("tomcat")
resolvedVersion, err := libbuildpack.FindMatchingVersion(versionPattern, allVersions)
if err != nil {
return fmt.Errorf("tomcat version resolution error for pattern %q: %w", versionPattern, err)
}
dep.Name = "tomcat"
dep.Version = resolvedVersion
t.context.Log.Debug("Resolved Tomcat version pattern '%s' to %s", versionPattern, resolvedVersion)
} else {
t.context.Log.Warning("Unable to determine Java version: %s", versionErr.Error())
}
}
// Fallback to default version if we couldn't determine Java version
if dep.Version == "" {
dep, err = t.context.Manifest.DefaultVersion("tomcat")
if err != nil {
return fmt.Errorf("failed to determine Tomcat version: no JAVA_HOME set and no default version in manifest: %w", err)
}
}
// Install Tomcat with strip components to remove the top-level directory
// Apache Tomcat tarballs extract to apache-tomcat-X.Y.Z/ subdirectory
tomcatDir := t.tomcatDir()
if err := t.context.Installer.InstallDependencyWithStrip(dep, tomcatDir, 1); err != nil {
return fmt.Errorf("failed to install Tomcat: %w", err)
}
t.context.Log.Info("Installed Tomcat (%s)", dep.Version)
// Get buildpack index for multi-buildpack support
depsIdx := t.context.Stager.DepsIdx()
// Write profile.d script to set CATALINA_HOME, CATALINA_BASE, and JAVA_OPTS at runtime
tomcatPath := fmt.Sprintf("$DEPS_DIR/%s/tomcat", depsIdx)
// Determine access logging configuration (default: disabled, matching Ruby buildpack)
// Can be enabled via: JBP_CONFIG_TOMCAT='{access_logging_support: {access_logging: enabled}}'
accessLoggingEnabled := t.isAccessLoggingEnabled()
// Add http.port system property to JAVA_OPTS so Tomcat uses $PORT for the HTTP connector
// Add access.logging.enabled to control CloudFoundryAccessLoggingValve
// These are required for Cloud Foundry where the platform assigns a dynamic port
envContent := fmt.Sprintf(`export CATALINA_HOME=%s
export CATALINA_BASE=%s
export JAVA_OPTS="${JAVA_OPTS:+$JAVA_OPTS }-Dhttp.port=$PORT -Daccess.logging.enabled=%s"
`, tomcatPath, tomcatPath, accessLoggingEnabled)
if err := t.context.Stager.WriteProfileD("tomcat.sh", envContent); err != nil {
t.context.Log.Warning("Could not write tomcat.sh profile.d script: %s", err.Error())
} else {
t.context.Log.Debug("Created profile.d script: tomcat.sh")
}
// Install Tomcat support libraries (lifecycle, access-logging, and logging)
// These are ALWAYS required for proper Tomcat initialization with Cloud Foundry
if err := t.installTomcatLifecycleSupport(); err != nil {
return fmt.Errorf("failed to install Tomcat lifecycle support: %w", err)
}
if err := t.installTomcatAccessLoggingSupport(); err != nil {
return fmt.Errorf("failed to install Tomcat access logging support: %w", err)
}
loggingSupportJar, err := t.installTomcatLoggingSupport()
if err != nil {
return fmt.Errorf("failed to install Tomcat logging support: %w", err)
}
// Create setenv.sh in tomcat/bin to add logging support JAR to CLASSPATH
// Tomcat's catalina.sh automatically sources setenv.sh if it exists
// This ensures the logging JAR is on the classpath before Tomcat's logging initializes
if err := t.createSetenvScript(tomcatDir, loggingSupportJar); err != nil {
return fmt.Errorf("failed to create setenv.sh: %w", err)
}
// Install default Cloud Foundry-optimized Tomcat configuration (unless external config is used)
if err := t.installDefaultConfiguration(tomcatDir); err != nil {
return fmt.Errorf("failed to install default Tomcat configuration: %w", err)
}
// Install external Tomcat configuration if enabled (overrides defaults)
if err := t.installExternalConfiguration(tomcatDir); err != nil {
return fmt.Errorf("failed to install external Tomcat configuration: %w", err)
}
// JVMKill agent is installed and configured by JRE component
return nil
}
// installTomcatLifecycleSupport installs Tomcat lifecycle support library to tomcat/lib
func (t *TomcatContainer) installTomcatLifecycleSupport() error {
dep, err := t.context.Manifest.DefaultVersion("tomcat-lifecycle-support")
if err != nil {
return err
}
// InstallDependency for JAR files (non-archives) copies the file to the target directory
// The JAR will be placed in tomcat/lib/ as tomcat/lib/tomcat-lifecycle-support-X.Y.Z.RELEASE.jar
tomcatDir := filepath.Join(t.tomcatDir())
libDir := filepath.Join(tomcatDir, "lib")
// Ensure lib directory exists
if err := os.MkdirAll(libDir, 0755); err != nil {
return fmt.Errorf("failed to create tomcat lib directory: %w", err)
}
if err := t.context.Installer.InstallDependency(dep, libDir); err != nil {
return fmt.Errorf("failed to install Tomcat lifecycle support: %w", err)
}
t.context.Log.Info("Successfully installed Tomcat Lifecycle Support %s to tomcat/lib", dep.Version)
return nil
}
// installTomcatAccessLoggingSupport installs Tomcat access logging support library to tomcat/lib
func (t *TomcatContainer) installTomcatAccessLoggingSupport() error {
dep, err := t.context.Manifest.DefaultVersion("tomcat-access-logging-support")
if err != nil {
return err
}
// InstallDependency for JAR files (non-archives) copies the file to the target directory
// The JAR will be placed in tomcat/lib/ as tomcat/lib/tomcat-access-logging-support-X.Y.Z.RELEASE.jar
tomcatDir := filepath.Join(t.tomcatDir())
libDir := filepath.Join(tomcatDir, "lib")
// Ensure lib directory exists
if err := os.MkdirAll(libDir, 0755); err != nil {
return fmt.Errorf("failed to create tomcat lib directory: %w", err)
}
if err := t.context.Installer.InstallDependency(dep, libDir); err != nil {
return fmt.Errorf("failed to install Tomcat access logging support: %w", err)
}
t.context.Log.Info("Successfully installed Tomcat Access Logging Support %s to tomcat/lib", dep.Version)
return nil
}
// installTomcatLoggingSupport installs Tomcat logging support library to tomcat/bin
// This JAR must be on the classpath BEFORE Tomcat's logging initializes
// Returns the JAR filename so it can be added to CLASSPATH in profile.d script
func (t *TomcatContainer) installTomcatLoggingSupport() (string, error) {
dep, err := t.context.Manifest.DefaultVersion("tomcat-logging-support")
if err != nil {
return "", err
}
// InstallDependency for JAR files (non-archives) copies the file to the target directory
// The JAR will be placed in tomcat/bin/ as tomcat/bin/tomcat-logging-support-X.Y.Z.RELEASE.jar
tomcatDir := filepath.Join(t.tomcatDir())
binDir := filepath.Join(tomcatDir, "bin")
// Ensure bin directory exists
if err := os.MkdirAll(binDir, 0755); err != nil {
return "", fmt.Errorf("failed to create tomcat bin directory: %w", err)
}
if err := t.context.Installer.InstallDependency(dep, binDir); err != nil {
return "", fmt.Errorf("failed to install Tomcat logging support: %w", err)
}
entry, err := t.context.Manifest.GetEntry(dep)
if err != nil {
return "", fmt.Errorf("failed to get manifest entry for tomcat-logging-support: %w", err)
}
jarName := filepath.Base(entry.URI)
t.context.Log.Info("Successfully installed Tomcat Logging Support %s to tomcat/bin (contains CloudFoundryConsoleHandler)", dep.Version)
return jarName, nil
}
// createSetenvScript creates a setenv.sh script in tomcat/bin to add logging support JAR to CLASSPATH
// Tomcat's catalina.sh automatically sources setenv.sh if it exists
func (t *TomcatContainer) createSetenvScript(tomcatDir, loggingSupportJar string) error {
binDir := filepath.Join(tomcatDir, "bin")
setenvPath := filepath.Join(binDir, "setenv.sh")
jarPath := "$CATALINA_HOME/bin/" + loggingSupportJar
// Note that Tomcat builds its own CLASSPATH env before starting. It ensures that any user defined CLASSPATH variables
// are not used on startup, as can be seen in the catalina.sh script. That is why even we have something already
// sourced in CLASSPATH env from profile.d scripts it is disregarded on Tomcat startup and fresh CLASSPATH env is
// built here in the setenv.sh script.
setenvContent := fmt.Sprintf(`#!/bin/sh
CLASSPATH="%s${CONTAINER_SECURITY_PROVIDER:+:$CONTAINER_SECURITY_PROVIDER}"
`, jarPath)
if err := os.WriteFile(setenvPath, []byte(setenvContent), 0755); err != nil {
return fmt.Errorf("failed to write setenv.sh: %w", err)
}
t.context.Log.Info("Created setenv.sh with logging JAR on boot classpath")
return nil
}
// installExternalConfiguration installs external Tomcat configuration if enabled
func (t *TomcatContainer) installExternalConfiguration(tomcatDir string) error {
// Check if external configuration is enabled
externalConfigEnabled, repositoryRoot, version := t.isExternalConfigurationEnabled()
if !externalConfigEnabled {
t.context.Log.Debug("External Tomcat configuration is disabled, using defaults only")
return nil
}
t.context.Log.Info("External Tomcat configuration is enabled, will overlay on top of defaults")
if repositoryRoot == "" {
t.context.Log.Warning("External configuration enabled but repository_root not set")
t.context.Log.Warning("To use external Tomcat configuration, you must:")
t.context.Log.Warning(" 1. Fork this buildpack and add external config to manifest.yml")
t.context.Log.Warning(" 2. Or use a custom buildpack with external configuration included")
return nil
}
if version == "" {
version = "1.0.0" // default version
}
t.context.Log.Info("External configuration repository: %s (version: %s)", repositoryRoot, version)
// Try to install from manifest first if available
// This will work if the user has added the external configuration to their forked buildpack manifest
dep, err := t.context.Manifest.DefaultVersion("tomcat-external-configuration")
if err != nil {
// Manifest entry not found - download directly from repository_root
t.context.Log.Info("External configuration not in manifest, downloading directly from repository")
return t.downloadExternalConfiguration(repositoryRoot, version, tomcatDir)
}
t.context.Log.Info("Downloading external Tomcat configuration version %s from manifest", dep.Version)
// Install external configuration with strip=0 to overlay onto Tomcat directory
// The external config archive has structure: ./conf/...
// We extract directly to tomcatDir (no stripping needed)
if err := t.context.Installer.InstallDependencyWithStrip(dep, tomcatDir, 0); err != nil {
return fmt.Errorf("failed to install external configuration: %w", err)
}
t.context.Log.Info("Installed external Tomcat configuration version %s (overlaid on defaults)", dep.Version)
return nil
}
// downloadExternalConfiguration downloads external Tomcat configuration by first fetching
// index.yml to lookup the actual download URL for the specified version
func (t *TomcatContainer) downloadExternalConfiguration(repositoryRoot, version, tomcatDir string) error {
// Step 1: Download and parse index.yml from repository_root
indexURL := fmt.Sprintf("%s/index.yml", repositoryRoot)
t.context.Log.Info("Fetching external configuration index from: %s", indexURL)
indexResp, err := http.Get(indexURL)
if err != nil {
return fmt.Errorf("failed to download index.yml: %w", err)
}
defer indexResp.Body.Close()
if indexResp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download index.yml: HTTP %d", indexResp.StatusCode)
}
// Read and parse index.yml
indexData, err := io.ReadAll(indexResp.Body)
if err != nil {
return fmt.Errorf("failed to read index.yml: %w", err)
}
// Parse YAML as map[string]string (version -> URL)
var index map[string]string
yamlHandler := common.YamlHandler{}
if err := yamlHandler.Unmarshal(indexData, &index); err != nil {
return fmt.Errorf("failed to parse index.yml: %w", err)
}
// Step 2: Look up the download URL for the requested version
downloadURL, found := index[version]
if !found {
return fmt.Errorf("version %s not found in index.yml (available versions: %v)", version, getKeys(index))
}
t.context.Log.Info("Found version %s in index, downloading from: %s", version, downloadURL)
// Step 3: Download the configuration archive
tmpFile, err := os.CreateTemp("", "tomcat-external-config-*.tar.gz")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download external configuration: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download external configuration: HTTP %d", resp.StatusCode)
}
// Write response to temp file
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
return fmt.Errorf("failed to write external configuration to temp file: %w", err)
}
tmpFile.Close()
// Step 4: Extract the archive to tomcatDir with strip=0
// The external config archive has structure: ./conf/...
// We extract directly to tomcatDir (no stripping needed)
t.context.Log.Info("Extracting external configuration to: %s", tomcatDir)
if err := libbuildpack.ExtractTarGzWithStrip(tmpFile.Name(), tomcatDir, 0); err != nil {
return fmt.Errorf("failed to extract external configuration: %w", err)
}
t.context.Log.Info("Successfully installed external Tomcat configuration version %s (overlaid on defaults)", version)
return nil
}
// installDefaultConfiguration installs embedded Cloud Foundry-optimized Tomcat configuration
// These defaults provide proper CF integration (dynamic ports, stdout logging, X-Forwarded-* headers, etc.)
// External configuration (if enabled) will be layered on top of these defaults
func (t *TomcatContainer) installDefaultConfiguration(tomcatDir string) error {
t.context.Log.Info("Installing Cloud Foundry-optimized Tomcat configuration defaults")
confDir := filepath.Join(tomcatDir, "conf")
if err := os.MkdirAll(confDir, 0755); err != nil {
return fmt.Errorf("failed to create conf directory: %w", err)
}
// Install embedded configuration files
configFiles := []string{
"tomcat/conf/server.xml",
"tomcat/conf/logging.properties",
"tomcat/conf/context.xml",
}
for _, configFile := range configFiles {
data, err := resources.GetResource(configFile)
if err != nil {
t.context.Log.Warning("Embedded config %s not found: %s", configFile, err)
continue
}
targetPath := filepath.Join(confDir, filepath.Base(configFile))
if err := os.WriteFile(targetPath, data, 0644); err != nil {
return fmt.Errorf("failed to write %s: %w", filepath.Base(configFile), err)
}
t.context.Log.Info("Installed default %s to %s", filepath.Base(configFile), targetPath)
}
t.context.Log.Info("Tomcat configuration includes:")
t.context.Log.Info(" - Dynamic port binding (${http.port} from $PORT)")
t.context.Log.Info(" - HTTP/2 support enabled")
t.context.Log.Info(" - RemoteIpValve for X-Forwarded-* headers")
t.context.Log.Info(" - CloudFoundryAccessLoggingValve with vcap_request_id")
t.context.Log.Info(" - Stdout logging via CloudFoundryConsoleHandler")
return nil
}
// getKeys returns the keys of a map as a slice (for error messages)
func getKeys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
// DetermineTomcatVersion determines the version of the tomcat
// based on the JBP_CONFIG_TOMCAT field from manifest.
// It looks for a tomcat block with a version of the form "<major>.+" (e.g. "9.+", "10.+", "10.1.+").
// Returns the pattern with "+" replaced by "*" (e.g. "9.*", "10.*", "10.1.*") so libbuildpack can resolve it.
// Masterminds/semver treats x, X, and * as equivalent wildcards.
func DetermineTomcatVersion(version string) string {
// Replace "+" with "*" so libbuildpack's FindMatchingVersion can resolve it.
// e.g. "9.+" -> "9.*", "10.+" -> "10.*", "10.1.+" -> "10.1.*"
return strings.ReplaceAll(version, "+", "*")
}
// isAccessLoggingEnabled checks if access logging is enabled in configuration
// Returns: "true" or "false" as a string (for use in JAVA_OPTS)
// Default: "false" (disabled, matching Ruby buildpack behavior)
// Can be enabled via: JBP_CONFIG_TOMCAT='{access_logging_support: {access_logging: enabled}}'
func (t *TomcatContainer) isAccessLoggingEnabled() string {
// Check for JBP_CONFIG_TOMCAT environment variable
if t.config.AccessLoggingSupport.AccessLogging == "enabled" || t.config.AccessLoggingSupport.AccessLogging == "true" {
t.context.Log.Info("Access logging enabled via JBP_CONFIG_TOMCAT")
return "true"
}
t.context.Log.Info("Access logging disabled by default (use JBP_CONFIG_TOMCAT to enable)")
return "false"
}
// isExternalConfigurationEnabled checks if external configuration is enabled in config
// Returns: (enabled bool, repositoryRoot string, version string)
func (t *TomcatContainer) isExternalConfigurationEnabled() (bool, string, string) {
if t.config.Tomcat.ExternalConfigurationEnabled {
repositoryRoot := t.config.ExternalConfiguration.RepositoryRoot
version := t.config.ExternalConfiguration.Version
return true, repositoryRoot, version
}
// Default to false (disabled)
return false, "", ""
}
func injectDocBase(xmlContent string, docBase string) string {
idx := strings.Index(xmlContent, "<Context")
if idx == -1 {
return xmlContent
}
endIdx := strings.Index(xmlContent[idx:], ">")
if endIdx == -1 {
return xmlContent
}
endIdx += idx
contextTag := xmlContent[idx:endIdx]
for strings.Contains(contextTag, "docBase=") {
docBaseIdx := strings.Index(contextTag, "docBase=")
if docBaseIdx+8 >= len(contextTag) {
break
}
quote := contextTag[docBaseIdx+8]
if quote != '"' && quote != '\'' {
break
}
endQuoteIdx := strings.Index(contextTag[docBaseIdx+9:], string(quote))
if endQuoteIdx == -1 {
break
}
endQuoteIdx += docBaseIdx + 9
before := strings.TrimSpace(contextTag[:docBaseIdx])
after := strings.TrimSpace(contextTag[endQuoteIdx+1:])
if before != "" && after != "" {
contextTag = before + " " + after
} else {
contextTag = before + after
}
}
newContextTag := strings.Replace(contextTag, "<Context", `<Context docBase="`+docBase+`"`, 1)
return xmlContent[:idx] + newContextTag + xmlContent[endIdx:]
}
// Finalize performs final Tomcat configuration
func (t *TomcatContainer) Finalize() error {
t.context.Log.BeginStep("Finalizing Tomcat")
buildDir := t.context.Stager.BuildDir()
contextXMLPath := filepath.Join(t.tomcatDir(), "conf", "Catalina", "localhost", "ROOT.xml")
webInf := filepath.Join(buildDir, "WEB-INF")
if _, err := os.Stat(webInf); err == nil {
// the script name is prefixed with 'zzz' as it is important to be the last script sourced from profile.d
// so that the previous scripts assembling the CLASSPATH variable(left from frameworks) are sourced previous to it.
if err := t.context.Stager.WriteProfileD("zzz_classpath_symlinks.sh", fmt.Sprintf(symlinkScript, filepath.Join("WEB-INF", "lib"))); err != nil {
return fmt.Errorf("failed to write zzz_classpath_symlinks.sh: %w", err)
}
contextXMLDir := filepath.Dir(contextXMLPath)
if err := os.MkdirAll(contextXMLDir, 0755); err != nil {
return fmt.Errorf("failed to create context directory: %w", err)
}
appContextXML := filepath.Join(buildDir, "META-INF", "context.xml")
var contextContent string
if _, err := os.Stat(appContextXML); err == nil {
xmlBytes, err := os.ReadFile(appContextXML)
if err != nil {
return fmt.Errorf("failed to read META-INF/context.xml: %w", err)
}
xmlStr := string(xmlBytes)
xmlStr = strings.TrimSpace(xmlStr)
contextContent = injectDocBase(xmlStr, "${user.home}/app")
t.context.Log.Info("Merged META-INF/context.xml with ROOT.xml - realm and resource configurations preserved")
} else {
contextContent = fmt.Sprintf("<Context docBase=\"${user.home}/app\" reloadable=\"false\">\n</Context>\n")
t.context.Log.Info("Created ROOT.xml with docBase pointing to application directory")
}
if err := os.WriteFile(contextXMLPath, []byte(contextContent), 0644); err != nil {
return fmt.Errorf("failed to write ROOT.xml: %w", err)
}
}
return nil
}
// Release returns the Tomcat startup command
func (t *TomcatContainer) Release() (string, error) {
// Use $CATALINA_HOME environment variable set by profile.d script
// Profile.d scripts run BEFORE the release command at runtime (same as $JAVA_HOME)
cmd := "$CATALINA_HOME/bin/catalina.sh run"
return cmd, nil
}
func (t *TomcatContainer) tomcatDir() string {
return filepath.Join(t.context.Stager.DepDir(), "tomcat")
}
func (t *TomcatContainer) loadConfig() (*tomcatConfig, error) {
tConfig := tomcatConfig{
Tomcat: Tomcat{
Version: "",
ExternalConfigurationEnabled: false,
},
ExternalConfiguration: ExternalConfiguration{
Version: "",
RepositoryRoot: "",
},
AccessLoggingSupport: AccessLoggingSupport{
AccessLogging: "disabled",
},
}
config := os.Getenv("JBP_CONFIG_TOMCAT")
if config != "" {
yamlHandler := common.YamlHandler{}
// overlay JBP_CONFIG_TOMCAT over default values
if err := yamlHandler.Unmarshal([]byte(config), &tConfig); err != nil {
return nil, fmt.Errorf("failed to parse JBP_CONFIG_TOMCAT: %w", err)
}
}
return &tConfig, nil
}
type tomcatConfig struct {
Tomcat Tomcat `yaml:"tomcat"`
ExternalConfiguration ExternalConfiguration `yaml:"external_configuration"`
AccessLoggingSupport AccessLoggingSupport `yaml:"access_logging_support"`
}
type Tomcat struct {
Version string `yaml:"version"`
ExternalConfigurationEnabled bool `yaml:"external_configuration_enabled"`
}
type ExternalConfiguration struct {
Version string `yaml:"version"`
RepositoryRoot string `yaml:"repository_root"`
}
type AccessLoggingSupport struct {
AccessLogging string `yaml:"access_logging"`
}