001/* 002// This software is subject to the terms of the Eclipse Public License v1.0 003// Agreement, available at the following URL: 004// http://www.eclipse.org/legal/epl-v10.html. 005// You must accept the terms of that agreement to use this software. 006// 007// Copyright (C) 2005-2005 Julian Hyde 008// Copyright (C) 2005-2009 Pentaho and others 009// All Rights Reserved. 010*/ 011package mondrian.recorder; 012 013import org.apache.log4j.Logger; 014 015import java.util.*; 016 017/** 018 * Implementation of {@link MessageRecorder} that records each message 019 * in a {@link List}. The calling code can then access the list and take 020 * actions as needed. 021 */ 022public class ListRecorder extends AbstractRecorder { 023 024 private final List<Entry> errorList; 025 private final List<Entry> warnList; 026 private final List<Entry> infoList; 027 028 public ListRecorder() { 029 errorList = new ArrayList<Entry>(); 030 warnList = new ArrayList<Entry>(); 031 infoList = new ArrayList<Entry>(); 032 } 033 034 public void clear() { 035 super.clear(); 036 errorList.clear(); 037 warnList.clear(); 038 infoList.clear(); 039 } 040 041 public Iterator<Entry> getErrorEntries() { 042 return errorList.iterator(); 043 } 044 045 public Iterator<Entry> getWarnEntries() { 046 return warnList.iterator(); 047 } 048 049 public Iterator<Entry> getInfoEntries() { 050 return infoList.iterator(); 051 } 052 053 protected void recordMessage( 054 final String msg, 055 final Object info, 056 final MsgType msgType) 057 { 058 String context = getContext(); 059 060 Entry e = new Entry(context, msg, msgType, info); 061 switch (msgType) { 062 case INFO: 063 infoList.add(e); 064 break; 065 case WARN: 066 warnList.add(e); 067 break; 068 case ERROR: 069 errorList.add(e); 070 break; 071 default: 072 e = new Entry( 073 context, 074 "Unknown message type enum \"" + msgType 075 + "\" for message: " + msg, 076 MsgType.WARN, 077 info); 078 warnList.add(e); 079 } 080 } 081 082 public void logInfoMessage(final Logger logger) { 083 if (hasInformation()) { 084 logMessage(getInfoEntries(), logger); 085 } 086 } 087 088 public void logWarningMessage(final Logger logger) { 089 if (hasWarnings()) { 090 logMessage(getWarnEntries(), logger); 091 } 092 } 093 094 public void logErrorMessage(final Logger logger) { 095 if (hasErrors()) { 096 logMessage(getErrorEntries(), logger); 097 } 098 } 099 100 static void logMessage(Iterator<Entry> it, Logger logger) { 101 while (it.hasNext()) { 102 Entry e = it.next(); 103 logMessage(e, logger); 104 } 105 } 106 107 static void logMessage( 108 final Entry e, 109 final Logger logger) 110 { 111 logMessage(e.getContext(), e.getMessage(), e.getMsgType(), logger); 112 } 113 114 /** 115 * Entry is a Info, Warning or Error message. This is the object stored 116 * in the Lists MessageRecorder's info, warning and error message lists. 117 */ 118 public static class Entry { 119 private final String context; 120 private final String msg; 121 private final MsgType msgType; 122 private final Object info; 123 124 private Entry( 125 final String context, 126 final String msg, 127 final MsgType msgType, 128 final Object info) 129 { 130 this.context = context; 131 this.msg = msg; 132 this.msgType = msgType; 133 this.info = info; 134 } 135 136 public String getContext() { 137 return context; 138 } 139 140 public String getMessage() { 141 return msg; 142 } 143 144 public MsgType getMsgType() { 145 return msgType; 146 } 147 148 public Object getInfo() { 149 return info; 150 } 151 } 152} 153 154// End ListRecorder.java