01: /*
02: * Created on Nov 23, 2004
03: */
04: package uk.org.ponder.errorutil;
05:
06: import uk.org.ponder.hashutil.EighteenIDGenerator;
07: import uk.org.ponder.hashutil.IDGenerator;
08: import uk.org.ponder.messageutil.TargettedMessage;
09: import uk.org.ponder.util.Logger;
10:
11: /**
12: * @author Antranig Basman (antranig@caret.cam.ac.uk)
13: *
14: */
15: public class ThreadErrorState {
16: /** An ID generator used to assign ids to error messages, for easy
17: * location in logs.
18: */
19: public static IDGenerator idgenerator = new EighteenIDGenerator();
20:
21: private static ThreadLocal errormap = new ThreadLocal() {
22: public Object initialValue() {
23: Logger.log.warn("ThreadErrorState allocated for thread "
24: + Thread.currentThread());
25: return new ErrorStateEntry();
26: }
27: };
28:
29: public static ErrorStateEntry getErrorState() {
30: return (ErrorStateEntry) errormap.get();
31: }
32:
33: /** Determines whether any errors have occured during the processing of
34: * the current request.
35: */
36: public static boolean isError() {
37: return getErrorState().messages.isError();
38: }
39:
40: // Idea is that during POST processing, this will be full of messages
41: // keyed by field path, but during GET processing, keyed by component ID.
42: public static void addMessage(TargettedMessage message) {
43: ErrorStateEntry ese = getErrorState();
44: if (ese.errorid == null) {
45: ese.errorid = idgenerator.generateID();
46: }
47: getErrorState().messages.addMessage(message);
48: }
49:
50: public static void beginRequest() {
51: errormap.set(new ErrorStateEntry());
52: }
53:
54: public static void endRequest() {
55: errormap.set(null);
56: }
57: }
|