01: /*
02: * Created on 16-Jun-2004
03: */
04: package uk.org.ponder.streamutil;
05:
06: import java.io.PrintWriter;
07: import java.io.StringWriter;
08:
09: import uk.org.ponder.util.UniversalRuntimeException;
10:
11: /**
12: * @author Bosmon
13: *
14: * The class
15: */
16: public class ErrorPrintWriter extends PrintWriter {
17:
18: private static ThreadLocal threadwriter = new ThreadLocal();
19:
20: public static void setWriter(PrintWriter pw) {
21: threadwriter.set(pw);
22: }
23:
24: public static PrintWriter getWriter() {
25: return (PrintWriter) threadwriter.get();
26: }
27:
28: StringWriter sw = new StringWriter();
29:
30: public ErrorPrintWriter() {
31: super (new StringWriter());
32: sw = (StringWriter) out; // recover our argument! Since this is mysteriously
33: setWriter(this );
34: // forbidden by super rules.
35: }
36:
37: public void dispose(String err) {
38: setWriter(null);
39: close();
40: String errorstring = sw.toString();
41: if (errorstring.length() > 0) {
42: throw new UniversalRuntimeException(err + ": "
43: + errorstring);
44: }
45: }
46: }
|