Skip to content

Commit d3d17c5

Browse files
committed
Update web.xml modification for auto-reconfiguration 1.0.0
This change updates the web.xml modification behavior to support auto- reconfiguration 1.0.0. The modification no longer makes any change to the application context configuration XML files or Java classes. It now only adds three ApplicationContextInitializers to the configuration. In cases where there is no web.xml file, auto- reconfiguration 1.0.0 will use a web fragment to configure itself. [#49695439]
1 parent f8fff2c commit d3d17c5

16 files changed

Lines changed: 35 additions & 275 deletions

config/play_framework_jpa_plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
# Configuration for the Play JPA Plugin framework
1717
---
18-
version: 0.+
18+
version: 1.0.+
1919
repository_root: "{default.repository.root}/play-jpa-plugin"

config/spring_auto_reconfiguration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
# Note that the repository is shared with the Play Auto Reconfiguration framework and should be kept in step to
1818
# avoid conflicts.
1919
---
20-
version: 0.+
20+
version: 1.0.+
2121
repository_root: "{default.repository.root}/auto-reconfiguration"

lib/java_buildpack/framework/spring_auto_reconfiguration/web_xml_modifier.rb

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@ module JavaBuildpack
2222
module Framework
2323

2424
# A class that encapsulates the modification of a +web.xml+ Servlet configuration file for the Auto-reconfiguration
25-
# framework. The modifications of +web.xml+ consist of two major behaviors.
26-
#
27-
# 1. Augmenting +contextConfigLocation+. The function starts be enumerating the current +contextConfigLocation+ s.
28-
# If none exist, a default configuration is created with +/WEB-INF/application-context.xml+ or
29-
# +/WEB-INF/<servlet-name>-servlet.xml+ as the default. An additional location is then added to the collection of
30-
# locations; +classpath:META- INF/cloud/cloudfoundry-auto-reconfiguration-context.xml+ if the +ApplicationContext+
31-
# is XML-based, +org.cloudfoundry.reconfiguration.spring.web.CloudAppAnnotationConfigAutoReconfig+ if the
32-
# +ApplicationContext+ is annotation-based.
33-
#
34-
# 2. Augmenting +contextInitializerClasses+. The function starts by enumerating the current
35-
# +contextInitializerClasses+. If none exist, a default configuration is created with no value as the default.
36-
# The +org.cloudfoundry.reconfiguration.spring.CloudApplicationContextInitializer+ class is then added to the
37-
# collection of classes.
25+
# framework. The modifications of +web.xml+ consist of augmenting +contextInitializerClasses+. The function starts
26+
# by enumerating the current +contextInitializerClasses+. If none exist, a default configuration is created with no
27+
# value as the default. The +org.cloudfoundry.reconfiguration.spring.CloudProfileApplicationContextInitializer+,
28+
# +org.cloudfoundry.reconfiguration.spring.CloudPropertySourceApplicationContextInitializer+, and
29+
# +org.cloudfoundry.reconfiguration.spring.CloudAutoReconfigurationApplicationContextInitializer+ classes are then
30+
# added to the collection of classes.
3831
class WebXmlModifier
3932

4033
# Creates a new instance of the modifier.
@@ -48,19 +41,15 @@ def initialize(source)
4841
#
4942
# @return [Void]
5043
def augment_root_context
51-
if context_loader_listener?
52-
augment_context_config_locations web_app(@document), 'context-param', CONTEXT_LOCATION_DEFAULT
53-
augment_context_initializer_classes web_app(@document), 'context-param'
54-
end
44+
augment web_app(@document), 'context-param' if context_loader_listener?
5545
end
5646

5747
# Make modifications to the the servlet contexts
5848
#
5949
# @return [Void]
6050
def augment_servlet_contexts
6151
servlets.each do |servlet|
62-
augment_context_config_locations servlet, 'init-param', default_servlet_context_location(servlet)
63-
augment_context_initializer_classes servlet, 'init-param'
52+
augment servlet, 'init-param'
6453
end
6554
end
6655

@@ -77,55 +66,33 @@ def to_s
7766

7867
private
7968

80-
CONTEXT_CLASS = 'contextClass'.freeze
81-
82-
CONTEXT_CLASS_ANNOTATION = 'org.springframework.web.context.support.AnnotationConfigWebApplicationContext'.freeze
83-
84-
CONTEXT_CONFIG_LOCATION = 'contextConfigLocation'.freeze
85-
86-
CONTEXT_INITIALIZER_ADDITIONAL = 'org.cloudfoundry.reconfiguration.spring.CloudApplicationContextInitializer'.freeze
69+
CONTEXT_INITIALIZER_ADDITIONAL = %w(
70+
org.cloudfoundry.reconfiguration.spring.CloudProfileApplicationContextInitializer
71+
org.cloudfoundry.reconfiguration.spring.CloudPropertySourceApplicationContextInitializer
72+
org.cloudfoundry.reconfiguration.spring.CloudAutoReconfigurationApplicationContextInitializer).freeze
8773

8874
CONTEXT_INITIALIZER_CLASSES = 'contextInitializerClasses'.freeze
8975

9076
CONTEXT_LOADER_LISTENER = 'ContextLoaderListener'.freeze
9177

92-
CONTEXT_LOCATION_ADDITIONAL_ANNOTATION = 'org.cloudfoundry.reconfiguration.spring.web.CloudAppAnnotationConfigAutoReconfig'.freeze
93-
94-
CONTEXT_LOCATION_ADDITIONAL_XML = 'classpath:META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml'.freeze
95-
96-
CONTEXT_LOCATION_DEFAULT = '/WEB-INF/applicationContext.xml'.freeze
97-
9878
DISPATCHER_SERVLET = 'DispatcherServlet'.freeze
9979

100-
private_constant :CONTEXT_CLASS, :CONTEXT_CLASS_ANNOTATION, :CONTEXT_CONFIG_LOCATION,
101-
:CONTEXT_INITIALIZER_ADDITIONAL, :CONTEXT_INITIALIZER_CLASSES, :CONTEXT_LOADER_LISTENER,
102-
:CONTEXT_LOCATION_ADDITIONAL_ANNOTATION, :CONTEXT_LOCATION_ADDITIONAL_XML,
103-
:CONTEXT_LOCATION_DEFAULT, :DISPATCHER_SERVLET
104-
105-
def additional_context_config_location(root, param_type)
106-
annotation_application_context?(root, param_type) ? CONTEXT_LOCATION_ADDITIONAL_ANNOTATION : CONTEXT_LOCATION_ADDITIONAL_XML
107-
end
108-
109-
def augment_context_config_locations(root, param_type, default_location)
110-
locations_string = xpath(root, "#{param_type}[param-name[contains(text(), '#{CONTEXT_CONFIG_LOCATION}')]]/param-value/text()").first
111-
locations_string = create_param(root, param_type, CONTEXT_CONFIG_LOCATION, default_location) unless locations_string
112-
113-
locations = locations_string.value.strip.split(/[,;\s]+/)
114-
locations << additional_context_config_location(root, param_type)
115-
116-
locations_string.value = locations.join(' ') # rubocop:disable UselessSetterCall
117-
end
80+
private_constant :CONTEXT_INITIALIZER_CLASSES, :CONTEXT_LOADER_LISTENER, :DISPATCHER_SERVLET
11881

119-
def augment_context_initializer_classes(root, param_type)
82+
def augment(root, param_type)
12083
classes_string = xpath(root, "#{param_type}[param-name[contains(text(), '#{CONTEXT_INITIALIZER_CLASSES}')]]/param-value/text()").first
12184
classes_string = create_param(root, param_type, CONTEXT_INITIALIZER_CLASSES, '') unless classes_string
12285

12386
classes = classes_string.value.strip.split(/[,;\s]+/)
124-
classes << CONTEXT_INITIALIZER_ADDITIONAL
87+
classes = classes.concat CONTEXT_INITIALIZER_ADDITIONAL
12588

12689
classes_string.value = classes.join(',') # rubocop:disable UselessSetterCall
12790
end
12891

92+
def context_loader_listener?
93+
xpath(@document, "/web-app/listener/listener-class[contains(text(), '#{CONTEXT_LOADER_LISTENER}')]").any?
94+
end
95+
12996
def create_param(root, param_type, name, value)
13097
param = REXML::Element.new param_type, root
13198

@@ -136,27 +103,12 @@ def create_param(root, param_type, name, value)
136103
REXML::Text.new value, true, param_value
137104
end
138105

139-
def default_servlet_context_location(servlet)
140-
name = xpath(servlet, 'servlet-name/text()').first.value.strip
141-
"/WEB-INF/#{name}-servlet.xml"
142-
end
143-
144106
def formatter
145107
formatter = REXML::Formatters::Pretty.new(4)
146108
formatter.compact = true
147109
formatter
148110
end
149111

150-
def annotation_application_context?(root, param_type)
151-
context_class_name = xpath(root, "#{param_type}[param-name[contains(text(), '#{CONTEXT_CLASS}')]]/param-value/text()").first
152-
context_class_name_value = context_class_name ? context_class_name.value.strip : nil
153-
CONTEXT_CLASS_ANNOTATION == context_class_name_value
154-
end
155-
156-
def context_loader_listener?
157-
xpath(@document, "/web-app/listener/listener-class[contains(text(), '#{CONTEXT_LOADER_LISTENER}')]").any?
158-
end
159-
160112
def servlets
161113
xpath(@document, "/web-app/servlet[servlet-class[contains(text(), '#{DISPATCHER_SERVLET}')]]")
162114
end

spec/fixtures/web_root_annotation_after.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

spec/fixtures/web_root_annotation_before.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

spec/fixtures/web_root_annotation_wrong_class_after.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

spec/fixtures/web_root_annotation_wrong_class_before.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

spec/fixtures/web_root_existing_params_after.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33
<listener>
44
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
55
</listener>
6-
<context-param>
7-
<param-name> contextConfigLocation </param-name>
8-
<param-value>
9-
/WEB-INF/dataContext.xml /WEB-INF/repositoryContext.xml
10-
/WEB-INF/serviceContext.xml /WEB-INF/utilContext.xml
11-
classpath:META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml
12-
</param-value>
13-
</context-param>
146
<context-param>
157
<param-name> contextInitializerClasses </param-name>
168
<param-value>
17-
com.gopivotal.test,org.cloudfoundry.reconfiguration.spring.CloudApplicationContextInitializer
9+
com.gopivotal.test,org.cloudfoundry.reconfiguration.spring.CloudProfileApplicationContextInitializer,org.cloudfoundry.reconfiguration.spring.CloudPropertySourceApplicationContextInitializer,org.cloudfoundry.reconfiguration.spring.CloudAutoReconfigurationApplicationContextInitializer
1810
</param-value>
1911
</context-param>
2012
</web-app>

spec/fixtures/web_root_existing_params_before.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
</listener-class>
88
</listener>
99

10-
<context-param>
11-
<param-name>
12-
contextConfigLocation
13-
</param-name>
14-
<param-value>
15-
/WEB-INF/dataContext.xml, /WEB-INF/repositoryContext.xml; /WEB-INF/serviceContext.xml /WEB-INF/utilContext.xml
16-
</param-value>
17-
</context-param>
18-
1910
<context-param>
2011
<param-name>
2112
contextInitializerClasses

spec/fixtures/web_root_no_params_after.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
55
</listener>
66
<context-param>
7-
<param-name>contextConfigLocation</param-name>
7+
<param-name>contextInitializerClasses</param-name>
88
<param-value>
9-
/WEB-INF/applicationContext.xml
10-
classpath:META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml
9+
org.cloudfoundry.reconfiguration.spring.CloudProfileApplicationContextInitializer,org.cloudfoundry.reconfiguration.spring.CloudPropertySourceApplicationContextInitializer,org.cloudfoundry.reconfiguration.spring.CloudAutoReconfigurationApplicationContextInitializer
1110
</param-value>
1211
</context-param>
13-
<context-param>
14-
<param-name>contextInitializerClasses</param-name>
15-
<param-value>org.cloudfoundry.reconfiguration.spring.CloudApplicationContextInitializer</param-value>
16-
</context-param>
1712
</web-app>

0 commit comments

Comments
 (0)