KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > statusline > StatusLine


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.statusline;
14
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17
18 import javax.servlet.http.HttpSession JavaDoc;
19
20 import com.tonbeller.tbutils.res.Resources;
21 import com.tonbeller.wcf.controller.RequestContext;
22 import com.tonbeller.wcf.utils.JDK13Utils;
23 import com.tonbeller.wcf.utils.XmlUtils;
24
25 /**
26  * @author av
27  * @since 01.07.2004
28  */

29 public class StatusLine {
30   /** name of session attribute for jstl scripting */
31   public static final String JavaDoc WEBKEY = "wcfStatusLine";
32   private String JavaDoc message;
33   private Exception JavaDoc exception;
34   private String JavaDoc exceptionType;
35
36   private Resources resources;
37
38   private StatusLine() {
39     resources = Resources.instance(StatusLine.class);
40   }
41
42   public static synchronized StatusLine instance() {
43     return instance(RequestContext.instance().getSession());
44   }
45   
46   public static synchronized StatusLine instance(HttpSession JavaDoc session) {
47     StatusLine sl = (StatusLine) session.getAttribute(WEBKEY);
48     if (sl == null) {
49       sl = new StatusLine();
50       session.setAttribute(WEBKEY, sl);
51     }
52     return sl;
53   }
54
55   public void setMessage(String JavaDoc message) {
56     this.message = message;
57     this.exception = null;
58   }
59
60   public void setError(String JavaDoc exceptionType, Exception JavaDoc exception) {
61     this.exceptionType = exceptionType;
62     this.exception = exception;
63   }
64
65   public String JavaDoc getStatusHTML() {
66     if (exception != null)
67       return getErrorHTML();
68     if (message != null)
69       return getMessageHTML();
70     return "";
71   }
72
73   public String JavaDoc getStatusText() {
74     if (exception != null)
75       return getErrorText();
76     if (message != null)
77       return getMessageText();
78     return "";
79   }
80
81   public boolean isEmpty() {
82     return exception == null && message == null;
83   }
84
85   public void clear() {
86     exception = null;
87     message = null;
88   }
89
90   private String JavaDoc getMessageHTML() {
91     String JavaDoc s = XmlUtils.escapeXml(message);
92     s = resources.getString("statusLine.message.format.html", s);
93     return s;
94   }
95
96   private String JavaDoc getMessageText() {
97     return resources.getString("statusLine.message.format.text", message);
98   }
99
100   private String JavaDoc getErrorHTML() {
101     return getErrorFmt("statusLine.error.format.html");
102   }
103
104   private String JavaDoc getErrorText() {
105     return getErrorFmt("statusLine.error.format.text");
106   }
107
108   private String JavaDoc getErrorFmt(String JavaDoc resKey) {
109     Object JavaDoc[] args = new Object JavaDoc[3];
110     Throwable JavaDoc t = unwind(exception);
111     if (exceptionType != null)
112       args[0] = exceptionType;
113     else
114       args[0] = t.getClass().getName();
115     args[1] = XmlUtils.escapeXml(t.getMessage());
116     args[2] = getStackTrace(t);
117     return resources.getString(resKey, args);
118   }
119
120   private String JavaDoc getStackTrace(Throwable JavaDoc t) {
121     StringWriter JavaDoc sw = new StringWriter JavaDoc();
122     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
123     t.printStackTrace(pw);
124     pw.close();
125     return sw.toString();
126   }
127
128   private Throwable JavaDoc unwind(Throwable JavaDoc t) {
129     Throwable JavaDoc x = JDK13Utils.getCause(t);
130     while (x != null) {
131       t = x;
132       x = JDK13Utils.getCause(t);
133     }
134     return t;
135   }
136
137 }
Popular Tags