📄 frameworkportlet.java
字号:
onRefresh(pac);
}
if (isPublishContext()) {
// publish the context as a portlet context attribute
String attName = getPortletContextAttributeName();
getPortletContext().setAttribute(attName, pac);
if (logger.isDebugEnabled()) {
logger.debug("Published ApplicationContext of portlet '" + getPortletName() +
"' as PortletContext attribute with name [" + attName + "]");
}
}
return pac;
}
/**
* Instantiate the Portlet ApplicationContext for this portlet, either a default
* XmlPortletApplicationContext or a custom context class if set.
* <p>This implementation expects custom contexts to implement
* ConfigurablePortletApplicationContext. Can be overridden in subclasses.
* @param parent the parent ApplicationContext to use, or null if none
* @return the Portlet ApplicationContext for this portlet
* @throws BeansException if the context couldn't be initialized
* @see #setContextClass
* @see org.springframework.web.portlet.context.XmlPortletApplicationContext
*/
protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Portlet with name '" + getPortletName() +
"' will try to create custom ApplicationContext context of class '" +
getContextClass().getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurablePortletApplicationContext.class.isAssignableFrom(getContextClass())) {
throw new ApplicationContextException("Fatal initialization error in portlet with name '" + getPortletName() +
"': custom ApplicationContext class [" + getContextClass().getName() +
"] is not of type ConfigurablePortletApplicationContext");
}
ConfigurablePortletApplicationContext pac =
(ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(getContextClass());
pac.setParent(parent);
pac.setPortletContext(getPortletContext());
pac.setPortletConfig(getPortletConfig());
pac.setNamespace(getNamespace());
if (getContextConfigLocation() != null) {
pac.setConfigLocations(StringUtils.tokenizeToStringArray(getContextConfigLocation(),
ConfigurablePortletApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
pac.addApplicationListener(new SourceFilteringListener(pac, this));
postProcessPortletApplicationContext(pac);
pac.refresh();
return pac;
}
/**
* Post-process the given Portlet ApplicationContext before it is refreshed
* and activated as context for this portlet.
* <p>The default implementation is empty. <code>refresh()</code> will
* be called automatically after this method returns.
* @param pac the configured Portlet ApplicationContext (not refreshed yet)
* @see #createPortletApplicationContext
* @see ConfigurableApplicationContext#refresh()
*/
protected void postProcessPortletApplicationContext(ConfigurableApplicationContext pac) {
}
/**
* Return the PortletContext attribute name for this portlets's ApplicationContext.
* <p>The default implementation returns PORTLET_CONTEXT_PREFIX + portlet name.
* @see #PORTLET_CONTEXT_PREFIX
* @see #getPortletName
*/
public String getPortletContextAttributeName() {
return PORTLET_CONTEXT_PREFIX + getPortletName();
}
/**
* Return this portlet's ApplicationContext.
*/
public final ApplicationContext getPortletApplicationContext() {
return this.portletApplicationContext;
}
/**
* This method will be invoked after any bean properties have been set and
* the ApplicationContext has been loaded.
* <p>The default implementation is empty; subclasses may override this method
* to perform any initialization they require.
* @throws PortletException in case of an initialization exception
* @throws BeansException if thrown by ApplicationContext methods
*/
protected void initFrameworkPortlet() throws PortletException, BeansException {
}
/**
* Refresh this portlet's application context, as well as the
* dependent state of the portlet.
* @throws BeansException in case of errors
* @see #getPortletApplicationContext()
* @see org.springframework.context.ConfigurableApplicationContext#refresh()
*/
public void refresh() throws BeansException {
ApplicationContext pac = getPortletApplicationContext();
if (!(pac instanceof ConfigurableApplicationContext)) {
throw new IllegalStateException("Portlet ApplicationContext does not support refresh: " + pac);
}
((ConfigurableApplicationContext) pac).refresh();
}
/**
* ApplicationListener endpoint that receives events from this servlet's
* WebApplicationContext.
* <p>The default implementation calls {@link #onRefresh} in case of a
* {@link org.springframework.context.event.ContextRefreshedEvent},
* triggering a refresh of this servlet's context-dependent state.
* @param event the incoming ApplicationContext event
*/
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
this.refreshEventReceived = true;
onRefresh(((ContextRefreshedEvent) event).getApplicationContext());
}
}
/**
* Template method which can be overridden to add portlet-specific refresh work.
* Called after successful context refresh.
* <p>This implementation is empty.
* @param context the current Portlet ApplicationContext
* @throws BeansException in case of errors
* @see #refresh()
*/
protected void onRefresh(ApplicationContext context) throws BeansException {
// For subclasses: do nothing by default.
}
/**
* Delegate render requests to processRequest/doRenderService.
*/
protected final void doDispatch(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
processRequest(request, response);
}
/**
* Delegate action requests to processRequest/doActionService.
*/
public final void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
processRequest(request, response);
}
/**
* Process this request, publishing an event regardless of the outcome.
* The actual event handling is performed by the abstract
* <code>doActionService()</code> and <code>doRenderService()</code> template methods.
* @see #doActionService
* @see #doRenderService
*/
protected final void processRequest(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
try {
if (request instanceof ActionRequest) {
doActionService((ActionRequest) request, (ActionResponse) response);
}
else {
doRenderService((RenderRequest) request, (RenderResponse) response);
}
}
catch (PortletException ex) {
failureCause = ex;
throw ex;
}
catch (IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new PortletException("Request processing failed", ex);
}
finally {
if (failureCause != null) {
logger.error("Could not complete request", failureCause);
}
else {
logger.debug("Successfully completed request");
}
if (isPublishEvents()) {
// Whether or not we succeeded, publish an event.
long processingTime = System.currentTimeMillis() - startTime;
this.portletApplicationContext.publishEvent(
new PortletRequestHandledEvent(this,
getPortletConfig().getPortletName(), request.getPortletMode().toString(),
(request instanceof ActionRequest ? "action" : "render"),
request.getRequestedSessionId(), getUsernameForRequest(request),
processingTime, failureCause));
}
}
}
/**
* Determine the username for the given request.
* <p>The default implementation first tries the UserPrincipal.
* If that does not exist, then it checks the USER_INFO map.
* Can be overridden in subclasses.
* @param request current portlet request
* @return the username, or <code>null</code> if none found
* @see javax.portlet.PortletRequest#getUserPrincipal()
* @see javax.portlet.PortletRequest#getRemoteUser()
* @see javax.portlet.PortletRequest#USER_INFO
* @see #setUserinfoUsernameAttributes
*/
protected String getUsernameForRequest(PortletRequest request) {
// Try the principal.
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null) {
return userPrincipal.getName();
}
// Try the remote user name.
String userName = request.getRemoteUser();
if (userName != null) {
return userName;
}
// Try the Portlet USER_INFO map.
Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
if (userInfo != null) {
for (int i = 0, n = this.userinfoUsernameAttributes.length; i < n; i++) {
userName = (String) userInfo.get(this.userinfoUsernameAttributes[i]);
if (userName != null) {
return userName;
}
}
}
// Nothing worked...
return null;
}
/**
* Subclasses must implement this method to do the work of render request handling.
* <p>The contract is essentially the same as that for the <code>doDispatch</code>
* method of GenericPortlet.
* <p>This class intercepts calls to ensure that exception handling and
* event publication takes place.
* @param request current render request
* @param response current render response
* @throws Exception in case of any kind of processing failure
* @see javax.portlet.GenericPortlet#doDispatch
*/
protected abstract void doRenderService(RenderRequest request, RenderResponse response)
throws Exception;
/**
* Subclasses must implement this method to do the work of action request handling.
* <p>The contract is essentially the same as that for the <code>processAction</code>
* method of GenericPortlet.
* <p>This class intercepts calls to ensure that exception handling and
* event publication takes place.
* @param request current action request
* @param response current action response
* @throws Exception in case of any kind of processing failure
* @see javax.portlet.GenericPortlet#processAction
*/
protected abstract void doActionService(ActionRequest request, ActionResponse response)
throws Exception;
/**
* Close the ApplicationContext of this portlet.
* @see org.springframework.context.ConfigurableApplicationContext#close()
*/
public void destroy() {
getPortletContext().log("Destroying Spring FrameworkPortlet '" + getPortletName() + "'");
if (this.portletApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.portletApplicationContext).close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -