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