The Spring Framework

org.springframework.web.portlet
Class FrameworkPortlet

java.lang.Object
  extended by javax.portlet.GenericPortlet
      extended by org.springframework.web.portlet.GenericPortletBean
          extended by org.springframework.web.portlet.FrameworkPortlet
All Implemented Interfaces:
EventListener, javax.portlet.Portlet, javax.portlet.PortletConfig, ApplicationListener
Direct Known Subclasses:
DispatcherPortlet

public abstract class FrameworkPortlet
extends GenericPortletBean
implements ApplicationListener

Base portlet for Spring's portlet framework. Provides integration with a Spring application context, in a JavaBean-based overall solution.

This class offers the following functionality:

Subclasses must implement doActionService(javax.portlet.ActionRequest, javax.portlet.ActionResponse) and doRenderService(javax.portlet.RenderRequest, javax.portlet.RenderResponse) to handle action and render requests. Because this extends GenericPortletBean rather than Portlet directly, bean properties are mapped onto it. Subclasses can override initFrameworkPortlet() for custom initialization.

Regards a "contextClass" parameter at the portlet init-param level, falling back to the default context class (XmlPortletApplicationContext) if not found. Note that, with the default FrameworkPortlet, a context class needs to implement the ConfigurablePortletApplicationContext SPI.

Passes a "contextConfigLocation" portlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "test-portlet.xml, myPortlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the portlet.

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using one of Spring's default ApplicationContext implementations. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

The default namespace is "'portlet-name'-portlet", e.g. "test-portlet" for a portlet-name "test" (leading to a "/WEB-INF/test-portlet.xml" default location with XmlPortletApplicationContext). The namespace can also be set explicitly via the "namespace" portlet init-param.

Since:
2.0
Author:
William G. Thompson, Jr., John A. Lewis, Juergen Hoeller
See Also:
doActionService(javax.portlet.ActionRequest, javax.portlet.ActionResponse), doRenderService(javax.portlet.RenderRequest, javax.portlet.RenderResponse), setContextClass(java.lang.Class), setContextConfigLocation(java.lang.String), setNamespace(java.lang.String)

Field Summary
static Class DEFAULT_CONTEXT_CLASS
          Default context class for FrameworkPortlet.
static String DEFAULT_NAMESPACE_SUFFIX
          Suffix for Portlet ApplicationContext namespaces.
static String[] DEFAULT_USERINFO_ATTRIBUTE_NAMES
          Default USER_INFO attribute names to search for the current username: "user.login.id", "user.name".
static String PORTLET_CONTEXT_PREFIX
          Prefix for the PortletContext attribute for the Portlet ApplicationContext.
 
Fields inherited from class org.springframework.web.portlet.GenericPortletBean
logger
 
Constructor Summary
FrameworkPortlet()
           
 
Method Summary
protected  ApplicationContext createPortletApplicationContext(ApplicationContext parent)
          Instantiate the Portlet ApplicationContext for this portlet, either a default XmlPortletApplicationContext or a custom context class if set.
 void destroy()
          Close the ApplicationContext of this portlet.
protected abstract  void doActionService(javax.portlet.ActionRequest request, javax.portlet.ActionResponse response)
          Subclasses must implement this method to do the work of action request handling.
protected  void doDispatch(javax.portlet.RenderRequest request, javax.portlet.RenderResponse response)
          Delegate render requests to processRequest/doRenderService.
protected abstract  void doRenderService(javax.portlet.RenderRequest request, javax.portlet.RenderResponse response)
          Subclasses must implement this method to do the work of render request handling.
 Class getContextClass()
          Return the custom context class.
 String getContextConfigLocation()
          Return the explicit context config location, if any.
 String getNamespace()
          Return the namespace for this portlet, falling back to default scheme if no custom namespace was set.
 ApplicationContext getPortletApplicationContext()
          Return this portlet's ApplicationContext.
 String getPortletContextAttributeName()
          Return the PortletContext attribute name for this portlets's ApplicationContext.
protected  String getTitle(javax.portlet.RenderRequest renderRequest)
          Overridden for friendlier behavior in unit tests.
 String[] getUserinfoUsernameAttributes()
          Returns the list of attributes that will be searched in the USER_INFO map when trying to find the username of the current user
protected  String getUsernameForRequest(javax.portlet.PortletRequest request)
          Determine the username for the given request.
protected  void initFrameworkPortlet()
          This method will be invoked after any bean properties have been set and the ApplicationContext has been loaded.
protected  ApplicationContext initPortletApplicationContext()
          Initialize and publish the Portlet ApplicationContext for this portlet.
protected  void initPortletBean()
          Overridden method of GenericPortletBean, invoked after any bean properties have been set.
 boolean isPublishContext()
          Return whether to publish this portlet's context as a PortletContext attribute.
 boolean isPublishEvents()
          Return whether this portlet should publish a PortletRequestHandledEvent at the end of each request.
 void onApplicationEvent(ApplicationEvent event)
          ApplicationListener endpoint that receives events from this servlet's WebApplicationContext.
protected  void onRefresh(ApplicationContext context)
          Template method which can be overridden to add portlet-specific refresh work.
protected  void postProcessPortletApplicationContext(ConfigurableApplicationContext pac)
          Post-process the given Portlet ApplicationContext before it is refreshed and activated as context for this portlet.
 void processAction(javax.portlet.ActionRequest request, javax.portlet.ActionResponse response)
          Delegate action requests to processRequest/doActionService.
protected  void processRequest(javax.portlet.PortletRequest request, javax.portlet.PortletResponse response)
          Process this request, publishing an event regardless of the outcome.
 void refresh()
          Refresh this portlet's application context, as well as the dependent state of the portlet.
 void setContextClass(Class contextClass)
          Set a custom context class.
 void setContextConfigLocation(String contextConfigLocation)
          Set the context config location explicitly, instead of relying on the default location built from the namespace.
 void setNamespace(String namespace)
          Set a custom namespace for this portlet, to be used for building a default context config location.
 void setPublishContext(boolean publishContext)
          Set whether to publish this portlet's context as a PortletContext attribute, available to all objects in the web container.
 void setPublishEvents(boolean publishEvents)
          Set whether this portlet should publish a PortletRequestHandledEvent at the end of each request.
 void setUserinfoUsernameAttributes(String[] userinfoUsernameAttributes)
          Set the list of attributes to search in the USER_INFO map when trying to find the username of the current user.
 
Methods inherited from class org.springframework.web.portlet.GenericPortletBean
addRequiredProperty, getPortletContext, getPortletName, init, initBeanWrapper
 
Methods inherited from class javax.portlet.GenericPortlet
doEdit, doHelp, doView, getInitParameter, getInitParameterNames, getPortletConfig, getResourceBundle, init, render
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONTEXT_CLASS

public static final Class DEFAULT_CONTEXT_CLASS
Default context class for FrameworkPortlet.

See Also:
XmlPortletApplicationContext

DEFAULT_NAMESPACE_SUFFIX

public static final String DEFAULT_NAMESPACE_SUFFIX
Suffix for Portlet ApplicationContext namespaces. If a portlet of this class is given the name "test" in a context, the namespace used by the portlet will resolve to "test-portlet".

See Also:
Constant Field Values

PORTLET_CONTEXT_PREFIX

public static final String PORTLET_CONTEXT_PREFIX
Prefix for the PortletContext attribute for the Portlet ApplicationContext. The completion is the portlet name.


DEFAULT_USERINFO_ATTRIBUTE_NAMES

public static final String[] DEFAULT_USERINFO_ATTRIBUTE_NAMES
Default USER_INFO attribute names to search for the current username: "user.login.id", "user.name".

Constructor Detail

FrameworkPortlet

public FrameworkPortlet()
Method Detail

setContextClass

public void setContextClass(Class contextClass)
Set a custom context class. This class must be of type ApplicationContext; when using the default FrameworkPortlet implementation, the context class must also implement ConfigurablePortletApplicationContext.

See Also:
createPortletApplicationContext(org.springframework.context.ApplicationContext)

getContextClass

public Class getContextClass()
Return the custom context class.


setNamespace

public void setNamespace(String namespace)
Set a custom namespace for this portlet, to be used for building a default context config location.


getNamespace

public String getNamespace()
Return the namespace for this portlet, falling back to default scheme if no custom namespace was set. (e.g. "test-portlet" for a portlet named "test")


setContextConfigLocation

public void setContextConfigLocation(String contextConfigLocation)
Set the context config location explicitly, instead of relying on the default location built from the namespace. This location string can consist of multiple locations separated by any number of commas and spaces.


getContextConfigLocation

public String getContextConfigLocation()
Return the explicit context config location, if any.


setPublishContext

public void setPublishContext(boolean publishContext)
Set whether to publish this portlet's context as a PortletContext attribute, available to all objects in the web container. Default is true.

This is especially handy during testing, although it is debatable whether it's good practice to let other application objects access the context this way.


isPublishContext

public boolean isPublishContext()
Return whether to publish this portlet's context as a PortletContext attribute.


setPublishEvents

public void setPublishEvents(boolean publishEvents)
Set whether this portlet should publish a PortletRequestHandledEvent at the end of each request. Default is true; can be turned off for a slight performance improvement, provided that no ApplicationListeners rely on such events.

See Also:
PortletRequestHandledEvent

isPublishEvents

public boolean isPublishEvents()
Return whether this portlet should publish a PortletRequestHandledEvent at the end of each request.


setUserinfoUsernameAttributes

public void setUserinfoUsernameAttributes(String[] userinfoUsernameAttributes)
Set the list of attributes to search in the USER_INFO map when trying to find the username of the current user.

See Also:
getUsernameForRequest(javax.portlet.PortletRequest)

getUserinfoUsernameAttributes

public String[] getUserinfoUsernameAttributes()
Returns the list of attributes that will be searched in the USER_INFO map when trying to find the username of the current user

See Also:
getUsernameForRequest(javax.portlet.PortletRequest)

initPortletBean

protected final void initPortletBean()
                              throws javax.portlet.PortletException,
                                     BeansException
Overridden method of GenericPortletBean, invoked after any bean properties have been set. Creates this portlet's ApplicationContext.

Overrides:
initPortletBean in class GenericPortletBean
Throws:
javax.portlet.PortletException - if subclass initialization fails
BeansException

initPortletApplicationContext

protected ApplicationContext initPortletApplicationContext()
                                                    throws BeansException
Initialize and publish the Portlet ApplicationContext for this portlet.

Delegates to createPortletApplicationContext(org.springframework.context.ApplicationContext) for actual creation. Can be overridden in subclasses.

Returns:
the ApplicationContext for this portlet
Throws:
BeansException - if the context couldn't be initialized

createPortletApplicationContext

protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
                                                      throws BeansException
Instantiate the Portlet ApplicationContext for this portlet, either a default XmlPortletApplicationContext or a custom context class if set.

This implementation expects custom contexts to implement ConfigurablePortletApplicationContext. Can be overridden in subclasses.

Parameters:
parent - the parent ApplicationContext to use, or null if none
Returns:
the Portlet ApplicationContext for this portlet
Throws:
BeansException - if the context couldn't be initialized
See Also:
setContextClass(java.lang.Class), XmlPortletApplicationContext

postProcessPortletApplicationContext

protected void postProcessPortletApplicationContext(ConfigurableApplicationContext pac)
Post-process the given Portlet ApplicationContext before it is refreshed and activated as context for this portlet.

The default implementation is empty. refresh() will be called automatically after this method returns.

Parameters:
pac - the configured Portlet ApplicationContext (not refreshed yet)
See Also:
createPortletApplicationContext(org.springframework.context.ApplicationContext), ConfigurableApplicationContext.refresh()

getPortletContextAttributeName

public String getPortletContextAttributeName()
Return the PortletContext attribute name for this portlets's ApplicationContext.

The default implementation returns PORTLET_CONTEXT_PREFIX + portlet name.

See Also:
PORTLET_CONTEXT_PREFIX, GenericPortletBean.getPortletName()

getPortletApplicationContext

public final ApplicationContext getPortletApplicationContext()
Return this portlet's ApplicationContext.


initFrameworkPortlet

protected void initFrameworkPortlet()
                             throws javax.portlet.PortletException,
                                    BeansException
This method will be invoked after any bean properties have been set and the ApplicationContext has been loaded.

The default implementation is empty; subclasses may override this method to perform any initialization they require.

Throws:
javax.portlet.PortletException - in case of an initialization exception
BeansException - if thrown by ApplicationContext methods

refresh

public void refresh()
             throws BeansException
Refresh this portlet's application context, as well as the dependent state of the portlet.

Throws:
BeansException - in case of errors
See Also:
getPortletApplicationContext(), ConfigurableApplicationContext.refresh()

onApplicationEvent

public void onApplicationEvent(ApplicationEvent event)
ApplicationListener endpoint that receives events from this servlet's WebApplicationContext.

The default implementation calls onRefresh(org.springframework.context.ApplicationContext) in case of a ContextRefreshedEvent, triggering a refresh of this servlet's context-dependent state.

Specified by:
onApplicationEvent in interface ApplicationListener
Parameters:
event - the incoming ApplicationContext event

onRefresh

protected void onRefresh(ApplicationContext context)
                  throws BeansException
Template method which can be overridden to add portlet-specific refresh work. Called after successful context refresh.

This implementation is empty.

Parameters:
context - the current Portlet ApplicationContext
Throws:
BeansException - in case of errors
See Also:
refresh()

getTitle

protected String getTitle(javax.portlet.RenderRequest renderRequest)
Overridden for friendlier behavior in unit tests.

Overrides:
getTitle in class javax.portlet.GenericPortlet

doDispatch

protected final void doDispatch(javax.portlet.RenderRequest request,
                                javax.portlet.RenderResponse response)
                         throws javax.portlet.PortletException,
                                IOException
Delegate render requests to processRequest/doRenderService.

Overrides:
doDispatch in class javax.portlet.GenericPortlet
Throws:
javax.portlet.PortletException
IOException

processAction

public final void processAction(javax.portlet.ActionRequest request,
                                javax.portlet.ActionResponse response)
                         throws javax.portlet.PortletException,
                                IOException
Delegate action requests to processRequest/doActionService.

Specified by:
processAction in interface javax.portlet.Portlet
Overrides:
processAction in class javax.portlet.GenericPortlet
Throws:
javax.portlet.PortletException
IOException

processRequest

protected final void processRequest(javax.portlet.PortletRequest request,
                                    javax.portlet.PortletResponse response)
                             throws javax.portlet.PortletException,
                                    IOException
Process this request, publishing an event regardless of the outcome. The actual event handling is performed by the abstract doActionService() and doRenderService() template methods.

Throws:
javax.portlet.PortletException
IOException
See Also:
doActionService(javax.portlet.ActionRequest, javax.portlet.ActionResponse), doRenderService(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

getUsernameForRequest

protected String getUsernameForRequest(javax.portlet.PortletRequest request)
Determine the username for the given request.

The default implementation first tries the UserPrincipal. If that does not exist, then it checks the USER_INFO map. Can be overridden in subclasses.

Parameters:
request - current portlet request
Returns:
the username, or null if none found
See Also:
PortletRequest.getUserPrincipal(), PortletRequest.getRemoteUser(), PortletRequest.USER_INFO, setUserinfoUsernameAttributes(java.lang.String[])

doRenderService

protected abstract void doRenderService(javax.portlet.RenderRequest request,
                                        javax.portlet.RenderResponse response)
                                 throws Exception
Subclasses must implement this method to do the work of render request handling.

The contract is essentially the same as that for the doDispatch method of GenericPortlet.

This class intercepts calls to ensure that exception handling and event publication takes place.

Parameters:
request - current render request
response - current render response
Throws:
Exception - in case of any kind of processing failure
See Also:
GenericPortlet.doDispatch(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

doActionService

protected abstract void doActionService(javax.portlet.ActionRequest request,
                                        javax.portlet.ActionResponse response)
                                 throws Exception
Subclasses must implement this method to do the work of action request handling.

The contract is essentially the same as that for the processAction method of GenericPortlet.

This class intercepts calls to ensure that exception handling and event publication takes place.

Parameters:
request - current action request
response - current action response
Throws:
Exception - in case of any kind of processing failure
See Also:
GenericPortlet.processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)

destroy

public void destroy()
Close the ApplicationContext of this portlet.

Specified by:
destroy in interface javax.portlet.Portlet
Overrides:
destroy in class javax.portlet.GenericPortlet
See Also:
ConfigurableApplicationContext.close()

The Spring Framework

Copyright © 2002-2008 The Spring Framework.