KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > ErrorBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23
24 /**
25  * Bean used by the error page, holds the last exception to be thrown by the system
26  *
27  * @author gavinc
28  */

29 public class ErrorBean
30 {
31    public static final String JavaDoc ERROR_BEAN_NAME = "alfresco.ErrorBean";
32    
33    private String JavaDoc returnPage;
34    private Throwable JavaDoc lastError;
35
36    /**
37     * @return Returns the page to go back to after the error has been displayed
38     */

39    public String JavaDoc getReturnPage()
40    {
41       return returnPage;
42    }
43
44    /**
45     * @param returnPage The page to return to after showing the error
46     */

47    public void setReturnPage(String JavaDoc returnPage)
48    {
49       this.returnPage = returnPage;
50    }
51
52    /**
53     * @return Returns the lastError.
54     */

55    public Throwable JavaDoc getLastError()
56    {
57       return lastError;
58    }
59
60    /**
61     * @param lastError The lastError to set.
62     */

63    public void setLastError(Throwable JavaDoc lastError)
64    {
65       this.lastError = lastError;
66    }
67    
68    /**
69     * @return Returns the last error to occur in string form
70     */

71    public String JavaDoc getLastErrorMessage()
72    {
73       String JavaDoc message = "No error currently stored";
74       
75       if (this.lastError != null)
76       {
77          StringBuilder JavaDoc builder = null;
78          Throwable JavaDoc cause = null;
79          if (this.lastError instanceof ServletException JavaDoc &&
80              ((ServletException JavaDoc)this.lastError).getRootCause() != null)
81          {
82             // servlet exception puts the actual error in root cause!!
83
Throwable JavaDoc actualError = ((ServletException JavaDoc)this.lastError).getRootCause();
84             builder = new StringBuilder JavaDoc(actualError.toString());
85             cause = actualError.getCause();
86          }
87          else
88          {
89             builder = new StringBuilder JavaDoc(this.lastError.toString());
90             cause = this.lastError.getCause();
91          }
92          
93          while (cause != null)
94          {
95             builder.append("<br/><br/>caused by:<br/>");
96             builder.append(cause.toString());
97             
98             if (cause instanceof ServletException JavaDoc &&
99              ((ServletException JavaDoc)cause).getRootCause() != null)
100             {
101                cause = ((ServletException JavaDoc)cause).getRootCause();
102             }
103             else
104             {
105                cause = cause.getCause();
106             }
107          }
108          
109          message = builder.toString();
110       }
111       
112       return message;
113    }
114    
115    /**
116     * @return Returns the stack trace for the last error
117     */

118    public String JavaDoc getStackTrace()
119    {
120       StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
121       PrintWriter JavaDoc writer = new PrintWriter JavaDoc(stringWriter);
122       
123       if (this.lastError instanceof ServletException JavaDoc &&
124           ((ServletException JavaDoc)this.lastError).getRootCause() != null)
125       {
126          Throwable JavaDoc actualError = ((ServletException JavaDoc)this.lastError).getRootCause();
127          actualError.printStackTrace(writer);
128       }
129       else
130       {
131          this.lastError.printStackTrace(writer);
132       }
133       
134       return stringWriter.toString().replaceAll("\r\n", "<br/>");
135    }
136 }
137
Popular Tags