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.Collections;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.jdiagnose.RemoteResult;
26 import org.jdiagnose.concurrent.ConcurrentHashMap;
27 import org.jdiagnose.remote.RemoteResultException;
28 import org.jdiagnose.remote.RemoteResultStore;
29
30 /***
31 * @author jmccrindle
32 */
33 public class InMemoryRemoteSystem implements RemoteSystem, RemoteResultStore {
34
35 private List results = Collections.synchronizedList(new LinkedList());
36 private Map resultsMap = new ConcurrentHashMap();
37 private Map hosts = new ConcurrentHashMap();
38 private Map agents = new ConcurrentHashMap();
39 private int max = 1000;
40
41
42
43
44 public void addRemoteResults(Iterator results) throws RemoteResultException {
45 while(results.hasNext()) {
46 addRemoteResult((RemoteResult) results.next());
47 }
48 }
49
50
51
52
53 public void addRemoteResult(RemoteResult result) throws RemoteResultException {
54 if(results.size() > max) {
55 RemoteResult removedResult = (RemoteResult) results.remove(results.size() - 1);
56 resultsMap.remove(removedResult.getGuid());
57 removeResult(removedResult);
58 }
59 results.add(0, result);
60 resultsMap.put(result.getGuid(), result);
61 addResult(result);
62 }
63
64 protected boolean removeResult(RemoteResult result) {
65 DefaultRemoteHost host = (DefaultRemoteHost) hosts.get(result.getHost());
66 boolean resultFound = false;
67 if(host != null) {
68 DefaultRemoteAgentAtHost agentAtHost = (DefaultRemoteAgentAtHost) host.getAgentAtHost(result.getAgent());
69 if(agentAtHost != null) {
70 DefaultRemoteDiagnostic diagnostic = (DefaultRemoteDiagnostic) agentAtHost.getDiagnostic(result.getResultInfo().getName());
71 if(diagnostic != null) {
72 List remoteResultList = diagnostic.getRemoteResultList();
73 int size = remoteResultList.size();
74 if(size > 0) {
75 for(int i = size - 1; i >=0; i--) {
76 RemoteResult diagnosticResult = (RemoteResult) remoteResultList.get(i);
77 if(diagnosticResult != null && diagnosticResult.getGuid().equals(result.getGuid())) {
78 remoteResultList.remove(i);
79 resultFound = true;
80 break;
81 }
82 }
83 }
84 if(remoteResultList.size() == 0) {
85 agentAtHost.getDiagnosticMap().remove(diagnostic.getName());
86 if(agentAtHost.getDiagnosticMap().size() == 0) {
87 host.getAgentsMap().remove(agentAtHost.getAgent());
88 DefaultRemoteAgent agent = (DefaultRemoteAgent) agents.get(result.getAgent());
89 if(agent != null) {
90 agent.getHostsMap().remove(agentAtHost.getHost());
91 if(host.getAgentsMap().size() == 0) {
92 hosts.remove(host.getName());
93 }
94 if(agent.getHostsMap().size() == 0) {
95 agents.remove(agent.getName());
96 }
97 }
98 }
99 }
100 }
101 }
102 }
103 return resultFound;
104 }
105
106 protected void addResult(RemoteResult result) {
107 DefaultRemoteHost host = (DefaultRemoteHost) hosts.get(result.getHost());
108 if(host == null) {
109 host = new DefaultRemoteHost(result.getHost());
110 hosts.put(result.getHost(), host);
111 }
112 DefaultRemoteAgent agent = (DefaultRemoteAgent) agents.get(result.getAgent());
113 if(agent == null) {
114 agent = new DefaultRemoteAgent(result.getAgent());
115 agents.put(result.getAgent(), agent);
116 }
117 DefaultRemoteAgentAtHost agentAtHost = (DefaultRemoteAgentAtHost) host.getAgentAtHost(result.getAgent());
118 if(agentAtHost == null) {
119
120 agentAtHost = new DefaultRemoteAgentAtHost(result.getHost(), result.getAgent());
121 host.getAgentsMap().put(result.getAgent(), agentAtHost);
122 agent.getHostsMap().put(result.getHost(), agentAtHost);
123 }
124 String diagnosticName = result.getResultInfo().getName();
125 DefaultRemoteDiagnostic diagnostic = (DefaultRemoteDiagnostic) agentAtHost.getDiagnostic(diagnosticName);
126 if(diagnostic == null) {
127 diagnostic = new DefaultRemoteDiagnostic(diagnosticName);
128 agentAtHost.getDiagnosticMap().put(diagnosticName, diagnostic);
129 }
130 diagnostic.getRemoteResultList().add(0, result);
131 }
132
133 public Iterator getHosts() {
134 return hosts.values().iterator();
135 }
136
137 public Iterator getAgents() {
138 return agents.values().iterator();
139 }
140
141
142
143
144 public String toString() {
145 return "{" + hosts.toString() + ", " + agents.toString() + "}";
146 }
147
148
149
150
151 public RemoteHost getHost(String name) {
152 return name == null ? null : (RemoteHost) hosts.get(name);
153 }
154
155
156
157
158 public RemoteAgent getAgent(String name) {
159 return name == null ? null : (RemoteAgent) agents.get(name);
160 }
161
162
163
164
165 public Iterator getResults() {
166 return new ArrayList(results).iterator();
167 }
168
169 /***
170 * @return Returns the max.
171 */
172 public int getMax() {
173 return max;
174 }
175 /***
176 * @param max The max to set.
177 */
178 public void setMax(int max) {
179 this.max = max;
180 }
181
182
183
184
185 public RemoteResult getResult(String guid) {
186 return guid == null ? null : (RemoteResult) resultsMap.get(guid);
187 }
188
189 }