Skip to content

Commit 0186270

Browse files
committed
Tomcat Context Path
This change updates the Tomcat container to allow a user to configure the context path that an application is exposed at. Previously, the application was always exposed at the root ('/') but this didn't work for many applications that had hard-coded context-path assumptions. In addition, changes to the Cloud Foundry router mean that applications can now be routed via both a host and a context path. [#101289516]
1 parent 229250a commit 0186270

5 files changed

Lines changed: 61 additions & 13 deletions

File tree

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The `java-buildpack` is a [Cloud Foundry][] buildpack for running JVM-based appl
1010
To use this buildpack specify the URI of the repository when pushing an application to Cloud Foundry:
1111

1212
```bash
13-
cf push <APP-NAME> -p <ARTIFACT> -b https://github.com/cloudfoundry/java-buildpack.git
13+
$ cf push <APP-NAME> -p <ARTIFACT> -b https://github.com/cloudfoundry/java-buildpack.git
1414
```
1515

1616
## Examples
@@ -29,11 +29,15 @@ The buildpack supports extension through the use of Git repository forking. The
2929

3030
Buildpack configuration can be overridden with an environment variable matching the configuration file you wish to override minus the `.yml` extension and with a prefix of `JBP_CONFIG`. It is not possible to add new configuration properties and properties with `nil` or empty values will be ignored by the buildpack. The value of the variable should be valid inline yaml. For example, to change the default version of Java to 7 and adjust the memory heuristics apply this environment variable to the application.
3131

32-
```cf set-env my-application JBP_CONFIG_OPEN_JDK_JRE '[jre: {version: 1.7.0_+}, memory_calculator: {memory_heuristics: {heap: 85, stack: 10}}]'```
32+
```bash
33+
$ cf set-env my-application JBP_CONFIG_OPEN_JDK_JRE 'jre: { version: 1.7.0_+ }'
34+
```
3335

3436
If the key or value contains a special character such as `:` it should be escaped with double quotes. For example, to change the default repository path for the buildpack.
3537

36-
```cf set-env my-application JBP_CONFIG_REPOSITORY '[ default_repository_root: "http://repo.example.io" ]'```
38+
```bash
39+
$ cf set-env my-application JBP_CONFIG_REPOSITORY 'default_repository_root: "http://repo.example.io"'
40+
```
3741

3842
Environment variable can also be specified in the applications `manifest` file. See the [Environment Variables][] documentation for more information.
3943

@@ -92,8 +96,8 @@ The buildpack can be packaged up so that it can be uploaded to Cloud Foundry usi
9296
The online package is a version of the buildpack that is as minimal as possible and is configured to connect to the network for all dependencies. This package is about 50K in size. To create the online package, run:
9397

9498
```bash
95-
bundle install
96-
bundle exec rake package
99+
$ bundle install
100+
$ bundle exec rake package
97101
...
98102
Creating build/java-buildpack-cfd6b17.zip
99103
```
@@ -104,8 +108,8 @@ The offline package is a version of the buildpack designed to run without access
104108
To pin the version of dependencies used by the buildpack to the ones currently resolvable use the `PINNED=true` argument. This will update the [`config/` directory][] to contain exact version of each dependency instead of version ranges.
105109

106110
```bash
107-
bundle install
108-
bundle exec rake package OFFLINE=true PINNED=true
111+
$ bundle install
112+
$ bundle exec rake package OFFLINE=true PINNED=true
109113
...
110114
Creating build/java-buildpack-offline-cfd6b17.zip
111115
```
@@ -114,8 +118,8 @@ Creating build/java-buildpack-offline-cfd6b17.zip
114118
Keeping track of different versions of the buildpack can be difficult. To help with this, the rake `package` task puts a version discriminator in the name of the created package file. The default value for this discriminator is the current Git hash (e.g. `cfd6b17`). To change the version when creating a package, use the `VERSION=<VERSION>` argument:
115119

116120
```bash
117-
bundle install
118-
bundle exec rake package VERSION=2.1
121+
$ bundle install
122+
$ bundle exec rake package VERSION=2.1
119123
...
120124
Creating build/java-buildpack-2.1.zip
121125
```
@@ -124,8 +128,8 @@ Creating build/java-buildpack-2.1.zip
124128
To run the tests, do the following:
125129

126130
```bash
127-
bundle install
128-
bundle exec rake
131+
$ bundle install
132+
$ bundle exec rake
129133
```
130134

131135
[Running Cloud Foundry locally][] is useful for privately testing new features.

config/tomcat.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
tomcat:
1919
version: 8.0.+
2020
repository_root: ! '{default.repository.root}/tomcat'
21+
context_path:
2122
lifecycle_support:
2223
version: 2.+
2324
repository_root: ! '{default.repository.root}/tomcat-lifecycle-support'

docs/container-tomcat.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The Tomcat Container allows servlet 2 and 3 web applications to be run. These a
1212
</table>
1313
Tags are printed to standard output by the buildpack detect script
1414

15-
If the application uses Spring, [Spring profiles][] can be specified by setting the [`SPRING_PROFILES_ACTIVE`][] environment variable. This is automatically detected and used by Spring. The Spring Auto-reconfiguration Framework will specify the `cloud` profile in addition to any others.
15+
If the application uses Spring, [Spring profiles][] can be specified by setting the [`SPRING_PROFILES_ACTIVE`][] environment variable. This is automatically detected and used by Spring. The Spring Auto-reconfiguration Framework will specify the `cloud` profile in addition to any others.
1616

1717
## Configuration
1818
For general information on configuring the buildpack, refer to [Configuration and Extension][].
@@ -45,9 +45,24 @@ The container can be configured by modifying the [`config/tomcat.yml`][] file in
4545
| `redis_store.repository_root` | The URL of the Redis Store repository index ([details][repositories]).
4646
| `redis_store.timeout` | The Redis connection timeout (in milliseconds).
4747
| `redis_store.version` | The version of Redis Store to use. Candidate versions can be found in [this listing](http://download.pivotal.io.s3.amazonaws.com/redis-store/index.yml).
48+
| `tomcat.context_path` | The context path to expose the application at.
4849
| `tomcat.repository_root` | The URL of the Tomcat repository index ([details][repositories]).
4950
| `tomcat.version` | The version of Tomcat to use. Candidate versions can be found in [this listing](http://download.pivotal.io.s3.amazonaws.com/tomcat/index.yml).
5051

52+
### Common configurations
53+
The version of Tomcat can be configured by setting an environment variable.
54+
55+
```
56+
$ cf set-env my-application JBP_CONFIG_TOMCAT 'tomcat: { version: 7.0.+ }'
57+
```
58+
59+
The context path that an application is deployed at can be configured by setting an environment variable.
60+
61+
```
62+
$ cf set-env my-application JBP_CONFIG_TOMCAT 'tomcat: { context_path: /first-segement/second-segment }'
63+
```
64+
65+
5166
### Additional Resources
5267
The container can also be configured by overlaying a set of resources on the default distribution. To do this, add files to the `resources/tomcat` directory in the buildpack fork. For example, to override the default `logging.properties` add your custom file to `resources/tomcat/conf/logging.properties`.
5368

lib/java_buildpack/container/tomcat/tomcat_instance.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def expand(file)
9898
end
9999

100100
def root
101-
tomcat_webapps + 'ROOT'
101+
context_path = (@configuration['context_path'] || 'ROOT').sub(%r{^/}, '').gsub(%r{/}, '#')
102+
tomcat_webapps + context_path
102103
end
103104

104105
def tomcat_datasource_jar

spec/java_buildpack/container/tomcat/tomcat_instance_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@
9595
expect(root_webapp + '.test-file').not_to exist
9696
end
9797

98+
context do
99+
let(:configuration) { { 'context_path' => '/first-segment/second-segment' } }
100+
101+
it 'links only the application files and directories to the ROOT webapp',
102+
app_fixture: 'container_tomcat_with_index',
103+
cache_fixture: 'stub-tomcat.tar.gz' do
104+
105+
FileUtils.touch(app_dir + '.test-file')
106+
107+
component.compile
108+
109+
root_webapp = app_dir + '.java-buildpack/tomcat/webapps/first-segment#second-segment'
110+
111+
web_inf = root_webapp + 'WEB-INF'
112+
expect(web_inf).to exist
113+
expect(web_inf).to be_symlink
114+
expect(web_inf.readlink).to eq((app_dir + 'WEB-INF').relative_path_from(root_webapp))
115+
116+
index = root_webapp + 'index.html'
117+
expect(index).to exist
118+
expect(index).to be_symlink
119+
expect(index.readlink).to eq((app_dir + 'index.html').relative_path_from(root_webapp))
120+
121+
expect(root_webapp + '.test-file').not_to exist
122+
end
123+
end
124+
98125
it 'links the Tomcat datasource JAR to the ROOT webapp when that JAR is present',
99126
app_fixture: 'container_tomcat',
100127
cache_fixture: 'stub-tomcat7.tar.gz' do

0 commit comments

Comments
 (0)