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