View Javadoc

1   package cz.cuni.amis.pogamut.base.component.bus.event.impl;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   
6   import cz.cuni.amis.pogamut.base.component.IComponent;
7   import cz.cuni.amis.pogamut.base.component.bus.event.IFatalErrorEvent;
8   import cz.cuni.amis.utils.Const;
9   
10  public class FatalErrorEvent<SOURCE extends IComponent> implements IFatalErrorEvent<SOURCE> {
11  		
12  	protected Object origin;
13  	protected SOURCE component;
14  	protected String message;
15  	protected Throwable cause;
16  	protected StackTraceElement[] stackTrace;
17  	
18  	public FatalErrorEvent(SOURCE component, String message) {
19  		this.message = message;
20  		this.component = component;
21  		this.origin = origin;
22  		stackTrace();
23  	}
24  	
25  	public FatalErrorEvent(SOURCE component, String message, Throwable cause) {
26  		this.message = message;
27  		this.component = component;
28  		this.cause = cause;
29  		stackTrace();
30  	}
31  	
32  	public FatalErrorEvent(SOURCE component, Throwable cause) {
33  		this.message = cause.getMessage();
34  		this.component = component;
35  		this.cause = cause;
36  		stackTrace();
37  	}
38  	
39  	private void stackTrace() {
40  		Exception e = new Exception();
41  		this.stackTrace = e.getStackTrace();
42  		this.stackTrace = Arrays.copyOfRange(this.stackTrace, 2, this.stackTrace.length);
43  	}
44  	
45  	@Override
46  	public SOURCE getSource() {
47  		return component;
48  	}
49  
50  	@Override
51  	public Throwable getCause() {
52  		return cause;
53  	}
54  
55  	@Override
56  	public String getMessage() {
57  		return message;
58  	}
59  
60  	@Override
61  	public StackTraceElement[] getStackTrace() {
62  		return stackTrace;
63  	}
64  
65  	protected String printStackStrace(StackTraceElement[] stackTraceToPrint, String indent) {
66  		StringBuffer sb = new StringBuffer();
67  		sb.append(indent);
68  		sb.append(stackTraceToPrint[0]);
69  		for (int i = 1; i < stackTraceToPrint.length; ++i) {
70  			sb.append(Const.NEW_LINE);
71  			sb.append(indent);
72  			sb.append(stackTraceToPrint[i]);
73  		}
74  		return sb.toString();
75  	}
76  	
77  	public String toString() {
78  		return getSummary();
79  	}
80  	
81  	@Override
82  	public String getSummary() {
83  		StringBuffer sb = new StringBuffer();
84  		sb.append("FatalErrorEvent[");
85  		sb.append(Const.NEW_LINE);
86  		sb.append("    Component:  " + component);
87  		sb.append(Const.NEW_LINE);
88  		sb.append("    Message:    " + message);
89  		if (cause != null) {
90  			Throwable cur = cause;
91  			while (cur != null) {
92  				sb.append(Const.NEW_LINE);
93  				sb.append("    Cause:      " + cur.getClass() + ": " + cur.getMessage() + 
94  						(cur.getStackTrace() == null || cur.getStackTrace().length == 0 ? 
95  								" (at UNAVAILABLE)"
96  							:	" (at " + cur.getStackTrace()[0].toString() + ")")
97  				);
98  				cur = cur.getCause();
99  			}
100 			sb.append(Const.NEW_LINE);
101 			sb.append("    Stacktrace:");
102 			sb.append(Const.NEW_LINE);
103 			sb.append(printStackStrace(stackTrace, "        "));
104 			cur = cause;
105 			while (cur != null) {
106 				sb.append(Const.NEW_LINE);
107 				sb.append("    Caused by: " + cur.getClass() + ": " + cur.getMessage() + 
108 						(cur.getStackTrace() == null || cur.getStackTrace().length == 0 ? 
109 								" (at UNAVAILABLE)"
110 							:	" (at " + cur.getStackTrace()[0].toString() + ")")
111 				);
112 				sb.append(Const.NEW_LINE);
113 				sb.append(printStackStrace(cur.getStackTrace(), "        "));
114 				cur = cur.getCause();
115 			}
116 		} else {
117 			sb.append(Const.NEW_LINE);
118 			sb.append("    Stacktrace:");
119 			sb.append(Const.NEW_LINE);
120 			sb.append(printStackStrace(stackTrace, "        "));
121 		}
122 		sb.append(Const.NEW_LINE);
123 		sb.append("]");
124 		return sb.toString();
125 	}
126 	
127 }