001    package org.LiveGraph.events;
002    
003    import java.io.PrintStream;
004    import java.io.PrintWriter;
005    import java.util.ArrayList;
006    import java.util.Collections;
007    import java.util.List;
008    
009    public class EventProcessingException extends Exception {
010    
011    private List<Cause> causes;
012    
013    public EventProcessingException() {
014            causes = new ArrayList<Cause>();
015    }
016    
017    public synchronized void addCause(Event<? extends EventType> event, EventListener listener, Throwable exception) {
018            causes.add(new Cause(event, listener, exception));
019    }
020    
021    public synchronized int countCauses() {
022            return causes.size();
023    }
024    
025    public synchronized List<Cause> getCauses() {
026            return Collections.unmodifiableList(causes);
027    }
028    
029    @Override
030    public synchronized String getLocalizedMessage() {
031            String s = String.format("EventProcessingException caused by %d actual Throwable(s):%n", countCauses());
032            for (int i = 0; i < causes.size(); i++) {
033                    Cause c = causes.get(i);
034                    s += String.format("(#%d) Event: %s. Listener: %s.%n",
035                                                       i + 1,
036                                                       (null == c.getCausingEvent() ? "null" : c.getCausingEvent().toString()),
037                                                       (null == c.getCausingListener() ? "null" : c.getCausingListener().toString()));
038                    
039                    s += String.format("(#%d) Throwable: %s.%n",
040                                                       i + 1,
041                                                       (null == c.getCausingException() ? "null" : c.getCausingException().toString()));
042            }
043            return s;
044    }
045    
046    @Override
047    public synchronized void printStackTrace(PrintStream s) {       
048            PrintWriter out = new PrintWriter(s);
049            printStackTrace(out);
050    }
051    
052    @Override
053    public synchronized void printStackTrace(PrintWriter s) {
054            synchronized (s) {
055            s.println(this);
056            StackTraceElement[] trace = getStackTrace();
057            for (int i = 0; i < trace.length; i++)
058                s.println("\tat " + trace[i]);
059            s.flush();
060            
061            for (int i = 0; i < causes.size(); i++) {
062                    s.printf("Cause #%d of %d: %n", i + 1, countCauses());
063                    Cause c = causes.get(i);
064                    if (null != c.getCausingException())
065                            c.getCausingException().printStackTrace(s);
066            }
067            s.flush();
068        }
069    }
070    
071    
072    public static class Cause {
073    
074            private Event<? extends EventType> event;
075            private EventListener listener;
076            private Throwable exception;
077            
078            public Cause(Event<? extends EventType> ev, EventListener li, Throwable ex) {
079                    event = ev;
080                    listener = li;
081                    exception = ex;
082            }
083            
084            public Event<? extends EventType> getCausingEvent() {
085                    return event;
086            }
087            
088            public EventListener getCausingListener() {
089                    return listener;
090            }
091            
092            public Throwable getCausingException() {
093                    return exception;
094            }
095            
096    } // private static class Cause
097    
098    }