1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.example.loanbroker; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.config.ConfigurationBuilder; |
16 | |
import org.mule.api.context.MuleContextFactory; |
17 | |
import org.mule.config.spring.SpringXmlConfigurationBuilder; |
18 | |
import org.mule.context.DefaultMuleContextFactory; |
19 | |
import org.mule.example.loanbroker.messages.Customer; |
20 | |
import org.mule.example.loanbroker.messages.CustomerQuoteRequest; |
21 | |
import org.mule.module.client.MuleClient; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public abstract class AbstractLoanBrokerApp |
33 | |
{ |
34 | 0 | private List<Customer> customers = new ArrayList<Customer>(); |
35 | 0 | private MuleClient client = null; |
36 | |
private String config; |
37 | |
|
38 | |
public AbstractLoanBrokerApp() throws Exception |
39 | 0 | { |
40 | 0 | this.config = null; |
41 | 0 | init(); |
42 | 0 | } |
43 | |
|
44 | |
public AbstractLoanBrokerApp(String config) throws Exception |
45 | 0 | { |
46 | 0 | this.config = config; |
47 | 0 | init(); |
48 | 0 | } |
49 | |
|
50 | |
protected void init() throws Exception |
51 | |
{ |
52 | 0 | if (config != null) |
53 | |
{ |
54 | 0 | MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); |
55 | 0 | muleContextFactory.createMuleContext(getConfigBuilder()); |
56 | |
} |
57 | |
|
58 | 0 | client = new MuleClient(); |
59 | |
|
60 | 0 | customers.add(new Customer("Jenson Button", 123)); |
61 | 0 | customers.add(new Customer("Michael Schumacker", 456)); |
62 | 0 | customers.add(new Customer("Juan Pablo Montoya", 789)); |
63 | 0 | customers.add(new Customer("David Colthard", 101)); |
64 | 0 | customers.add(new Customer("Rubens Barrichello", 112)); |
65 | 0 | customers.add(new Customer("Mark Webber", 131)); |
66 | 0 | customers.add(new Customer("Takuma Sato", 415)); |
67 | 0 | customers.add(new Customer("Kimi Raikkonen", 161)); |
68 | 0 | customers.add(new Customer("Ralf Schumacher", 718)); |
69 | 0 | customers.add(new Customer("Jarno Trulli", 192)); |
70 | 0 | } |
71 | |
|
72 | |
protected ConfigurationBuilder getConfigBuilder() throws MuleException |
73 | |
{ |
74 | 0 | return new SpringXmlConfigurationBuilder(config); |
75 | |
} |
76 | |
|
77 | |
protected void dispose() throws Exception |
78 | |
{ |
79 | 0 | client.dispose(); |
80 | 0 | } |
81 | |
|
82 | |
protected void run(boolean synchronous) throws Exception |
83 | |
{ |
84 | 0 | int response = 0; |
85 | 0 | while (response != 'q') |
86 | |
{ |
87 | 0 | System.out.println("\n" + LocaleMessage.menu()); |
88 | |
|
89 | 0 | response = readCharacter(); |
90 | |
|
91 | 0 | switch (response) |
92 | |
{ |
93 | |
case '1' : |
94 | |
{ |
95 | 0 | CustomerQuoteRequest request = getRequestFromUser(); |
96 | 0 | request(request, synchronous); |
97 | 0 | break; |
98 | |
} |
99 | |
|
100 | |
case '2' : |
101 | |
{ |
102 | 0 | sendRandomRequests(100, synchronous); |
103 | 0 | break; |
104 | |
} |
105 | |
|
106 | |
case '3' : |
107 | |
{ |
108 | 0 | System.out.println(LocaleMessage.menuOptionNumberOfRequests()); |
109 | 0 | int number = readInt(); |
110 | 0 | if (number < 1) |
111 | |
{ |
112 | 0 | System.out.println(LocaleMessage.menuErrorNumberOfRequests()); |
113 | |
} |
114 | |
else |
115 | |
{ |
116 | 0 | sendRandomRequests(number, synchronous); |
117 | |
} |
118 | 0 | break; |
119 | |
} |
120 | |
|
121 | |
case 'q' : |
122 | |
{ |
123 | 0 | System.out.println(LocaleMessage.exiting()); |
124 | 0 | dispose(); |
125 | 0 | System.exit(0); |
126 | |
} |
127 | |
|
128 | |
default : |
129 | |
{ |
130 | 0 | System.out.println(LocaleMessage.menuError()); |
131 | |
} |
132 | |
} |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
public CustomerQuoteRequest createRequest() |
137 | |
{ |
138 | 0 | int index = new Double(Math.random() * 10).intValue(); |
139 | 0 | Customer c = customers.get(index); |
140 | |
|
141 | 0 | return new CustomerQuoteRequest(c, getRandomAmount(), getRandomDuration()); |
142 | |
} |
143 | |
|
144 | |
protected static CustomerQuoteRequest getRequestFromUser() throws IOException |
145 | |
{ |
146 | 0 | byte[] buf = new byte[128]; |
147 | 0 | System.out.print(LocaleMessage.enterName()); |
148 | 0 | System.in.read(buf); |
149 | 0 | String name = new String(buf).trim(); |
150 | 0 | System.out.print(LocaleMessage.enterLoanAmount()); |
151 | 0 | buf = new byte[16]; |
152 | 0 | System.in.read(buf); |
153 | 0 | String amount = new String(buf).trim(); |
154 | 0 | System.out.print(LocaleMessage.enterLoanDuration()); |
155 | 0 | buf = new byte[16]; |
156 | 0 | System.in.read(buf); |
157 | 0 | String duration = new String(buf).trim(); |
158 | |
|
159 | 0 | int d = 0; |
160 | |
try |
161 | |
{ |
162 | 0 | d = Integer.parseInt(duration); |
163 | |
} |
164 | 0 | catch (NumberFormatException e) |
165 | |
{ |
166 | 0 | System.out.println(LocaleMessage.loanDurationError(duration)); |
167 | 0 | d = getRandomDuration(); |
168 | 0 | } |
169 | |
|
170 | 0 | double a = 0; |
171 | |
try |
172 | |
{ |
173 | 0 | a = Double.valueOf(amount).doubleValue(); |
174 | |
} |
175 | 0 | catch (NumberFormatException e) |
176 | |
{ |
177 | 0 | System.out.println(LocaleMessage.loanAmountError(amount)); |
178 | 0 | a = getRandomAmount(); |
179 | 0 | } |
180 | |
|
181 | 0 | Customer c = new Customer(name, getRandomSsn()); |
182 | 0 | CustomerQuoteRequest request = new CustomerQuoteRequest(c, a, d); |
183 | 0 | return request; |
184 | |
} |
185 | |
|
186 | |
public void request(CustomerQuoteRequest request, boolean sync) throws Exception |
187 | |
{ |
188 | 0 | if (!sync) |
189 | |
{ |
190 | 0 | client.dispatch("CustomerRequests", request, null); |
191 | 0 | System.out.println(LocaleMessage.sentAsync()); |
192 | |
|
193 | 0 | Thread.sleep(1500); |
194 | |
} |
195 | |
else |
196 | |
{ |
197 | 0 | MuleMessage result = client.send("CustomerRequests", request, null); |
198 | 0 | if (result == null) |
199 | |
{ |
200 | 0 | System.out.println(LocaleMessage.requestError()); |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 0 | System.out.println(LocaleMessage.requestResponse(result.getPayload())); |
205 | |
} |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
public void requestDispatch(int number, String endpoint) throws Exception |
210 | |
{ |
211 | 0 | for (int i = 0; i < number; i++) |
212 | |
{ |
213 | 0 | client.dispatch(endpoint, createRequest(), null); |
214 | |
} |
215 | 0 | } |
216 | |
|
217 | |
public List requestSend(int number, String endpoint) throws Exception |
218 | |
{ |
219 | 0 | return requestSend(number, endpoint, null); |
220 | |
} |
221 | |
|
222 | |
public List requestSend(int number, String endpoint, Map properties) throws Exception |
223 | |
{ |
224 | 0 | List results = new ArrayList(number); |
225 | |
MuleMessage result; |
226 | 0 | for (int i = 0; i < number; i++) |
227 | |
{ |
228 | 0 | result = client.send(endpoint, createRequest(), properties); |
229 | 0 | if (result != null) |
230 | |
{ |
231 | 0 | results.add(result.getPayload()); |
232 | |
} |
233 | |
} |
234 | 0 | return results; |
235 | |
} |
236 | |
|
237 | |
protected void sendRandomRequests(int number, boolean synchronous) throws Exception |
238 | |
{ |
239 | 0 | if (synchronous) |
240 | |
{ |
241 | 0 | List list = this.requestSend(number, "CustomerRequests"); |
242 | 0 | int i = 1; |
243 | 0 | for (Iterator iterator = list.iterator(); iterator.hasNext(); i++) |
244 | |
{ |
245 | 0 | System.out.println( |
246 | |
LocaleMessage.request(i, iterator.next().toString())); |
247 | |
} |
248 | 0 | } |
249 | |
else |
250 | |
{ |
251 | 0 | this.requestDispatch(number, "CustomerRequests"); |
252 | |
} |
253 | 0 | } |
254 | |
|
255 | |
protected static int readCharacter() throws IOException |
256 | |
{ |
257 | 0 | byte[] buf = new byte[16]; |
258 | 0 | System.in.read(buf); |
259 | 0 | return buf[0]; |
260 | |
} |
261 | |
|
262 | |
protected static String readString() throws IOException |
263 | |
{ |
264 | 0 | byte[] buf = new byte[80]; |
265 | 0 | System.in.read(buf); |
266 | 0 | return new String(buf).trim(); |
267 | |
} |
268 | |
|
269 | |
protected static int readInt() throws IOException |
270 | |
{ |
271 | |
try |
272 | |
{ |
273 | 0 | return Integer.parseInt(readString()); |
274 | |
} |
275 | 0 | catch (NumberFormatException nfex) |
276 | |
{ |
277 | 0 | return 0; |
278 | |
} |
279 | |
} |
280 | |
|
281 | |
protected static double getRandomAmount() |
282 | |
{ |
283 | 0 | return Math.round(Math.random() * 18000); |
284 | |
} |
285 | |
|
286 | |
protected static int getRandomDuration() |
287 | |
{ |
288 | 0 | return new Double(Math.random() * 60).intValue(); |
289 | |
} |
290 | |
|
291 | |
protected static int getRandomSsn() |
292 | |
{ |
293 | 0 | return new Double(Math.random() * 6000).intValue(); |
294 | |
} |
295 | |
} |