001    /*
002    @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import java.lang.reflect.Method;
007    import java.util.Collections;
008    import java.util.Iterator;
009    import java.util.LinkedList;
010    import java.util.List;
011    
012    /**
013     * @author Pavel Vlasov 
014     * @version $Revision: 1.1 $
015     */
016    public class AccumulatingVisitorExceptionSink implements VisitorExceptionSink {
017            private List exceptions=new LinkedList();
018            
019            public class Entry {
020                    private Object visitor;
021                    private Exception exception;
022                    private Object visitee;
023                    private Method method;
024    
025                    Entry(Object visitor, Method method, Object visitee, Exception exception) {
026                            this.visitor = visitor;
027                            this.exception = exception;
028                            this.visitee = visitee;
029                            this.method = method;
030                    }
031                    
032                    public Exception getException() {
033                            return exception;
034                    }
035                    
036                    public Method getMethod() {
037                            return method;
038                    }
039                    
040                    public Object getVisitee() {
041                            return visitee;
042                    }
043                    
044                    public Object getVisitor() {
045                            return visitor;
046                    }
047            }
048    
049            public void consume(DispatchingVisitor dispatcher, Object visitor, Method method, Object visitee, Exception e) {
050                    exceptions.add(new Entry(visitor, method, visitee, e));
051            }
052            
053            public synchronized List getExceptions() {
054                    return Collections.unmodifiableList(exceptions);
055            }
056            
057            public synchronized void reset() {
058                    exceptions.clear();
059            }
060            
061            /**
062             * Invokes printStackTrace() for all accumulated exceptions 
063             */
064            public synchronized void dump() {
065                    Iterator it=exceptions.iterator();
066                    while (it.hasNext()) {
067                            Entry entry=(Entry) it.next();
068                            System.err.println("Visitor: "+entry.getVisitor());
069                            System.err.println("Method: "+entry.getMethod());
070                            System.err.println("Visitee: "+entry.getVisitee());
071                            entry.getException().printStackTrace();
072                    }
073            }
074    }