KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > recorder > ListRecorder


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/recorder/ListRecorder.java#4 $
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) 2005-2006 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.recorder;
11
12 import org.apache.log4j.Logger;
13
14 import java.util.*;
15
16 /**
17  * Implementation of {@link MessageRecorder} that records each message
18  * in a {@link List}. The calling code can then access the list and take
19  * actions as needed.
20  */

21 public class ListRecorder extends AbstractRecorder {
22
23     private final List<Entry> errorList;
24     private final List<Entry> warnList;
25     private final List<Entry> infoList;
26
27     public ListRecorder() {
28         errorList = new ArrayList<Entry>();
29         warnList = new ArrayList<Entry>();
30         infoList = new ArrayList<Entry>();
31     }
32
33     public void clear() {
34         super.clear();
35         errorList.clear();
36         warnList.clear();
37         infoList.clear();
38     }
39
40     public Iterator<Entry> getErrorEntries() {
41         return errorList.iterator();
42     }
43
44     public Iterator<Entry> getWarnEntries() {
45         return warnList.iterator();
46     }
47
48     public Iterator<Entry> getInfoEntries() {
49         return infoList.iterator();
50     }
51
52     protected void recordMessage(final String JavaDoc msg,
53                                  final Object JavaDoc info,
54                                  final MsgType msgType) {
55         String JavaDoc context = getContext();
56
57         Entry e = new Entry(context, msg, msgType, info);
58         switch (msgType) {
59         case INFO:
60             infoList.add(e);
61             break;
62         case WARN:
63             warnList.add(e);
64             break;
65         case ERROR:
66             errorList.add(e);
67             break;
68         default :
69             e = new Entry(
70                 context,
71                 "Unknown message type enum \"" +
72                 msgType +
73                 "\" for message: " + msg,
74                 MsgType.WARN,
75                 info);
76             warnList.add(e);
77         }
78     }
79
80     public void logInfoMessage(final Logger logger) {
81         if (hasInformation()) {
82             logMessage(getInfoEntries(), logger);
83         }
84     }
85
86     public void logWarningMessage(final Logger logger) {
87         if (hasWarnings()) {
88             logMessage(getWarnEntries(), logger);
89         }
90     }
91
92     public void logErrorMessage(final Logger logger) {
93         if (hasErrors()) {
94             logMessage(getErrorEntries(), logger);
95         }
96     }
97
98     static void logMessage(Iterator<Entry> it, Logger logger) {
99         while (it.hasNext()) {
100             Entry e = it.next();
101             logMessage(e, logger);
102         }
103     }
104
105     static void logMessage(
106             final Entry e,
107             final Logger logger) {
108         logMessage(e.getContext(), e.getMessage(), e.getMsgType(), logger);
109     }
110
111     /**
112      * Entry is a Info, Warning or Error message. This is the object stored
113      * in the Lists MessageRecorder's info, warning and error message lists.
114      */

115     public static class Entry {
116         private final String JavaDoc context;
117         private final String JavaDoc msg;
118         private final MsgType msgType;
119         private final Object JavaDoc info;
120
121         private Entry(final String JavaDoc context,
122                       final String JavaDoc msg,
123                       final MsgType msgType,
124                       final Object JavaDoc info) {
125             this.context = context;
126             this.msg = msg;
127             this.msgType = msgType;
128             this.info = info;
129         }
130         public String JavaDoc getContext() {
131             return context;
132         }
133         public String JavaDoc getMessage() {
134             return msg;
135         }
136         public MsgType getMsgType() {
137             return msgType;
138         }
139         public Object JavaDoc getInfo() {
140             return info;
141         }
142     }
143 }
144
145 // End ListRecorder.java
146
Popular Tags