1
2
3
4 package org.jdiagnose.runtime;
5
6 import java.util.ArrayList;
7 import java.util.Iterator;
8 import java.util.List;
9
10 import org.jdiagnose.Diagnostic;
11 import org.jdiagnose.DiagnosticContainer;
12 import org.jdiagnose.DiagnosticMessage;
13 import org.jdiagnose.MessageFactory;
14 import org.jdiagnose.ResultInfo;
15
16 /***
17 * @author jmccrindle
18 */
19 public class DefaultDiagnosticContainerResult implements DiagnosticContainerResult {
20
21 private DiagnosticContainer container;
22 private DiagnosticMessage message;
23 private List results = new ArrayList();
24 private List containerResults = new ArrayList();
25 private int totalDiagnostics = 0;
26 private MessageFactory messageFactory = null;
27
28 public DefaultDiagnosticContainerResult(MessageFactory messageFactory, DiagnosticContainer container) {
29 this.messageFactory = messageFactory;
30 this.container = container;
31 for (Iterator diagnosticIterator = container.getDiagnosticContainers().iterator(); diagnosticIterator.hasNext();) {
32 DiagnosticContainer childContainer = (DiagnosticContainer) diagnosticIterator.next();
33 DefaultDiagnosticContainerResult containerResult = new DefaultDiagnosticContainerResult(messageFactory, childContainer);
34 containerResults.add(containerResult);
35 totalDiagnostics += containerResult.getTotalDiagnostics();
36 }
37 for (Iterator containerIterator = container.getDiagnostics().iterator(); containerIterator.hasNext();) {
38 Diagnostic diagnostic = (Diagnostic) containerIterator.next();
39 DefaultDiagnosticResult result = new DefaultDiagnosticResult(messageFactory, diagnostic);
40 results.add(result);
41 totalDiagnostics++;
42 }
43 }
44
45
46
47
48 public int getTotalDiagnostics() {
49 return totalDiagnostics;
50 }
51
52
53
54
55 public int getFailedDiagnostics() {
56 int failedDiagnostics = 0;
57 for (Iterator resultIterator = results.iterator(); resultIterator.hasNext();) {
58 DiagnosticResult result = (DiagnosticResult) resultIterator.next();
59 if(result.getState() == ResultState.FAILED) {
60 failedDiagnostics++;
61 }
62 }
63 return failedDiagnostics;
64 }
65
66
67
68
69 public int getSuccessfulDiagnostics() {
70 int successfulDiagnostics = 0;
71 for (Iterator resultIterator = results.iterator(); resultIterator.hasNext();) {
72 DiagnosticResult result = (DiagnosticResult) resultIterator.next();
73 if(result.getState() == ResultState.SUCCEEDED) {
74 successfulDiagnostics++;
75 }
76 }
77 return successfulDiagnostics;
78 }
79
80
81
82
83 public List getResults() {
84 return results;
85 }
86
87
88
89
90 public String getName() {
91 return container.getName();
92 }
93
94 protected ResultState getStateFromList(List resultInfos) {
95 ResultState state = ResultState.NOT_STARTED;
96 boolean allRunning = false;
97 boolean allSucceeded = true;
98 for (Iterator resultIterator = resultInfos.iterator(); resultIterator.hasNext();) {
99 ResultInfo result = (ResultInfo) resultIterator.next();
100 if(result.getState() == ResultState.FAILED) {
101 return ResultState.FAILED;
102 } else if (result.getState() == ResultState.RUNNING) {
103 allSucceeded = false;
104 allRunning = true;
105 } else if (result.getState() == ResultState.SUCCEEDED) {
106 allRunning = true;
107 } else if (result.getState() == ResultState.NOT_STARTED) {
108 allSucceeded = false;
109 }
110 }
111 if(allSucceeded) {
112 state = ResultState.SUCCEEDED;
113 } else if (allRunning) {
114 state = ResultState.RUNNING;
115 }
116 return state;
117 }
118
119
120
121
122 public ResultState getState() {
123 ResultState resultsState = getStateFromList(results);
124 if(resultsState == ResultState.FAILED) {
125 return ResultState.FAILED;
126 }
127 ResultState containersState = getStateFromList(containerResults);
128 if(containersState == ResultState.FAILED) {
129 return ResultState.FAILED;
130 } else if (resultsState == ResultState.RUNNING || containersState == ResultState.RUNNING) {
131 return ResultState.RUNNING;
132 } else if (resultsState == ResultState.NOT_STARTED && containersState == ResultState.NOT_STARTED) {
133 return ResultState.NOT_STARTED;
134 } else if (resultsState == ResultState.SUCCEEDED && containersState == ResultState.SUCCEEDED) {
135 return ResultState.SUCCEEDED;
136 } else {
137 return ResultState.NOT_STARTED;
138 }
139 }
140
141
142
143
144 public long getDuration() {
145 return getFinishTime() - getStartTime();
146 }
147
148 protected long getStartTimeFromList(List resultInfos) {
149 long startTime = Long.MAX_VALUE;
150 for (Iterator resultIterator = resultInfos.iterator(); resultIterator.hasNext();) {
151 ResultInfo result = (ResultInfo) resultIterator.next();
152 long resultStartTime = result.getStartTime();
153 if(resultStartTime < startTime) {
154 startTime = resultStartTime;
155 }
156 }
157 return startTime;
158 }
159
160 protected long getFinishTimeFromList(List resultInfos) {
161 long finishTime = 0;
162 for (Iterator resultIterator = resultInfos.iterator(); resultIterator.hasNext();) {
163 ResultInfo result = (ResultInfo) resultIterator.next();
164 long resultFinishTime = result.getFinishTime();
165 if(resultFinishTime > finishTime) {
166 finishTime = resultFinishTime;
167 }
168 }
169 return finishTime;
170 }
171
172
173
174
175 public long getStartTime() {
176 long resultStartTime = getStartTimeFromList(results);
177 long containerStartTime = getStartTimeFromList(containerResults);
178 if(resultStartTime == Long.MAX_VALUE && containerStartTime == Long.MAX_VALUE) {
179 return 0;
180 } else if(resultStartTime == Long.MAX_VALUE) {
181 return containerStartTime;
182 } else if (containerStartTime == Long.MAX_VALUE) {
183 return resultStartTime;
184 } else {
185 return resultStartTime < containerStartTime ? resultStartTime : containerStartTime;
186 }
187 }
188
189
190
191
192 public long getFinishTime() {
193 long resultFinishTime = getFinishTimeFromList(results);
194 long containerFinishTime = getFinishTimeFromList(containerResults);
195 if(resultFinishTime == 0) {
196 return containerFinishTime;
197 } else if (containerFinishTime == 0) {
198 return resultFinishTime;
199 } else {
200 return resultFinishTime > containerFinishTime ? resultFinishTime : containerFinishTime;
201 }
202 }
203
204
205
206
207 public List getContainerResults() {
208 return containerResults;
209 }
210
211
212
213
214 public DiagnosticContainer getDiagnosticContainer() {
215 return container;
216 }
217
218 public DiagnosticMessage getMessage() {
219 return message;
220 }
221 }