1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.library.mock;
17
18 import java.util.Arrays;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Random;
22
23 import org.jdiagnose.DefaultDiagnosticMessage;
24 import org.jdiagnose.RemoteResult;
25 import org.jdiagnose.remote.DefaultRemoteResult;
26 import org.jdiagnose.remote.DefaultResultInfo;
27 import org.jdiagnose.remote.GuidGenerator;
28 import org.jdiagnose.remote.RemoteResultException;
29 import org.jdiagnose.remote.VMIDGuidGenerator;
30 import org.jdiagnose.remote.system.InMemoryRemoteSystem;
31 import org.jdiagnose.remote.system.RemoteAgent;
32 import org.jdiagnose.remote.system.RemoteHost;
33 import org.jdiagnose.remote.system.RemoteSystem;
34 import org.jdiagnose.runtime.ResultState;
35
36 /***
37 * @author jmccrindle
38 */
39 public class MockRemoteSystem implements RemoteSystem {
40
41 private InMemoryRemoteSystem system = new InMemoryRemoteSystem();
42 private GuidGenerator guidGenerator = new VMIDGuidGenerator();
43 private Random random = new Random();
44 private List hosts = null;
45 private List agents = null;
46 private List diagnostics = null;
47 private long nextStartTime = System.currentTimeMillis();
48 private int maxResults = 100000;
49 private double failurePercentage = 0.1;
50 private List messages = null;
51 private String domain = "jdiagnose.org";
52 private long sequenceNumber;
53
54 public MockRemoteSystem() {
55
56 }
57
58 protected RemoteResult createRandomFailingRemoteResult(String diagnostic, String agent, String host, String message) {
59 return createRandomRemoteResult(diagnostic, agent, host, false, message);
60 }
61
62 protected RemoteResult createRandomSucceedingRemoteResult(String diagnostic, String agent, String host) {
63 return createRandomRemoteResult(diagnostic, agent, host, true, null);
64 }
65
66 protected RemoteResult createRandomRemoteResult(String diagnostic, String agent, String host, boolean succeeded, String message) {
67 int duration = random.nextInt(5000);
68 long startTime = nextStartTime + random.nextInt(1000);
69 long finishTime = startTime + duration;
70 nextStartTime = finishTime;
71 return new DefaultRemoteResult(guidGenerator.nextGuid(),
72 new DefaultResultInfo(diagnostic, succeeded ? ResultState.SUCCEEDED : ResultState.FAILED, duration, message == null ? null : new DefaultDiagnosticMessage(message), startTime, finishTime), agent, host, sequenceNumber++);
73 }
74
75 public void init() throws RemoteResultException {
76
77 system.setMax(maxResults);
78
79 if(hosts == null) {
80 hosts = Arrays.asList(new String[] {
81 "bart", "homer", "marge", "apu", "moe",
82 "lisa", "cletus", "carl", "chiefwiggum", "comicbookguy"
83 });
84 }
85 if(agents == null) {
86 agents = Arrays.asList(new String[] {
87 "billing", "crm", "messaging",
88 "toaster", "webserver", "build", "feeds", "site",
89 "content", "database",
90 "ledger", "financials", "tracking", "cardservices"
91 });
92 }
93 if(diagnostics == null) {
94 diagnostics = Arrays.asList(new String[] {
95 "FailingDiagnostic", "HibernateDiagnostic", "SucceedingDiagnostic", "DatasourceDiagnostic", "PerformanceDiagnostic",
96 "ConnectionDiagnostic", "SessionDiagnostic",
97 "TestDiagnostic", "WorkerThreadDiagnostic", "MemoryDiagnostic", "DiskspaceDiagnositc",
98 "LoginDiagnostic", "MaxUsersDiagnostic", "MaxWaitDiagnostic",
99 "SecurityDiagnostic", "LatencyDiagnostic", "TimerDiagnostic",
100 "ProcessDiagnostic",
101 });
102 }
103 if(messages == null) {
104 messages = Arrays.asList(new String[] {
105 "Response time too slow", "NullPointerException",
106 "Connection pool exhausted", "Cannot connect",
107 "Max connections", "Unknown error",
108 "Driver failure", "Could not clean up resources",
109 "OutOfMemoryException", "Failure in subsystem"
110 });
111 }
112 for(int i = 0; i < maxResults; i++) {
113 double percentage = random.nextDouble() * 100;
114 if(percentage < failurePercentage) {
115 system.addRemoteResult(createRandomFailingRemoteResult((String) diagnostics.get(random.nextInt(diagnostics.size())),
116 (String) agents.get(random.nextInt(agents.size())),
117 (String) hosts.get(random.nextInt(hosts.size())) + "." + domain,
118 (String) messages.get(random.nextInt(messages.size()))));
119 } else {
120 system.addRemoteResult(createRandomSucceedingRemoteResult((String) diagnostics.get(random.nextInt(diagnostics.size())),
121 (String) agents.get(random.nextInt(agents.size())),
122 (String) hosts.get(random.nextInt(hosts.size())) + "." + domain));
123 }
124 }
125 }
126
127 /***
128 * @param name
129 * @return
130 */
131 public RemoteAgent getAgent(String name) {
132 return system.getAgent(name);
133 }
134 /***
135 * @return
136 */
137 public Iterator getAgents() {
138 return system.getAgents();
139 }
140 /***
141 * @param name
142 * @return
143 */
144 public RemoteHost getHost(String name) {
145 return system.getHost(name);
146 }
147 /***
148 * @return
149 */
150 public Iterator getHosts() {
151 return system.getHosts();
152 }
153 /***
154 * @return
155 */
156 public Iterator getResults() {
157 return system.getResults();
158 }
159 /***
160 * @param diagnostics The diagnostics to set.
161 */
162 public void setMockDiagnostics(List diagnostics) {
163 this.diagnostics = diagnostics;
164 }
165
166 public void setMockAgents(List agents) {
167 this.agents = agents;
168 }
169
170 public void setMockHosts(List hosts) {
171 this.hosts = hosts;
172 }
173
174 /***
175 * @param domain The domain to set.
176 */
177 public void setDomain(String domain) {
178 this.domain = domain;
179 }
180 /***
181 * @param failurePercentage The failurePercentage to set.
182 */
183 public void setFailurePercentage(int failurePercentage) {
184 this.failurePercentage = failurePercentage;
185 }
186 /***
187 * @param guidGenerator The guidGenerator to set.
188 */
189 public void setGuidGenerator(GuidGenerator guidGenerator) {
190 this.guidGenerator = guidGenerator;
191 }
192 /***
193 * @param maxResults The maxResults to set.
194 */
195 public void setMaxResults(int maxResults) {
196 this.maxResults = maxResults;
197 }
198 /***
199 * @param messages The messages to set.
200 */
201 public void setMessages(List messages) {
202 this.messages = messages;
203 }
204 /***
205 * @param nextStartTime The nextStartTime to set.
206 */
207 public void setNextStartTime(long nextStartTime) {
208 this.nextStartTime = nextStartTime;
209 }
210 /***
211 * @param random The random to set.
212 */
213 public void setRandom(Random random) {
214 this.random = random;
215 }
216 /***
217 * @param system The system to set.
218 */
219 public void setSystem(InMemoryRemoteSystem system) {
220 this.system = system;
221 }
222
223
224
225
226 public RemoteResult getResult(String guid) {
227 return this.system.getResult(guid);
228 }
229 }