1 package org.opensync.tools;
2
3 import java.text.DecimalFormat;
4
5 public final class TrackSourceMethod {
6
7 private String processEventId;
8 private String processItem;
9 private String callerEventId;
10 private String uid;
11 private String sourceId;
12 private String sourceName;
13 private String methodId;
14 private String sourceMethod;
15 private int level;
16 private long serialNumber;
17 private Exception exception;
18 private boolean isEnd;
19 private long beginTimestamp,endTimestamp,exceptionTimestamp;
20 private String exceptionMessage;
21 private String exceptionSourceMethod;
22 private String exceptionEventId;
23 private short exceptionStatus;
24 private String sourceMethodEventId;
25 private StringBuffer comments;
26 private static DecimalFormat df = new DecimalFormat("000");
27
28 public TrackSourceMethod(String processEventId, String processItem, String callerEventId,
29 String uid, int level, long serialNumber, String sourceMethod, String comments)
30 throws Exception {
31 this.processEventId = processEventId;
32 this.processItem = processItem;
33 this.callerEventId = callerEventId;
34 this.uid = uid;
35 this.level = level;
36 this.serialNumber = serialNumber;
37 this.exceptionTimestamp = 0;
38 this.endTimestamp = 0;
39 this.sourceMethod = sourceMethod;
40 this.sourceId = sourceMethod.substring(0,sourceMethod.indexOf(':'));
41 this.methodId = sourceMethod.substring(sourceMethod.indexOf(':') + 1);
42 this.sourceMethodEventId = makeSourceMethodEventId();
43 this.exception = null;
44 this.exceptionEventId = null;
45 this.isEnd = false;
46 this.exceptionSourceMethod = null;
47 this.exceptionStatus = 0;
48 this.comments = new StringBuffer("");
49 if (comments != null) this.comments.append(comments);
50 if ((sourceId.indexOf(".") != -1)) {
51 this.sourceName = new String(sourceId.substring(sourceId.lastIndexOf(".") + 1));
52 } else {
53 this.sourceName = new String(sourceId);
54 }
55 this.beginTimestamp = System.currentTimeMillis();
56 }
57
58 public void endEvent(long endTimestamp, Exception exception, String endComments) throws Exception {
59 this.endTimestamp = endTimestamp;
60 this.exception = exception;
61 if (endComments != null) {
62 if (this.comments.length() > 0) this.comments.append(";" + endComments);
63 else this.comments.append(endComments);
64 }
65 if (this.exception != null) {
66 this.exceptionSourceMethod = new String(Tracker.findCaller(exception));
67 if (this.exceptionSourceMethod.equals(this.sourceMethod)) {
68 this.exceptionTimestamp = this.endTimestamp;
69 this.exceptionMessage = formatExceptionMessage(this.exception);
70 this.exceptionEventId = this.sourceMethodEventId;
71 this.exceptionStatus = 1;
72 }
73 }
74 this.isEnd = true;
75 }
76
77 public String uid() {
78 return uid;
79 }
80
81 public String processEventId() {
82 return processEventId;
83 }
84
85 public String processItem() {
86 return processItem;
87 }
88
89 public int level() {
90 return level;
91 }
92
93 public String sourceId() {
94 return sourceId;
95 }
96
97 public String sourceName() {
98 return sourceName;
99 }
100
101 public String methodId() {
102 return methodId;
103 }
104
105 public String sourceMethod() {
106 return sourceMethod;
107 }
108
109 public String callerEventId() {
110 return callerEventId;
111 }
112
113 public long beginTimestamp() throws Exception {
114 if (beginTimestamp == 0) {
115 throw new Exception("TrackEvent instance not begin, beginTimestamp is undefined");
116 }
117 return beginTimestamp;
118 }
119
120 public Exception exception() throws Exception {
121 if (endTimestamp == 0) {
122 throw new Exception("TrackEvent instance not ended, exception undefined");
123 }
124 return exception;
125 }
126
127 public String exceptionMessage() throws Exception {
128 if (endTimestamp == 0) {
129 throw new Exception("TrackEvent instance not ended, exceptionMessage undefined");
130 }
131 return exceptionMessage;
132 }
133
134 public long exceptionTimestamp() throws Exception {
135 if (endTimestamp == 0) {
136 throw new Exception("TrackEvent instance not ended, exceptionTimestamp undefined");
137 }
138 return exceptionTimestamp;
139 }
140
141 public String exceptionSourceMethod() throws Exception {
142 if (endTimestamp == 0) {
143 throw new Exception("TrackEvent instance not ended, exception trackItem undefined");
144 }
145 return exceptionSourceMethod;
146 }
147
148 public String exceptionEventId() throws Exception {
149 if (endTimestamp == 0) {
150 throw new Exception("TrackEvent instance not ended, exception sourceMethodEventId undefined");
151 }
152 return exceptionEventId;
153 }
154
155 public short exceptionStatus() throws Exception {
156 if (endTimestamp == 0) {
157 throw new Exception("TrackEvent instance not ended, exception status undefined");
158 }
159 return exceptionStatus;
160 }
161
162 public long endTimestamp() throws Exception {
163 if (endTimestamp == 0) {
164 throw new Exception("TrackEvent instance not ended, endtime is 0");
165 }
166 return endTimestamp;
167 }
168
169 public String comments() {
170 return comments.toString();
171 }
172
173 public boolean isEnd() {
174 return isEnd;
175 }
176
177 public long responseTime() throws Exception {
178 if (beginTimestamp == 0) {
179 throw new Exception("TrackEvent instance not begin, beginTimestamp is 0");
180 }
181 if (endTimestamp == 0) {
182 throw new Exception("TrackEvent instance not ended, endTimestamp is 0");
183 }
184 return (endTimestamp - beginTimestamp);
185 }
186
187 private static String formatExceptionMessage(Exception exception) throws Exception {
188 StringBuffer exceptionMessage = new StringBuffer("");
189 if (exception != null) {
190 if (exception.getMessage() != null && exception.getMessage().length() != 0) {
191 exceptionMessage.append(exception.getMessage());
192 } else if (exception.toString() != null && exception.toString().length() != 0) {
193 exceptionMessage.append(exception.toString());
194 } else {
195 exceptionMessage.append("Unknown Exception!");
196 }
197 if (exceptionMessage.length() > 512) {
198 exceptionMessage.delete(512,exceptionMessage.length());
199 }
200 }
201 return exceptionMessage.toString();
202 }
203
204 public String sourceMethodEventId() {
205 return sourceMethodEventId;
206 }
207
208 private String makeSourceMethodEventId() {
209 String derivedId = new String(processEventId.substring(0,processEventId.lastIndexOf('.') + 1) + df.format(serialNumber));
210 return derivedId;
211 }
212
213 private static void printMessage(String msg) {
214 if (Tracker.isDebug()) {
215 System.out.println("TrackEvent::" + msg);
216 }
217 }
218
219 public String toString() {
220 return sourceMethod;
221 }
222 }