001    /*
002    @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import java.util.Collections;
007    import java.util.Iterator;
008    import java.util.LinkedList;
009    import java.util.List;
010    
011    /**
012     * @author Pavel Vlasov 
013     * @version $Revision: 1.1 $
014     */
015    public class AccumulatingExceptionSink implements ExceptionSink {
016            private List exceptions=new LinkedList();
017            
018            public class Entry {
019                    Entry(Object source, Exception exception) {
020                            super();
021                            this.source = source;
022                            this.exception = exception;
023                    }
024                    Object source;
025                    Exception exception;
026                    
027                    public Exception getException() {
028                            return exception;
029                    }
030                    public Object getSource() {
031                            return source;
032                    }
033            }
034    
035            public synchronized void consume(Object source, Exception e) {
036                    exceptions.add(new Entry(source, e));
037            }
038            
039            public synchronized List getExceptions() {
040                    return Collections.unmodifiableList(exceptions);
041            }
042            
043            public synchronized void reset() {
044                    exceptions.clear();
045            }
046            
047            /**
048             * Invokes printStackTrace() for all accumulated exceptions 
049             */
050            public synchronized void dump() {
051                    Iterator it=exceptions.iterator();
052                    while (it.hasNext()) {
053                            Entry entry=(Entry) it.next();
054                            System.err.println("Source: "+entry.getSource());
055                            entry.getException().printStackTrace();
056                    }
057            }
058    }