View Javadoc

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