1 package org.opensync.tools;
2
3 import java.text.DecimalFormat;
4 import java.util.ArrayList;
5
6 public final class TrackProcess {
7
8 private static int pxCounter = 0;
9 private ArrayList arrListTrackSourceMethods;
10 private String processEventId;
11 private String processItem;
12 private String uid;
13 private int level;
14 private long serialNumber;
15 private boolean isEnd;
16 private boolean isHung;
17 private long beginTimestamp, endTimestamp, exceptionTimestamp;
18 private String beginSourceMethodEventId;
19 private String endSourceMethodEventId;
20 private Exception exception;
21 private String exceptionEventId;
22 private String exceptionTrackItem;
23 private String exceptionMessage;
24 private String comments;
25 private short exceptionStatus;
26 private static DecimalFormat _DF = new DecimalFormat("000");
27
28 public TrackProcess(String processItem, String uid)
29 throws Exception {
30 this.beginTimestamp = System.currentTimeMillis();
31 this.endTimestamp = 0;
32 this.processEventId = makeProcessId();
33 this.processItem = processItem;
34 this.uid = uid;
35 this.isEnd = false;
36 this.isHung = false;
37 this.level = -1;
38 this.serialNumber = 0;
39 this.beginSourceMethodEventId = null;
40 this.exception = null;
41 this.exceptionTimestamp = 0;
42 this.exceptionStatus = 0;
43 this.comments = null;
44 this.arrListTrackSourceMethods = new ArrayList();
45 }
46
47 public String addTrackSourceMethod(String sourceMethod, String comments)
48 throws Exception {
49 String callerEventId = null;
50 level++;
51 serialNumber++;
52
53 if (level == 0) {
54 callerEventId = new String("M");
55 } else {
56 callerEventId = new String(findLastTrackSourceMethodByLevel(level - 1).sourceMethodEventId());
57 }
58 TrackSourceMethod trackSourceMethod = new TrackSourceMethod(processEventId,processItem,callerEventId,uid,level,serialNumber,sourceMethod,comments);
59 arrListTrackSourceMethods.add(trackSourceMethod);
60 if (this.beginTimestamp > trackSourceMethod.beginTimestamp()) {
61 this.beginTimestamp = trackSourceMethod.beginTimestamp();
62 this.beginSourceMethodEventId = trackSourceMethod.sourceMethodEventId();
63 }
64 printMessage(trackSourceMethod.sourceMethodEventId() + ":" + trackSourceMethod.sourceMethod() + ":" + level + "(added)");
65 return trackSourceMethod.sourceMethodEventId();
66 }
67
68 public void end(long lTime, String originSourceMethod, Exception exception, String comments)
69 throws Exception {
70
71 TrackSourceMethod trackSourceMethod = findLastTrackSourceMethodByLevel(level);
72 if (trackSourceMethod != null) {
73 if (!trackSourceMethod.sourceMethod().equals(originSourceMethod)) {
74 isHung = true;
75 return;
76 }
77 trackSourceMethod.endEvent(lTime,exception,comments);
78 if (trackSourceMethod.exceptionStatus() == 1) {
79 if (exceptionTimestamp < trackSourceMethod.exceptionTimestamp()) {
80 exception = trackSourceMethod.exception();
81 exceptionTimestamp = trackSourceMethod.exceptionTimestamp();
82 exceptionEventId = trackSourceMethod.exceptionEventId();
83 exceptionTrackItem = trackSourceMethod.exceptionSourceMethod();
84 exceptionMessage = trackSourceMethod.exceptionMessage();
85 exceptionStatus = 1;
86 }
87 }
88 printMessage(trackSourceMethod.sourceMethodEventId() + ":" + trackSourceMethod.sourceMethod() + ":" + level + "(ended)");
89 level--;
90 if (level < 0) {
91 this.endTimestamp = lTime;
92 this.endSourceMethodEventId = trackSourceMethod.sourceMethodEventId();
93 isEnd = true;
94 }
95 }
96 }
97
98 public TrackSourceMethod findLastTrackSourceMethodByLevel(int nLevel)
99 throws Exception {
100 TrackSourceMethod trackSourceMethodReturn = null;
101 TrackSourceMethod trackSourceMethod = null;
102 for (int i=arrListTrackSourceMethods.size() - 1; i>-1; i--){
103 trackSourceMethod = (TrackSourceMethod)arrListTrackSourceMethods.get(i);
104 if (trackSourceMethod.level() == nLevel && !trackSourceMethod.isEnd()){
105 trackSourceMethodReturn = trackSourceMethod;
106 break;
107 }
108 }
109 return trackSourceMethodReturn;
110 }
111
112 public void setComments(String comments) {
113 this.comments = comments;
114 }
115
116 public ArrayList arrListTrackSourceMethods() {
117 return arrListTrackSourceMethods;
118 }
119
120 public long beginTimestamp() {
121 return beginTimestamp;
122 }
123
124 public long endTimestamp() {
125 return endTimestamp;
126 }
127
128 public String processEventId() {
129 return processEventId;
130 }
131
132 public String processItem() {
133 return processItem;
134 }
135
136 public long responseTime() {
137 return (endTimestamp - beginTimestamp);
138 }
139
140 public String uid() {
141 return uid;
142 }
143
144 public int level() {
145 return level;
146 }
147
148 public boolean isHung() {
149 if ((System.currentTimeMillis() - beginTimestamp) > 300000) isHung = true;
150 return isHung;
151 }
152
153 public String beginSourceMethodEventId() {
154 return beginSourceMethodEventId;
155 }
156
157 public String endSourceMethodEventId() {
158 return endSourceMethodEventId;
159 }
160
161 public Exception exception() {
162 return exception;
163 }
164
165 public String exceptionTrackItem() {
166 return exceptionTrackItem;
167 }
168
169 public String exceptionEventId() {
170 return exceptionEventId;
171 }
172
173 public long exceptionTimestamp() {
174 return exceptionTimestamp;
175 }
176
177 public String exceptionMessage() {
178 return exceptionMessage;
179 }
180
181 public short exceptionStatus() {
182 return exceptionStatus;
183 }
184
185 public String comments() {
186 return comments;
187 }
188
189 public boolean isEnd() {
190 return isEnd;
191 }
192
193 private String makeProcessId() {
194 Long lTime = new Long(this.beginTimestamp);
195 String derivedId = new String(lTime.toString() + "." + _DF.format(getProcessCounter()) + ".000");
196 return derivedId;
197 }
198
199 private static synchronized int getProcessCounter() {
200 if (pxCounter > 999) pxCounter = 0;
201 pxCounter++;
202 return pxCounter;
203 }
204
205 private static void printMessage(String msg) {
206 if (Tracker.isDebug()) {
207 System.out.println("TrackProcess::" + msg);
208 }
209 }
210
211 }