| 1 10 11 package mondrian.recorder; 12 13 import mondrian.resource.MondrianResource; 14 15 import java.util.*; 16 17 23 public abstract class AbstractRecorder implements MessageRecorder { 24 25 28 public static void logMessage( 29 final String context, 30 final String msg, 31 final MsgType msgType, 32 final org.apache.log4j.Logger logger) { 33 StringBuilder buf = new StringBuilder (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 > contexts; 68 private int errorMsgCount; 69 private int warningMsgCount; 70 private int infoMsgCount; 71 private String 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 >(); 80 this.startTime = System.currentTimeMillis(); 81 } 82 83 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 getContext() { 123 if (contextMsgCache == null) { 125 final StringBuilder buf = new StringBuilder (); 126 int k = 0; 127 for (String 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 name) { 139 contexts.add(name); 141 contextMsgCache = null; 142 } 143 144 public void popContextName() { 145 contexts.remove(contexts.size()-1); 147 contextMsgCache = null; 148 } 149 150 public void throwRTException() throws RecorderException { 151 if (hasErrors()) { 152 final String errorMsg = 153 MondrianResource.instance().ForceMessageRecorderError.str( 154 getContext(), 155 errorMsgCount); 156 throw new RecorderException(errorMsg); 157 } 158 } 159 160 public void reportError(final Exception ex) 161 throws RecorderException { 162 reportError(ex, null); 163 } 164 165 public void reportError(final Exception ex, final Object info) 166 throws RecorderException { 167 reportError(ex.toString(), info); 168 } 169 170 public void reportError(final String msg) 171 throws RecorderException { 172 reportError(msg, null); 173 } 174 public void reportError(final String msg, final Object info) 175 throws RecorderException { 176 errorMsgCount++; 177 recordMessage(msg, info, MsgType.ERROR); 178 179 if (errorMsgCount >= errorMsgLimit) { 180 final String errorMsg = 181 MondrianResource.instance().TooManyMessageRecorderErrors.str( 182 getContext(), 183 errorMsgCount); 184 throw new RecorderException(errorMsg); 185 } 186 } 187 188 public void reportWarning(final String msg) { 189 reportWarning(msg, null); 190 } 191 public void reportWarning(final String msg, final Object info) { 192 warningMsgCount++; 193 recordMessage(msg, info, MsgType.WARN); 194 } 195 196 public void reportInfo(final String msg) { 197 reportInfo(msg, null); 198 } 199 public void reportInfo(final String msg, final Object info) { 200 infoMsgCount++; 201 recordMessage(msg, info, MsgType.INFO); 202 } 203 204 213 protected abstract void recordMessage( 214 String msg, 215 Object info, 216 MsgType msgType); 217 } 218 219 | Popular Tags |