1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@ public class DelegatingFilterProxy extends GenericFilterBean {
88
88
89
89
private boolean targetFilterLifecycle = false ;
90
90
91
- private Filter delegate ;
91
+ private volatile Filter delegate ;
92
92
93
93
private final Object delegateMonitor = new Object ();
94
94
@@ -227,7 +227,6 @@ protected void initFilterBean() throws ServletException {
227
227
if (this .targetBeanName == null ) {
228
228
this .targetBeanName = getFilterName ();
229
229
}
230
-
231
230
// Fetch Spring root application context and initialize the delegate early,
232
231
// if possible. If the root application context will be started after this
233
232
// filter proxy, we'll have to resort to lazy initialization.
@@ -243,16 +242,18 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
243
242
throws ServletException , IOException {
244
243
245
244
// Lazily initialize the delegate if necessary.
246
- Filter delegateToUse = null ;
247
- synchronized (this .delegateMonitor ) {
248
- if (this .delegate == null ) {
249
- WebApplicationContext wac = findWebApplicationContext ();
250
- if (wac == null ) {
251
- throw new IllegalStateException ("No WebApplicationContext found: no ContextLoaderListener registered?" );
245
+ Filter delegateToUse = this .delegate ;
246
+ if (delegateToUse == null ) {
247
+ synchronized (this .delegateMonitor ) {
248
+ if (this .delegate == null ) {
249
+ WebApplicationContext wac = findWebApplicationContext ();
250
+ if (wac == null ) {
251
+ throw new IllegalStateException ("No WebApplicationContext found: no ContextLoaderListener registered?" );
252
+ }
253
+ this .delegate = initDelegate (wac );
252
254
}
253
- this . delegate = initDelegate ( wac ) ;
255
+ delegateToUse = this . delegate ;
254
256
}
255
- delegateToUse = this .delegate ;
256
257
}
257
258
258
259
// Let the delegate perform the actual doFilter operation.
@@ -261,10 +262,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
261
262
262
263
@ Override
263
264
public void destroy () {
264
- Filter delegateToUse = null ;
265
- synchronized (this .delegateMonitor ) {
266
- delegateToUse = this .delegate ;
267
- }
265
+ Filter delegateToUse = this .delegate ;
268
266
if (delegateToUse != null ) {
269
267
destroyDelegate (delegateToUse );
270
268
}
@@ -281,8 +279,7 @@ public void destroy() {
281
279
* {@code ServletContext} before this filter gets initialized (or invoked).
282
280
* <p>Subclasses may override this method to provide a different
283
281
* {@code WebApplicationContext} retrieval strategy.
284
- * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not
285
- * found
282
+ * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not found
286
283
* @see #DelegatingFilterProxy(String, WebApplicationContext)
287
284
* @see #getContextAttribute()
288
285
* @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext)
0 commit comments