1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.remote.system;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.jdiagnose.RemoteResult;
25 import org.jdiagnose.remote.RemoteDataService;
26 import org.jdiagnose.runtime.ResultState;
27
28 /***
29 * @author jmccrindle
30 */
31 public class RemoteDataServiceSystem implements RemoteSystem {
32
33 private RemoteDataService dataService = null;
34
35 private static class DataServiceHost implements RemoteHost {
36
37 private RemoteDataService dataService;
38 private String name;
39 private Map agentAtHosts = new HashMap();
40 private DefaultCompositeStatistics compositeStatistics;
41
42 /***
43 * @param results
44 */
45 public DataServiceHost(RemoteDataService dataService, String name, Iterator results) {
46 this.dataService = dataService;
47 this.name = name;
48 int totalChildren = 0;
49 int upChildren = 0;
50 int downChildren = 0;
51 while(results.hasNext()) {
52 RemoteAgentAtHost agentAtHost = (RemoteAgentAtHost) results.next();
53 totalChildren++;
54 if(agentAtHost.isUp()) upChildren++; else downChildren++;
55 agentAtHosts.put(agentAtHost.getAgent(), agentAtHost);
56 }
57 compositeStatistics = new DefaultCompositeStatistics(totalChildren, upChildren, downChildren);
58 }
59
60
61
62
63 public String getName() {
64 return name;
65 }
66
67
68
69
70 public Iterator getAgents() {
71 return agentAtHosts.values().iterator();
72 }
73
74
75
76
77 public RemoteAgentAtHost getAgent(String agent) {
78 return (RemoteAgentAtHost) agentAtHosts.get(agent);
79 }
80
81
82
83
84 public CompositeStatistics getCompositeStatistics() {
85 return compositeStatistics;
86 }
87
88
89
90
91 public boolean isUp() {
92 return compositeStatistics.getDownChildren() == 0;
93 }
94
95 }
96 private static class DataServiceAgent implements RemoteAgent {
97
98 private RemoteDataService dataService;
99 private String name;
100 private DefaultCompositeStatistics compositeStatistics;
101 private Map agentAtHostsMap = new HashMap();
102
103 /***
104 * @param results
105 */
106 public DataServiceAgent(RemoteDataService dataService, String name, Iterator results) {
107 this.dataService = dataService;
108 this.name = name;
109 int totalChildren = 0;
110 int upChildren = 0;
111 int downChildren = 0;
112 while(results.hasNext()) {
113 RemoteAgentAtHost agentAtHost = (RemoteAgentAtHost) results.next();
114 totalChildren++;
115 if(agentAtHost.isUp()) upChildren++; else downChildren++;
116 agentAtHostsMap.put(agentAtHost.getHost(), agentAtHost);
117 }
118 compositeStatistics = new DefaultCompositeStatistics(totalChildren, upChildren, downChildren);
119 }
120
121
122
123
124 public String getName() {
125 return name;
126 }
127
128
129
130
131 public Iterator getHosts() {
132 return agentAtHostsMap.values().iterator();
133 }
134
135
136
137
138 public RemoteAgentAtHost getHost(String host) {
139 return (RemoteAgentAtHost) agentAtHostsMap.get(host);
140 }
141
142
143
144
145 public CompositeStatistics getCompositeStatistics() {
146 return compositeStatistics;
147 }
148
149
150
151
152 public boolean isUp() {
153 return compositeStatistics.getDownChildren() == 0;
154 }
155
156 }
157 private static class DataServiceDiagnostic implements RemoteDiagnostic {
158
159 private RemoteDataService dataService;
160 private String name;
161 private RemoteResult result;
162
163 public DataServiceDiagnostic(RemoteDataService dataService, RemoteResult result) {
164 this.dataService = dataService;
165 this.name = result.getResultInfo().getName();
166 this.result = result;
167 }
168
169
170
171
172 public String getName() {
173 return name;
174 }
175
176
177
178
179 public Iterator getRemoteResults() {
180 return dataService.getResults(name, result.getAgent(), result.getHost());
181 }
182
183
184
185
186 public boolean isUp() {
187 return result.getResultInfo().getState() != ResultState.FAILED;
188 }
189
190 }
191 private static class DataServiceAgentAtHost implements RemoteAgentAtHost {
192
193 private RemoteDataService dataService;
194 private String agent;
195 private String host;
196 private Map diagnosticsMap = new HashMap();
197 private DefaultCompositeStatistics compositeStatistics;
198
199 /***
200 * @param agentAtHostResults
201 */
202 public DataServiceAgentAtHost(RemoteDataService dataService, String host, String agent, Iterator results) {
203 this.dataService = dataService;
204 this.agent = agent;
205 this.host = host;
206 int totalChildren = 0;
207 int upChildren = 0;
208 int downChildren = 0;
209 while(results.hasNext()) {
210 RemoteResult result = (RemoteResult) results.next();
211 DataServiceDiagnostic diagnostic = new DataServiceDiagnostic(dataService, result);
212 totalChildren++;
213 if(diagnostic.isUp()) upChildren++; else downChildren++;
214 diagnosticsMap.put(result.getResultInfo().getName(), diagnostic);
215 }
216 compositeStatistics = new DefaultCompositeStatistics(totalChildren, upChildren, downChildren);
217 }
218
219
220
221
222 public String getAgent() {
223 return agent;
224 }
225
226
227
228
229 public String getHost() {
230 return host;
231 }
232
233
234
235
236 public Iterator getDiagnostics() {
237 return diagnosticsMap.values().iterator();
238 }
239
240
241
242
243 public RemoteDiagnostic getDiagnostic(String name) {
244 return (RemoteDiagnostic) diagnosticsMap.get(name);
245 }
246
247
248
249
250 public CompositeStatistics getCompositeStatistics() {
251 return compositeStatistics;
252 }
253
254
255
256
257 public boolean isUp() {
258 return compositeStatistics.getDownChildren() == 0;
259 }
260
261 }
262
263 public RemoteDataServiceSystem() {
264
265 }
266
267 public RemoteDataServiceSystem(RemoteDataService dataService) {
268 this.dataService = dataService;
269 }
270
271
272
273
274 public Iterator getHosts() {
275 final Iterator results = dataService.getLatestResultsByHost();
276 List hosts = new ArrayList();
277 if(results.hasNext()) {
278 String currentHost = null;
279 String currentAgentAtHost = null;
280 List agentAtHostResults = new ArrayList();
281 List hostResults = new ArrayList();
282 while(results.hasNext()) {
283 RemoteResult result = (RemoteResult) results.next();
284
285 if(currentHost == null) currentHost = result.getHost();
286 if(currentAgentAtHost == null) currentAgentAtHost = result.getAgent();
287
288 if(currentHost.equals(result.getHost())) {
289 if(currentAgentAtHost.equals(result.getAgent())) {
290 agentAtHostResults.add(result);
291 } else {
292 hostResults.add(new DataServiceAgentAtHost(dataService, currentHost, currentAgentAtHost, agentAtHostResults.iterator()));
293 agentAtHostResults = new ArrayList();
294 currentAgentAtHost = result.getAgent();
295 agentAtHostResults.add(result);
296 }
297 if(!results.hasNext()) {
298 hostResults.add(new DataServiceAgentAtHost(dataService, currentHost, currentAgentAtHost, agentAtHostResults.iterator()));
299 }
300 } else {
301 hosts.add(new DataServiceHost(dataService, currentHost, hostResults.iterator()));
302 hostResults = new ArrayList();
303 currentHost = result.getAgent();
304 }
305
306 if(!results.hasNext()) {
307 hosts.add(new DataServiceHost(dataService, currentHost, hostResults.iterator()));
308 }
309 }
310 }
311 return hosts.iterator();
312 }
313
314
315
316
317 public Iterator getAgents() {
318 final Iterator results = dataService.getLatestResultsByAgent();
319 List agents = new ArrayList();
320 if(results.hasNext()) {
321 String currentAgent = null;
322 String currentAgentAtHost = null;
323 List agentAtHostResults = new ArrayList();
324 List agentResults = new ArrayList();
325 while(results.hasNext()) {
326 RemoteResult result = (RemoteResult) results.next();
327
328 if(currentAgent == null) currentAgent = result.getAgent();
329 if(currentAgentAtHost == null) currentAgentAtHost = result.getHost();
330
331 if(currentAgent.equals(result.getAgent())) {
332 if(currentAgentAtHost.equals(result.getHost())) {
333 agentAtHostResults.add(result);
334 } else {
335 agentResults.add(new DataServiceAgentAtHost(dataService, currentAgentAtHost, currentAgent, agentAtHostResults.iterator()));
336 agentAtHostResults = new ArrayList();
337 currentAgentAtHost = result.getHost();
338 agentAtHostResults.add(result);
339 }
340 if(!results.hasNext()) {
341 agentResults.add(new DataServiceAgentAtHost(dataService, currentAgentAtHost, currentAgent, agentAtHostResults.iterator()));
342 }
343 } else {
344 agents.add(new DataServiceAgent(dataService, currentAgent, agentResults.iterator()));
345 agentResults = new ArrayList();
346 currentAgent = result.getAgent();
347 }
348
349 if(!results.hasNext()) {
350 agents.add(new DataServiceAgent(dataService, currentAgent, agentResults.iterator()));
351 }
352 }
353 }
354 return agents.iterator();
355 }
356
357
358
359
360 public RemoteHost getHost(String name) {
361 Iterator results = dataService.getLatestHostResults(name);
362 if(results.hasNext()) {
363
364 String currentAgentAtHost = null;
365 List agentAtHostResults = new ArrayList();
366 List hostResults = new ArrayList();
367
368 while(results.hasNext()) {
369 RemoteResult result = (RemoteResult) results.next();
370 if(currentAgentAtHost == null) currentAgentAtHost = result.getAgent();
371 if(currentAgentAtHost.equals(result.getAgent())) {
372 agentAtHostResults.add(result);
373 } else {
374 hostResults.add(new DataServiceAgentAtHost(dataService, name, currentAgentAtHost, agentAtHostResults.iterator()));
375 agentAtHostResults = new ArrayList();
376 agentAtHostResults.add(result);
377 currentAgentAtHost = result.getAgent();
378 }
379 if(!results.hasNext()) {
380 hostResults.add(new DataServiceAgentAtHost(dataService, name, currentAgentAtHost, agentAtHostResults.iterator()));
381 }
382 }
383
384 return new DataServiceHost(dataService, name, hostResults.iterator());
385 } else {
386 return null;
387 }
388 }
389
390
391
392
393 public RemoteAgent getAgent(String name) {
394 Iterator results = dataService.getLatestAgentResults(name);
395 if(results.hasNext()) {
396
397 String currentAgentAtHost = null;
398 List agentAtHostResults = new ArrayList();
399 List agentResults = new ArrayList();
400
401 while(results.hasNext()) {
402 RemoteResult result = (RemoteResult) results.next();
403 if(currentAgentAtHost == null) currentAgentAtHost = result.getHost();
404 if(currentAgentAtHost.equals(result.getHost())) {
405 agentAtHostResults.add(result);
406 } else {
407 agentResults.add(new DataServiceAgentAtHost(dataService, currentAgentAtHost, name, agentAtHostResults.iterator()));
408 agentAtHostResults = new ArrayList();
409 agentAtHostResults.add(result);
410 currentAgentAtHost = result.getHost();
411 }
412 if(!results.hasNext()) {
413 agentResults.add(new DataServiceAgentAtHost(dataService, currentAgentAtHost, name, agentAtHostResults.iterator()));
414 }
415 }
416
417 return new DataServiceAgent(dataService, name, agentResults.iterator());
418 } else {
419 return null;
420 }
421 }
422
423
424
425
426 public Iterator getResults() {
427 return dataService.getAllResults();
428 }
429
430 /***
431 * @param dataService The dataService to set.
432 */
433 public void setDataService(RemoteDataService dataService) {
434 this.dataService = dataService;
435 }
436
437
438
439
440 public RemoteResult getResult(String guid) {
441 return dataService.getResult(guid);
442 }
443 }