KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > recorder > AbstractRecorder


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/recorder/AbstractRecorder.java#7 $
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
11 package mondrian.recorder;
12
13 import mondrian.resource.MondrianResource;
14
15 import java.util.*;
16
17 /**
18  * Abstract implemention of the {@link MessageRecorder} interface.
19  *
20  * @author Richard M. Emberson
21  * @version $Id: //open/mondrian/src/main/mondrian/recorder/AbstractRecorder.java#7 $
22  */

23 public abstract class AbstractRecorder implements MessageRecorder {
24
25     /**
26      * Helper method to format a message and write to logger.
27      */

28     public static void logMessage(
29             final String JavaDoc context,
30             final String JavaDoc msg,
31             final MsgType msgType,
32             final org.apache.log4j.Logger logger) {
33         StringBuilder JavaDoc buf = new StringBuilder JavaDoc(64);
34         buf.append(context);
35         buf.append(": ");
36         buf.append(msg);
37
38         switch (msgType) {
39         case INFO:
40             logger.info(buf.toString());
41             break;
42         case WARN:
43             logger.warn(buf.toString());
44             break;
45         case ERROR:
46             logger.error(buf.toString());
47             break;
48         default:
49             logger.warn(
50                     "Unknown message type enum \"" +
51                     msgType +
52                     "\" for message: " +
53                     buf.toString()
54             );
55         }
56     }
57
58     enum MsgType {
59         INFO,
60         WARN,
61         ERROR
62     }
63
64     public static final int DEFAULT_MSG_LIMIT = 10;
65
66     private final int errorMsgLimit;
67     private final List<String JavaDoc> contexts;
68     private int errorMsgCount;
69     private int warningMsgCount;
70     private int infoMsgCount;
71     private String JavaDoc contextMsgCache;
72     private long startTime;
73
74     protected AbstractRecorder() {
75         this(DEFAULT_MSG_LIMIT);
76     }
77     protected AbstractRecorder(final int errorMsgLimit) {
78         this.errorMsgLimit = errorMsgLimit;
79         this.contexts = new ArrayList<String JavaDoc>();
80         this.startTime = System.currentTimeMillis();
81     }
82
83     /**
84      * Resets this MessageRecorder.
85      */

86     public void clear() {
87         errorMsgCount = 0;
88         warningMsgCount = 0;
89         infoMsgCount = 0;
90         contextMsgCache = null;
91         contexts.clear();
92         this.startTime = System.currentTimeMillis();
93     }
94
95     public long getStartTimeMillis() {
96         return this.startTime;
97     }
98
99     public long getRunTimeMillis() {
100         return (System.currentTimeMillis() - this.startTime);
101     }
102
103     public boolean hasInformation() {
104         return (infoMsgCount > 0);
105     }
106     public boolean hasWarnings() {
107         return (warningMsgCount > 0);
108     }
109     public boolean hasErrors() {
110         return (errorMsgCount > 0);
111     }
112     public int getInfoCount() {
113         return infoMsgCount;
114     }
115     public int getWarningCount() {
116         return warningMsgCount;
117     }
118     public int getErrorCount() {
119         return errorMsgCount;
120     }
121
122     public String JavaDoc getContext() {
123         // heavy weight
124
if (contextMsgCache == null) {
125             final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
126             int k = 0;
127             for (String JavaDoc name : contexts) {
128                 if (k++ > 0) {
129                     buf.append(':');
130                 }
131                 buf.append(name);
132             }
133             contextMsgCache = buf.toString();
134         }
135         return contextMsgCache;
136     }
137
138     public void pushContextName(final String JavaDoc name) {
139         // light weight
140
contexts.add(name);
141         contextMsgCache = null;
142     }
143
144     public void popContextName() {
145         // light weight
146
contexts.remove(contexts.size()-1);
147         contextMsgCache = null;
148     }
149
150     public void throwRTException() throws RecorderException {
151         if (hasErrors()) {
152             final String JavaDoc errorMsg =
153                 MondrianResource.instance().ForceMessageRecorderError.str(
154                     getContext(),
155                     errorMsgCount);
156             throw new RecorderException(errorMsg);
157         }
158     }
159
160     public void reportError(final Exception JavaDoc ex)
161             throws RecorderException {
162         reportError(ex, null);
163     }
164
165     public void reportError(final Exception JavaDoc ex, final Object JavaDoc info)
166             throws RecorderException {
167         reportError(ex.toString(), info);
168     }
169
170     public void reportError(final String JavaDoc msg)
171             throws RecorderException {
172         reportError(msg, null);
173     }
174     public void reportError(final String JavaDoc msg, final Object JavaDoc info)
175             throws RecorderException {
176         errorMsgCount++;
177         recordMessage(msg, info, MsgType.ERROR);
178
179         if (errorMsgCount >= errorMsgLimit) {
180             final String JavaDoc errorMsg =
181                 MondrianResource.instance().TooManyMessageRecorderErrors.str(
182                     getContext(),
183                     errorMsgCount);
184             throw new RecorderException(errorMsg);
185         }
186     }
187
188     public void reportWarning(final String JavaDoc msg) {
189         reportWarning(msg, null);
190     }
191     public void reportWarning(final String JavaDoc msg, final Object JavaDoc info) {
192         warningMsgCount++;
193         recordMessage(msg, info, MsgType.WARN);
194     }
195
196     public void reportInfo(final String JavaDoc msg) {
197         reportInfo(msg, null);
198     }
199     public void reportInfo(final String JavaDoc msg, final Object JavaDoc info) {
200         infoMsgCount++;
201         recordMessage(msg, info, MsgType.INFO);
202     }
203
204     /**
205      * Handles a message.
206      * Classes implementing this abstract class must provide an implemention
207      * of this method; it receives all warning/error messages.
208      *
209      * @param msg the error or warning message.
210      * @param info the information Object which might be null.
211      * @param msgType one of the message type enum values
212      */

213     protected abstract void recordMessage(
214             String JavaDoc msg,
215             Object JavaDoc info,
216             MsgType msgType);
217 }
218
219 // End AbstractRecorder.java
220
Popular Tags