KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Exceptions


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.pageflow.internal.AdapterManager;
23 import org.apache.beehive.netui.tags.AbstractSimpleTag;
24 import org.apache.beehive.netui.util.HtmlExceptionFormatter;
25 import org.apache.struts.Globals;
26 import org.apache.struts.util.ResponseUtils;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31
32 /**
33  * Renders formatted exception data, as found in the Request with the key:
34  * <code>org.apache.struts.action.Action.EXCEPTION_KEY</code>. Exceptions ignores its
35  * body content.
36  * @jsptagref.tagdescription Renders exception messages and stack traces inline on the JSP page.
37  * @example In this sample, the &lt;netui:exceptions> tag will output the exception title and message,
38  * but not the stacktraces.
39  * <pre>&lt;netui:exceptions showMessage="true" showStackTrace="false" /></pre>
40  * @netui:tag name="exceptions" body-content="empty" description="Displays formatted exception messages."
41  */

42 public class Exceptions extends AbstractSimpleTag
43 {
44     private boolean _showMessage = true;
45     private boolean _showStackTrace = false;
46     private boolean _showDevModeStackTrace = false;
47
48     /**
49      * Return the name of the Tag.
50      */

51     public String JavaDoc getTagName()
52     {
53         return "Exceptions";
54     }
55
56     /**
57      * Set whether or not the exception message is being shown.
58      * @param showMessage true or false depending on the setting desired
59      * @jsptagref.attributedescription Boolean. Determines whether or not the exception message is shown. Defaults to <code>true</code>
60      * @jsptagref.databindable false
61      * @jsptagref.attributesyntaxvalue <i>boolean_showMessage</i>
62      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
63      * description="Determines whether or not the exception message is shown. Defaults to true."
64      */

65     public void setShowMessage(boolean showMessage)
66     {
67         _showMessage = showMessage;
68     }
69
70     /**
71      * Set whether or not the stack trace is being shown.
72      * @param showStackTrace true or false depending on the setting desired
73      * @jsptagref.attributedescription Boolean. Determines whether or not the stack trace is shown. Defaults to <code>false</code>
74      * @jsptagref.databindable false
75      * @jsptagref.attributesyntaxvalue <i>boolean_showStackTrace</i>
76      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
77      * description="Determines whether or not the stack trace is shown. Defaults to false."
78      */

79     public void setShowStackTrace(boolean showStackTrace)
80     {
81         _showStackTrace = showStackTrace;
82     }
83
84     /**
85      * Set whether or not the stack trace is being shown.
86      * @param showStackTrace true or false depending on the setting desired
87      * @jsptagref.attributedescription Boolean. Determine if the stack trace is display only when dev mode
88      * is on. Default is <code>true</code>.
89      * @jsptagref.databindable false
90      * @jsptagref.attributesyntaxvalue <i>boolean_showDevModeStackTrace</i>
91      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
92      * description="Determine if the stack trace is display only when dev mode is on. Default is true."
93      */

94     public void setShowDevModeStackTrace(boolean showStackTrace)
95     {
96         _showDevModeStackTrace = showStackTrace;
97     }
98
99     /**
100      * Render the exception text based on the display attributes.
101      * @throws JspException if a JSP exception has occurred
102      */

103     public void doTag()
104             throws JspException JavaDoc
105     {
106         // First look for the exception in the pageflow/struts request attribute. If it's not there,
107
// look for it in the request attribute the container provides for web.xml-configured error
108
// pages.
109
InternalStringBuilder results = new InternalStringBuilder(128);
110         PageContext JavaDoc pageContext = getPageContext();
111         Throwable JavaDoc e = (Throwable JavaDoc) pageContext.getAttribute(Globals.EXCEPTION_KEY, PageContext.REQUEST_SCOPE);
112
113         if (e == null) {
114             ServletRequest JavaDoc req = pageContext.getRequest();
115             e = (Throwable JavaDoc) req.getAttribute("javax.servlet.error.exception");
116             if (e == null) {
117                 e = (Throwable JavaDoc) req.getAttribute("javax.servlet.jsp.jspException");
118             }
119         }
120
121         if (!_showStackTrace && _showDevModeStackTrace) {
122             boolean devMode = !AdapterManager.getServletContainerAdapter(pageContext.getServletContext()).isInProductionMode();
123             if (devMode)
124                 _showStackTrace = true;
125         }
126
127         if (e != null) {
128             if (_showMessage) {
129                 String JavaDoc msg = e.getMessage();
130                 // if we have message lets output the exception name and the name of the
131
if ((msg != null) && (msg.length() > 0)) {
132                     if (!_showStackTrace)
133                         msg = e.getClass().getName() + ": " + msg;
134                     results.append(HtmlExceptionFormatter.format(msg, e, _showStackTrace));
135                 }
136                 else {
137                     results.append(HtmlExceptionFormatter.format(e.getClass().getName(), e, _showStackTrace));
138                 }
139             }
140             else {
141                 results.append(HtmlExceptionFormatter.format(null, e, _showStackTrace));
142             }
143             ResponseUtils.write(pageContext, results.toString());
144         }
145     }
146 }
147
Popular Tags