Skip to content

Commit 68b1eb1

Browse files
committed
Polishing
1 parent 5fb2665 commit 68b1eb1

File tree

7 files changed

+55
-84
lines changed

7 files changed

+55
-84
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@
6161
*/
6262
public abstract class AbstractContextLoader implements SmartContextLoader {
6363

64-
private static final Log logger = LogFactory.getLog(AbstractContextLoader.class);
65-
6664
private static final String[] EMPTY_STRING_ARRAY = new String[0];
65+
6766
private static final String SLASH = "/";
6867

68+
private static final Log logger = LogFactory.getLog(AbstractContextLoader.class);
69+
6970

7071
// --- SmartContextLoader -----------------------------------------------
7172

@@ -79,23 +80,19 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
7980
* processed locations are then
8081
* {@link ContextConfigurationAttributes#setLocations(String[]) set} in
8182
* the supplied configuration attributes.
82-
*
8383
* <p>Can be overridden in subclasses &mdash; for example, to process
8484
* annotated classes instead of resource locations.
85-
*
8685
* @since 3.1
8786
* @see #processLocations(Class, String...)
8887
*/
8988
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
90-
String[] processedLocations = processLocations(configAttributes.getDeclaringClass(),
91-
configAttributes.getLocations());
89+
String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations());
9290
configAttributes.setLocations(processedLocations);
9391
}
9492

9593
/**
9694
* Prepare the {@link ConfigurableApplicationContext} created by this
9795
* {@code SmartContextLoader} <i>before</i> bean definitions are read.
98-
*
9996
* <p>The default implementation:
10097
* <ul>
10198
* <li>Sets the <em>active bean definition profiles</em> from the supplied
@@ -106,17 +103,15 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
106103
* {@linkplain ApplicationContextInitializer#initialize invokes each} with the
107104
* given application context.</li>
108105
* </ul>
109-
*
110106
* <p>Any {@code ApplicationContextInitializers} implementing
111107
* {@link org.springframework.core.Ordered Ordered} or marked with {@link
112108
* org.springframework.core.annotation.Order @Order} will be sorted appropriately.
113-
*
114109
* @param context the newly created application context
115110
* @param mergedConfig the merged context configuration
111+
* @since 3.2
116112
* @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
117113
* @see #loadContext(MergedContextConfiguration)
118114
* @see ConfigurableApplicationContext#setId
119-
* @since 3.2
120115
*/
121116
@SuppressWarnings("unchecked")
122117
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
@@ -129,17 +124,18 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
129124
return;
130125
}
131126

132-
final List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
133-
final Class<?> contextClass = context.getClass();
127+
List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances =
128+
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
129+
Class<?> contextClass = context.getClass();
134130

135131
for (Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>> initializerClass : initializerClasses) {
136132
Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
137-
ApplicationContextInitializer.class);
133+
ApplicationContextInitializer.class);
138134
Assert.isAssignable(initializerContextClass, contextClass, String.format(
139-
"Could not add context initializer [%s] since its generic parameter [%s] "
140-
+ "is not assignable from the type of application context used by this "
141-
+ "context loader [%s]: ", initializerClass.getName(), initializerContextClass.getName(),
142-
contextClass.getName()));
135+
"Could not add context initializer [%s] since its generic parameter [%s] " +
136+
"is not assignable from the type of application context used by this " +
137+
"context loader [%s]: ", initializerClass.getName(), initializerContextClass.getName(),
138+
contextClass.getName()));
143139
initializerInstances.add((ApplicationContextInitializer<ConfigurableApplicationContext>) BeanUtils.instantiateClass(initializerClass));
144140
}
145141

@@ -160,7 +156,6 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
160156
* {@link #getResourceSuffix() resource suffix}; otherwise, the supplied
161157
* {@code locations} will be {@link #modifyLocations modified} if
162158
* necessary and returned.
163-
*
164159
* @param clazz the class with which the locations are associated: to be
165160
* used when generating default locations
166161
* @param locations the unmodified locations to use for loading the
@@ -174,29 +169,25 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
174169
* @see #processContextConfiguration(ContextConfigurationAttributes)
175170
*/
176171
public final String[] processLocations(Class<?> clazz, String... locations) {
177-
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? generateDefaultLocations(clazz)
178-
: modifyLocations(clazz, locations);
172+
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ?
173+
generateDefaultLocations(clazz) : modifyLocations(clazz, locations);
179174
}
180175

181176
/**
182177
* Generate the default classpath resource locations array based on the
183178
* supplied class.
184-
*
185179
* <p>For example, if the supplied class is {@code com.example.MyTest},
186180
* the generated locations will contain a single string with a value of
187181
* &quot;classpath:/com/example/MyTest{@code <suffix>}&quot;,
188182
* where {@code <suffix>} is the value of the
189183
* {@link #getResourceSuffix() resource suffix} string.
190-
*
191184
* <p>As of Spring 3.1, the implementation of this method adheres to the
192185
* contract defined in the {@link SmartContextLoader} SPI. Specifically,
193186
* this method will <em>preemptively</em> verify that the generated default
194187
* location actually exists. If it does not exist, this method will log a
195188
* warning and return an empty array.
196-
*
197189
* <p>Subclasses can override this method to implement a different
198190
* <em>default location generation</em> strategy.
199-
*
200191
* @param clazz the class for which the default locations are to be generated
201192
* @return an array of default application context resource locations
202193
* @since 2.5
@@ -212,23 +203,22 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
212203

213204
if (classPathResource.exists()) {
214205
if (logger.isInfoEnabled()) {
215-
logger.info(String.format("Detected default resource location \"%s\" for test class [%s].",
216-
prefixedResourcePath, clazz.getName()));
206+
logger.info(String.format("Detected default resource location \"%s\" for test class [%s]",
207+
prefixedResourcePath, clazz.getName()));
217208
}
218-
return new String[] { prefixedResourcePath };
209+
return new String[] {prefixedResourcePath};
219210
}
220-
221-
// else
222-
if (logger.isInfoEnabled()) {
223-
logger.info(String.format("Could not detect default resource locations for test class [%s]: "
224-
+ "%s does not exist.", clazz.getName(), classPathResource));
211+
else {
212+
if (logger.isInfoEnabled()) {
213+
logger.info(String.format("Could not detect default resource locations for test class [%s]: " +
214+
"%s does not exist", clazz.getName(), classPathResource));
215+
}
216+
return EMPTY_STRING_ARRAY;
225217
}
226-
return EMPTY_STRING_ARRAY;
227218
}
228219

229220
/**
230221
* Generate a modified version of the supplied locations array and return it.
231-
*
232222
* <p>A plain path &mdash; for example, &quot;context.xml&quot; &mdash; will
233223
* be treated as a classpath resource that is relative to the package in which
234224
* the specified class is defined. A path starting with a slash is treated
@@ -238,10 +228,8 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
238228
* {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
239229
* {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:},
240230
* etc.) will be added to the results unchanged.
241-
*
242231
* <p>Subclasses can override this method to implement a different
243232
* <em>location modification</em> strategy.
244-
*
245233
* @param clazz the class with which the locations are associated
246234
* @param locations the resource locations to be modified
247235
* @return an array of modified application context resource locations
@@ -253,10 +241,12 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
253241
String path = locations[i];
254242
if (path.startsWith(SLASH)) {
255243
modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
256-
} else if (!ResourcePatternUtils.isUrl(path)) {
257-
modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH
258-
+ StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
259-
} else {
244+
}
245+
else if (!ResourcePatternUtils.isUrl(path)) {
246+
modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH +
247+
StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
248+
}
249+
else {
260250
modifiedLocations[i] = StringUtils.cleanPath(path);
261251
}
262252
}
@@ -267,7 +257,6 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
267257
* Determine whether or not <em>default</em> resource locations should be
268258
* generated if the {@code locations} provided to
269259
* {@link #processLocations(Class, String...)} are {@code null} or empty.
270-
*
271260
* <p>As of Spring 3.1, the semantics of this method have been overloaded
272261
* to include detection of either default resource locations or default
273262
* configuration classes. Consequently, this method can also be used to
@@ -276,9 +265,7 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
276265
* {@link ContextConfigurationAttributes configuration attributes} supplied
277266
* to {@link #processContextConfiguration(ContextConfigurationAttributes)}
278267
* are {@code null} or empty.
279-
*
280268
* <p>Can be overridden by subclasses to change the default behavior.
281-
*
282269
* @return always {@code true} by default
283270
* @since 2.5
284271
*/
@@ -289,9 +276,7 @@ protected boolean isGenerateDefaultLocations() {
289276
/**
290277
* Get the suffix to append to {@link ApplicationContext} resource locations
291278
* when generating default locations.
292-
*
293279
* <p>Must be implemented by subclasses.
294-
*
295280
* @return the resource suffix; should not be {@code null} or empty
296281
* @since 2.5
297282
* @see #generateDefaultLocations(Class)

spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,9 @@ protected void onRefresh() {
201201
*/
202202
@Override
203203
protected void initPropertySources() {
204-
super.initPropertySources();
205-
ConfigurableEnvironment env = this.getEnvironment();
204+
ConfigurableEnvironment env = getEnvironment();
206205
if (env instanceof ConfigurableWebEnvironment) {
207-
((ConfigurableWebEnvironment)env).initPropertySources(
208-
this.servletContext, this.servletConfig);
206+
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
209207
}
210208
}
211209

spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.ui.context.Theme;
2929
import org.springframework.ui.context.ThemeSource;
3030
import org.springframework.ui.context.support.UiApplicationContextUtils;
31-
import org.springframework.util.Assert;
3231
import org.springframework.util.ObjectUtils;
3332
import org.springframework.util.StringUtils;
3433
import org.springframework.web.context.ConfigurableWebApplicationContext;
@@ -192,11 +191,9 @@ protected void onRefresh() {
192191
*/
193192
@Override
194193
protected void initPropertySources() {
195-
super.initPropertySources();
196-
ConfigurableEnvironment env = this.getEnvironment();
194+
ConfigurableEnvironment env = getEnvironment();
197195
if (env instanceof ConfigurableWebEnvironment) {
198-
((ConfigurableWebEnvironment)env).initPropertySources(
199-
this.servletContext, null);
196+
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
200197
}
201198
}
202199

spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,7 @@
4141
* @since 3.1
4242
* @see StandardEnvironment
4343
*/
44-
public class StandardServletEnvironment extends StandardEnvironment
45-
implements ConfigurableWebEnvironment {
44+
public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {
4645

4746
/** Servlet context init parameters property source name: {@value} */
4847
public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";
@@ -91,8 +90,7 @@ protected void customizePropertySources(MutablePropertySources propertySources)
9190
}
9291

9392
public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
94-
WebApplicationContextUtils.initServletPropertySources(
95-
this.getPropertySources(), servletContext, servletConfig);
93+
WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
9694
}
9795

9896
}

spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ protected void onRefresh() {
177177

178178
@Override
179179
protected void initPropertySources() {
180-
super.initPropertySources();
181-
WebApplicationContextUtils.initServletPropertySources(
182-
this.getEnvironment().getPropertySources(), this.servletContext, this.servletConfig);
180+
WebApplicationContextUtils.initServletPropertySources(getEnvironment().getPropertySources(),
181+
this.servletContext, this.servletConfig);
183182
}
184183

185184
public Theme getTheme(String themeName) {

spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,11 @@
1616

1717
package org.springframework.web.context.support;
1818

19-
import static org.springframework.web.context.support.StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
20-
import static org.springframework.web.context.support.StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
21-
2219
import java.io.Serializable;
2320
import java.util.Collections;
2421
import java.util.Enumeration;
2522
import java.util.HashMap;
2623
import java.util.Map;
27-
2824
import javax.faces.context.ExternalContext;
2925
import javax.faces.context.FacesContext;
3026
import javax.servlet.ServletConfig;
@@ -51,19 +47,18 @@
5147
/**
5248
* Convenience methods for retrieving the root
5349
* {@link org.springframework.web.context.WebApplicationContext} for a given
54-
* {@code ServletContext}. This is e.g. useful for accessing a Spring
55-
* context from within custom web views or Struts actions.
50+
* {@code ServletContext}. This is useful for programmatically accessing a
51+
* Spring application context from within custom web views or MVC actions.
5652
*
5753
* <p>Note that there are more convenient ways of accessing the root context for
58-
* many web frameworks, either part of Spring or available as external library.
54+
* many web frameworks, either part of Spring or available as an external library.
5955
* This helper class is just the most generic way to access the root context.
6056
*
6157
* @author Juergen Hoeller
6258
* @see org.springframework.web.context.ContextLoader
6359
* @see org.springframework.web.servlet.FrameworkServlet
6460
* @see org.springframework.web.servlet.DispatcherServlet
6561
* @see org.springframework.web.jsf.FacesContextUtils
66-
* @see org.springframework.web.jsf.SpringBeanVariableResolver
6762
* @see org.springframework.web.jsf.el.SpringBeanFacesELResolver
6863
*/
6964
public abstract class WebApplicationContextUtils {
@@ -260,16 +255,17 @@ public static void initServletPropertySources(
260255
*/
261256
public static void initServletPropertySources(
262257
MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
258+
263259
Assert.notNull(propertySources, "propertySources must not be null");
264-
if(servletContext != null &&
265-
propertySources.contains(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
266-
propertySources.get(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
267-
propertySources.replace(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new ServletContextPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
260+
if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
261+
propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
262+
propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
263+
new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
268264
}
269-
if(servletConfig != null &&
270-
propertySources.contains(SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
271-
propertySources.get(SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
272-
propertySources.replace(SERVLET_CONFIG_PROPERTY_SOURCE_NAME, new ServletConfigPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
265+
if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
266+
propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
267+
propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
268+
new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
273269
}
274270
}
275271

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,10 +78,8 @@ protected ConfigurableEnvironment createEnvironment() {
7878
*/
7979
@Override
8080
protected void initPropertySources() {
81-
super.initPropertySources();
82-
PortletApplicationContextUtils.initPortletPropertySources(
83-
this.getEnvironment().getPropertySources(), this.servletContext,
84-
this.portletContext, this.portletConfig);
81+
PortletApplicationContextUtils.initPortletPropertySources(getEnvironment().getPropertySources(),
82+
this.servletContext, this.portletContext, this.portletConfig);
8583
}
8684

8785
/**

0 commit comments

Comments
 (0)