001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.tags;
018:
019: import javax.servlet.jsp.JspException;
020: import javax.servlet.jsp.JspTagException;
021: import javax.servlet.jsp.tagext.TagSupport;
022: import javax.servlet.jsp.tagext.TryCatchFinally;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.springframework.web.servlet.support.JspAwareRequestContext;
028: import org.springframework.web.servlet.support.RequestContext;
029:
030: /**
031: * Superclass for all tags that require a {@link RequestContext}.
032: *
033: * <p>The <code>RequestContext</code> instance provides easy access
034: * to current state like the
035: * {@link org.springframework.web.context.WebApplicationContext},
036: * the {@link java.util.Locale}, the
037: * {@link org.springframework.ui.context.Theme}, etc.
038: *
039: * <p>Mainly intended for
040: * {@link org.springframework.web.servlet.DispatcherServlet} requests;
041: * will use fallbacks when used outside <code>DispatcherServlet</code>.
042: *
043: * @author Rod Johnson
044: * @author Juergen Hoeller
045: * @see org.springframework.web.servlet.support.RequestContext
046: * @see org.springframework.web.servlet.DispatcherServlet
047: */
048: public abstract class RequestContextAwareTag extends TagSupport
049: implements TryCatchFinally {
050:
051: /** {@link javax.servlet.jsp.PageContext} attribute for page-level
052: * {@link RequestContext} instance.
053: * */
054: public static final String REQUEST_CONTEXT_PAGE_ATTRIBUTE = "org.springframework.web.servlet.tags.REQUEST_CONTEXT";
055:
056: /** Logger available to subclasses */
057: protected final Log logger = LogFactory.getLog(getClass());
058:
059: private RequestContext requestContext;
060:
061: /**
062: * Create and expose the current RequestContext.
063: * Delegates to {@link #doStartTagInternal()} for actual work.
064: * @see #REQUEST_CONTEXT_PAGE_ATTRIBUTE
065: * @see org.springframework.web.servlet.support.JspAwareRequestContext
066: */
067: public final int doStartTag() throws JspException {
068: this .requestContext = (RequestContext) this .pageContext
069: .getAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE);
070: try {
071: if (this .requestContext == null) {
072: this .requestContext = new JspAwareRequestContext(
073: this .pageContext);
074: this .pageContext.setAttribute(
075: REQUEST_CONTEXT_PAGE_ATTRIBUTE,
076: this .requestContext);
077: }
078: return doStartTagInternal();
079: } catch (JspException ex) {
080: logger.error(ex.getMessage(), ex);
081: throw ex;
082: } catch (RuntimeException ex) {
083: logger.error(ex.getMessage(), ex);
084: throw ex;
085: } catch (Exception ex) {
086: logger.error(ex.getMessage(), ex);
087: throw new JspTagException(ex.getMessage());
088: }
089: }
090:
091: /**
092: * Return the current RequestContext.
093: */
094: protected final RequestContext getRequestContext() {
095: return this .requestContext;
096: }
097:
098: /**
099: * Called by doStartTag to perform the actual work.
100: * @return same as TagSupport.doStartTag
101: * @throws Exception any exception, any checked one other than
102: * a JspException gets wrapped in a JspException by doStartTag
103: * @see javax.servlet.jsp.tagext.TagSupport#doStartTag
104: */
105: protected abstract int doStartTagInternal() throws Exception;
106:
107: public void doCatch(Throwable throwable) throws Throwable {
108: throw throwable;
109: }
110:
111: public void doFinally() {
112: this.requestContext = null;
113: }
114:
115: }
|