Skip to content

Commit 7d94b5e

Browse files
committed
FrameworkPortlet properly initializes StandardPortletEnvironment's property sources and exposes local PortletRequestAttributes in case of pre-bound ServletRequestAttributes as well
Issue: SPR-11816 Issue: SPR-11295
1 parent 8ddbbc2 commit 7d94b5e

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

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

Lines changed: 14 additions & 2 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-2014 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,12 +41,15 @@
4141
import org.springframework.context.i18n.LocaleContext;
4242
import org.springframework.context.i18n.LocaleContextHolder;
4343
import org.springframework.context.i18n.SimpleLocaleContext;
44+
import org.springframework.core.env.ConfigurableEnvironment;
4445
import org.springframework.web.context.request.RequestAttributes;
4546
import org.springframework.web.context.request.RequestContextHolder;
47+
import org.springframework.web.context.request.ServletRequestAttributes;
4648
import org.springframework.web.portlet.context.ConfigurablePortletApplicationContext;
4749
import org.springframework.web.portlet.context.PortletApplicationContextUtils;
4850
import org.springframework.web.portlet.context.PortletRequestAttributes;
4951
import org.springframework.web.portlet.context.PortletRequestHandledEvent;
52+
import org.springframework.web.portlet.context.StandardPortletEnvironment;
5053
import org.springframework.web.portlet.context.XmlPortletApplicationContext;
5154

5255
/**
@@ -353,6 +356,14 @@ protected ApplicationContext createPortletApplicationContext(ApplicationContext
353356
pac.setConfigLocation(getContextConfigLocation());
354357
pac.addApplicationListener(new SourceFilteringListener(pac, this));
355358

359+
// The wac environment's #initPropertySources will be called in any case when the context
360+
// is refreshed; do it eagerly here to ensure portlet property sources are in place for
361+
// use in any post-processing or initialization that occurs below prior to #refresh
362+
ConfigurableEnvironment env = pac.getEnvironment();
363+
if (env instanceof StandardPortletEnvironment) {
364+
((StandardPortletEnvironment) env).initPropertySources(pac.getServletContext(), getPortletContext(), getPortletConfig());
365+
}
366+
356367
postProcessPortletApplicationContext(pac);
357368
pac.refresh();
358369

@@ -506,7 +517,8 @@ protected final void processRequest(PortletRequest request, PortletResponse resp
506517
// Expose current RequestAttributes to current thread.
507518
RequestAttributes previousRequestAttributes = RequestContextHolder.getRequestAttributes();
508519
PortletRequestAttributes requestAttributes = null;
509-
if (previousRequestAttributes == null || previousRequestAttributes.getClass().equals(PortletRequestAttributes.class)) {
520+
if (previousRequestAttributes == null || previousRequestAttributes.getClass().equals(PortletRequestAttributes.class) ||
521+
previousRequestAttributes.getClass().equals(ServletRequestAttributes.class)) {
510522
requestAttributes = new PortletRequestAttributes(request);
511523
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
512524
}

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

Lines changed: 23 additions & 6 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-2014 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.
@@ -31,14 +31,15 @@
3131

3232
/**
3333
* {@link Environment} implementation to be used by {@code Servlet}-based web
34-
* applications. All Portlet-related {@code ApplicationContext} classes initialize an instance
35-
* by default.
34+
* applications. All Portlet-related {@code ApplicationContext} classes
35+
* initialize an instance by default.
3636
*
37-
* <p>Contributes {@code ServletContext}, {@code PortletContext}, {@code PortletConfig}
38-
* and JNDI-based {@link PropertySource} instances. See the
39-
* {@link #customizePropertySources} method for details.
37+
* <p>Contributes {@code ServletContext}, {@code PortletContext},
38+
* {@code PortletConfig} and JNDI-based {@link PropertySource} instances.
39+
* See the {@link #customizePropertySources} method for details.
4040
*
4141
* @author Chris Beams
42+
* @author Juergen Hoeller
4243
* @since 3.1
4344
* @see StandardEnvironment
4445
* @see StandardServletEnvironment
@@ -51,6 +52,7 @@ public class StandardPortletEnvironment extends StandardEnvironment {
5152
/** Portlet config init parameters property source name: {@value} */
5253
public static final String PORTLET_CONFIG_PROPERTY_SOURCE_NAME = "portletConfigInitParams";
5354

55+
5456
/**
5557
* Customize the set of property sources with those contributed by superclasses as
5658
* well as those appropriate for standard portlet-based environments:
@@ -88,4 +90,19 @@ protected void customizePropertySources(MutablePropertySources propertySources)
8890
super.customizePropertySources(propertySources);
8991
}
9092

93+
/**
94+
* Replace any {@linkplain
95+
* org.springframework.core.env.PropertySource.StubPropertySource stub property source}
96+
* instances acting as placeholders with real portlet context/config property sources
97+
* using the given parameters.
98+
* @param servletContext the {@link ServletContext} (may be {@code null})
99+
* @param portletContext the {@link PortletContext} (may not be {@code null})
100+
* @param portletConfig the {@link PortletConfig} ({@code null} if not available)
101+
* @see org.springframework.web.portlet.context.PortletApplicationContextUtils#initPortletPropertySources(
102+
* org.springframework.core.env.MutablePropertySources, ServletContext, PortletContext, PortletConfig)
103+
*/
104+
public void initPropertySources(ServletContext servletContext, PortletContext portletContext, PortletConfig portletConfig) {
105+
PortletApplicationContextUtils.initPortletPropertySources(getPropertySources(), servletContext, portletContext, portletConfig);
106+
}
107+
91108
}

0 commit comments

Comments
 (0)