KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > RequestContextAwareTag


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.tags;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.web.servlet.support.JspAwareRequestContext;
28 import org.springframework.web.servlet.support.RequestContext;
29
30 /**
31  * Superclass for all tags that require a {@link RequestContext}.
32  *
33  * <p>The <code>RequestContext</code> instance provides easy access
34  * to current state like the
35  * {@link org.springframework.web.context.WebApplicationContext},
36  * the {@link java.util.Locale}, the
37  * {@link org.springframework.ui.context.Theme}, etc.
38  *
39  * <p>Mainly intended for
40  * {@link org.springframework.web.servlet.DispatcherServlet} requests;
41  * will use fallbacks when used outside <code>DispatcherServlet</code>.
42  *
43  * @author Rod Johnson
44  * @author Juergen Hoeller
45  * @see org.springframework.web.servlet.support.RequestContext
46  * @see org.springframework.web.servlet.DispatcherServlet
47  */

48 public abstract class RequestContextAwareTag extends TagSupport JavaDoc implements TryCatchFinally JavaDoc {
49
50     /** {@link javax.servlet.jsp.PageContext} attribute for page-level
51      * {@link RequestContext} instance.
52      * */

53     public static final String JavaDoc REQUEST_CONTEXT_PAGE_ATTRIBUTE =
54             "org.springframework.web.servlet.tags.REQUEST_CONTEXT";
55
56     
57     /** Logger available to subclasses */
58     protected final Log logger = LogFactory.getLog(getClass());
59
60
61     private RequestContext requestContext;
62
63
64     /**
65      * Create and expose the current RequestContext.
66      * Delegates to {@link #doStartTagInternal()} for actual work.
67      * @see #REQUEST_CONTEXT_PAGE_ATTRIBUTE
68      * @see org.springframework.web.servlet.support.JspAwareRequestContext
69      */

70     public final int doStartTag() throws JspException JavaDoc {
71         this.requestContext = (RequestContext) this.pageContext.getAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE);
72         try {
73             if (this.requestContext == null) {
74                 this.requestContext = new JspAwareRequestContext(this.pageContext);
75                 this.pageContext.setAttribute(REQUEST_CONTEXT_PAGE_ATTRIBUTE, this.requestContext);
76             }
77             return doStartTagInternal();
78         }
79         catch (JspException JavaDoc ex) {
80             logger.error(ex.getMessage(), ex);
81             throw ex;
82         }
83         catch (RuntimeException JavaDoc ex) {
84             logger.error(ex.getMessage(), ex);
85             throw ex;
86         }
87         catch (Exception JavaDoc ex) {
88             logger.error(ex.getMessage(), ex);
89             throw new JspTagException JavaDoc(ex.getMessage());
90         }
91     }
92
93     /**
94      * Return the current RequestContext.
95      */

96     protected final RequestContext getRequestContext() {
97         return this.requestContext;
98     }
99
100     /**
101      * Called by doStartTag to perform the actual work.
102      * @return same as TagSupport.doStartTag
103      * @throws Exception any exception, any checked one other than
104      * a JspException gets wrapped in a JspException by doStartTag
105      * @see javax.servlet.jsp.tagext.TagSupport#doStartTag
106      */

107     protected abstract int doStartTagInternal() throws Exception JavaDoc;
108
109
110     public void doCatch(Throwable JavaDoc throwable) throws Throwable JavaDoc {
111         throw throwable;
112     }
113
114     public void doFinally() {
115         this.requestContext = null;
116     }
117
118 }
119
Popular Tags