@@ -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
0 commit comments