Skip to content

Commit c209861

Browse files
committed
Conditional Tomcat Configuration
In an earlier, naive, attempt to make Tomcat 8 the default, the code did not take into account that certain configuration options have changed. This commit fixes the code so that resource linking is done properly depending on whether Tomcat 8 or pre-Tomcat 8 is used and only adds the JasperListener to pre-Tomcat 8 is used. [#78317410][resolves #95]
1 parent 3b65269 commit c209861

File tree

6 files changed

+102
-14
lines changed

6 files changed

+102
-14
lines changed

lib/java_buildpack/container/tomcat/tomcat_instance.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require 'java_buildpack/component/versioned_dependency_component'
1919
require 'java_buildpack/container'
2020
require 'java_buildpack/container/tomcat/tomcat_utils'
21+
require 'java_buildpack/util/tokenized_version'
2122

2223
module JavaBuildpack
2324
module Container
@@ -54,12 +55,45 @@ def supports?
5455

5556
private
5657

58+
TOMCAT_8 = JavaBuildpack::Util::TokenizedVersion.new('8.0.0').freeze
59+
60+
private_constant :TOMCAT_8
61+
62+
def configure_jasper
63+
return unless @version < TOMCAT_8
64+
65+
document = read_xml server_xml
66+
server = REXML::XPath.match(document, '/Server').first
67+
68+
listener = REXML::Element.new('Listener')
69+
listener.add_attribute 'className', 'org.apache.catalina.core.JasperListener'
70+
71+
server.insert_before '//Service', listener
72+
73+
write_xml server_xml, document
74+
end
75+
76+
def configure_linking
77+
document = read_xml context_xml
78+
context = REXML::XPath.match(document, '/Context').first
79+
80+
if @version < TOMCAT_8
81+
context.add_attribute 'allowLinking', true
82+
else
83+
context.add_element 'Resources', 'allowLinking' => true
84+
end
85+
86+
write_xml context_xml, document
87+
end
88+
5789
def expand(file)
5890
with_timing "Expanding Tomcat to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do
5991
FileUtils.mkdir_p @droplet.sandbox
6092
shell "tar xzf #{file.path} -C #{@droplet.sandbox} --strip 1 --exclude webapps 2>&1"
6193

6294
@droplet.copy_resources
95+
configure_linking
96+
configure_jasper
6397
end
6498
end
6599

lib/java_buildpack/container/tomcat/tomcat_redis_store.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
require 'java_buildpack/container'
1919
require 'java_buildpack/container/tomcat/tomcat_utils'
2020
require 'java_buildpack/logging/logger_factory'
21-
require 'rexml/document'
22-
require 'rexml/formatters/pretty'
2321

2422
module JavaBuildpack
2523
module Container
@@ -90,10 +88,6 @@ def add_valve(context)
9088
context.add_element 'Valve', 'className' => FLUSH_VALVE_CLASS_NAME
9189
end
9290

93-
def context_xml
94-
@droplet.sandbox + 'conf/context.xml'
95-
end
96-
9791
def formatter
9892
formatter = REXML::Formatters::Pretty.new(4)
9993
formatter.compact = true
@@ -107,16 +101,13 @@ def jar_name
107101
def mutate_context
108102
puts ' Adding Redis-based Session Replication'
109103

110-
document = context_xml.open { |file| REXML::Document.new file }
104+
document = read_xml context_xml
111105
context = REXML::XPath.match(document, '/Context').first
112106

113107
add_valve context
114108
add_manager context
115109

116-
context_xml.open('w') do |file|
117-
formatter.write document, file
118-
file << "\n"
119-
end
110+
write_xml context_xml, document
120111
end
121112

122113
end

lib/java_buildpack/container/tomcat/tomcat_utils.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack'
18+
require 'rexml/document'
19+
require 'rexml/formatters/pretty'
1820

1921
module JavaBuildpack
2022
module Container
2123

24+
# The Tomcat +context.xml+ file
25+
#
26+
# @return [Pathname] the Tomcat +context.xml+ file
27+
def context_xml
28+
@droplet.sandbox + 'conf/context.xml'
29+
end
30+
2231
# Link a collection of files to a destination directory, using relative paths
2332
#
2433
# @param [Array<Pathname>] source the collection of files to link
@@ -29,6 +38,21 @@ def link_to(source, destination)
2938
source.each { |path| (destination + path.basename).make_symlink(path.relative_path_from(destination)) }
3039
end
3140

41+
# Read an XML file into a +REXML::Document+
42+
#
43+
# @param [Pathname] file the file to read
44+
# @return [REXML::Document] the file parsed into a +REXML::Document+
45+
def read_xml(file)
46+
file.open { |f| REXML::Document.new f }
47+
end
48+
49+
# The Tomcat +server.xml+ file
50+
#
51+
# @return [Pathname] The Tomcat +server.xml+ file
52+
def server_xml
53+
@droplet.sandbox + 'conf/server.xml'
54+
end
55+
3256
# The Tomcat +lib+ directory
3357
#
3458
# @return [Pathname] the Tomcat +lib+ directory
@@ -43,5 +67,24 @@ def tomcat_webapps
4367
@droplet.sandbox + 'webapps'
4468
end
4569

70+
# Write a properly formatted XML file
71+
#
72+
# @param [Pathname] file the file to write
73+
# @return [Void]
74+
def write_xml(file, document)
75+
file.open('w') do |f|
76+
formatter.write document, f
77+
f << "\n"
78+
end
79+
end
80+
81+
private
82+
83+
def formatter
84+
formatter = REXML::Formatters::Pretty.new(4)
85+
formatter.compact = true
86+
formatter
87+
end
88+
4689
end
4790
end

resources/tomcat/conf/context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
~ limitations under the License.
1616
-->
1717

18-
<Context allowLinking='true'>
18+
<Context>
1919
</Context>

resources/tomcat/conf/server.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
<Server port='-1'>
2020

21-
<Listener className='org.apache.catalina.core.JasperListener'/>
22-
2321
<Service name='Catalina'>
2422
<Connector port='${http.port}' bindOnInit="false"/>
2523

spec/java_buildpack/container/tomcat/tomcat_instance_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@
4848
expect(sandbox + 'conf/server.xml').to exist
4949
end
5050

51+
it 'should configure for Tomcat 7',
52+
app_fixture: 'container_tomcat',
53+
cache_fixture: 'stub-tomcat.tar.gz' do
54+
55+
component.compile
56+
expect((sandbox + 'conf/context.xml').read).to match(/<Context allowLinking='true'>/)
57+
expect((sandbox + 'conf/server.xml').read).to match(/<Listener className='org.apache.catalina.core.JasperListener'\/>/)
58+
end
59+
60+
context do
61+
let(:version) { '8.0.12' }
62+
63+
it 'should configure for Tomcat 8',
64+
app_fixture: 'container_tomcat',
65+
cache_fixture: 'stub-tomcat.tar.gz' do
66+
67+
component.compile
68+
expect((sandbox + 'conf/context.xml').read).to match(/<Context>[\s]*<Resources allowLinking='true'\/>/)
69+
expect((sandbox + 'conf/server.xml').read).not_to match(/<Listener className='org.apache.catalina.core.JasperListener'\/>/)
70+
end
71+
end
72+
5173
it 'should link only the application files and directories to the ROOT webapp',
5274
app_fixture: 'container_tomcat_with_index',
5375
cache_fixture: 'stub-tomcat.tar.gz' do

0 commit comments

Comments
 (0)