org.frameworkset.web.servlet.mvc.AbstractController Maven / Gradle / Ivy
Show all versions of bboss-mvc Show documentation
/*
* Copyright 2008 biaoping.yin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.frameworkset.web.servlet.mvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import org.frameworkset.web.servlet.Controller;
import org.frameworkset.web.servlet.HandlerExecutionChain;
import org.frameworkset.web.servlet.ModelAndView;
import org.frameworkset.web.servlet.handler.annotations.ExcludeMethod;
import org.frameworkset.web.servlet.support.WebContentGenerator;
import org.frameworkset.web.util.WebUtils;
/**
* Title: AbstractController.java
* Description:
* bboss workgroup
* Copyright (c) 2008
* @Date 2010-10-6
* @author biaoping.yin
* @version 1.0
*/
public abstract class AbstractController extends WebContentGenerator implements Controller{
private boolean synchronizeOnSession = false;
/**
* Set if controller execution should be synchronized on the session,
* to serialize parallel invocations from the same client.
* More specifically, the execution of the handleRequestInternal
* method will get synchronized if this flag is "true". The best available
* session mutex will be used for the synchronization; ideally, this will
* be a mutex exposed by HttpSessionMutexListener.
*
The session mutex is guaranteed to be the same object during
* the entire lifetime of the session, available under the key defined
* by the SESSION_MUTEX_ATTRIBUTE
constant. It serves as a
* safe reference to synchronize on for locking on the current session.
*
In many cases, the HttpSession reference itself is a safe mutex
* as well, since it will always be the same object reference for the
* same active logical session. However, this is not guaranteed across
* different servlet containers; the only 100% safe way is a session mutex.
*/
public final void setSynchronizeOnSession(boolean synchronizeOnSession) {
this.synchronizeOnSession = synchronizeOnSession;
}
/**
* Return whether controller execution should be synchronized on the session.
*/
public final boolean isSynchronizeOnSession() {
return this.synchronizeOnSession;
}
@ExcludeMethod
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response,PageContext pageContext)
throws Exception {
// Delegate to WebContentGenerator for checking and preparing.
checkAndPrepare(request, response, this instanceof LastModified);
// Execute handleRequestInternal in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
return handleRequestInternal(request, response,pageContext,null);
}
}
}
return handleRequestInternal(request, response,pageContext,null);
}
@ExcludeMethod
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response,PageContext pageContext,HandlerExecutionChain handlerExecutionChain)
throws Exception {
// Delegate to WebContentGenerator for checking and preparing.
checkAndPrepare(request, response, this instanceof LastModified);
// Execute handleRequestInternal in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
return handleRequestInternal(request, response,pageContext,handlerExecutionChain);
}
}
}
return handleRequestInternal(request, response,pageContext,handlerExecutionChain);
}
/**
* Template method. Subclasses must implement this.
* The contract is the same as for handleRequest
.
* @see #handleRequest
*/
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response, PageContext pageContext,HandlerExecutionChain handlerExecutionChain)
throws Exception{
return handleRequestInternal( request, response, pageContext);
}
/**
* Template method. Subclasses must implement this.
* The contract is the same as for handleRequest
.
* @see #handleRequest
*/
protected abstract ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response, PageContext pageContext)
throws Exception;
}