View Javadoc

1   /*
2    * $Id: AbstractLoanBrokerApp.java 7976 2007-08-21 14:26:13Z 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          MuleManager.getInstance().dispose();
79      }
80  
81      protected void run(boolean synchronous) throws Exception
82      {
83          int response = 0;
84          while (response != 'q')
85          {
86              System.out.println("\n" + LocaleMessage.menu());
87  
88              response = readCharacter();
89  
90              switch (response)
91              {
92                  case '1' :
93                  {
94                      CustomerQuoteRequest request = getRequestFromUser();
95                      request(request, synchronous);
96                      break;
97                  }
98  
99                  case '2' :
100                 {
101                     sendRandomRequests(100, synchronous);
102                     break;
103                 }
104 
105                 case '3' :
106                 {
107                     System.out.println(LocaleMessage.menuOptionNumberOfRequests());
108                     int number = readInt();
109                     if (number < 1)
110                     {
111                         System.out.println(LocaleMessage.menuErrorNumberOfRequests());
112                     }
113                     else
114                     {
115                         sendRandomRequests(number, synchronous);
116                     }
117                     break;
118                 }
119 
120                 case 'q' :
121                 {
122                     System.out.println(LocaleMessage.exiting());
123                     dispose();
124                     System.exit(0);
125                 }
126 
127                 default :
128                 {
129                     System.out.println(LocaleMessage.menuError());
130                 }
131             }
132         }
133     }
134     
135     public CustomerQuoteRequest createRequest()
136     {
137         int index = new Double(Math.random() * 10).intValue();
138         Customer c = (Customer)customers.get(index);
139 
140         return new CustomerQuoteRequest(c, getRandomAmount(), getRandomDuration());
141     }
142 
143     protected static CustomerQuoteRequest getRequestFromUser() throws IOException
144     {
145         byte[] buf = new byte[128];
146         System.out.print(LocaleMessage.enterName());
147         System.in.read(buf);
148         String name = new String(buf).trim();
149         System.out.print(LocaleMessage.enterLoanAmount());
150         buf = new byte[16];
151         System.in.read(buf);
152         String amount = new String(buf).trim();
153         System.out.print(LocaleMessage.enterLoanDuration());
154         buf = new byte[16];
155         System.in.read(buf);
156         String duration = new String(buf).trim();
157 
158         int d = 0;
159         try
160         {
161             d = Integer.parseInt(duration);
162         }
163         catch (NumberFormatException e)
164         {
165             System.out.println(LocaleMessage.loanDurationError(duration));
166             d = getRandomDuration();
167         }
168 
169         double a = 0;
170         try
171         {
172             a = Double.valueOf(amount).doubleValue();
173         }
174         catch (NumberFormatException e)
175         {
176             System.out.println(LocaleMessage.loanAmountError(amount));
177             a = getRandomAmount();
178         }
179 
180         Customer c = new Customer(name, getRandomSsn());
181         CustomerQuoteRequest request = new CustomerQuoteRequest(c, a, d);
182         return request;
183     }
184 
185     public void request(CustomerQuoteRequest request, boolean sync) throws Exception
186     {
187         if (!sync)
188         {
189             client.dispatch("CustomerRequests", request, null);
190             System.out.println(LocaleMessage.sentAsync());
191             // let the request catch up
192             Thread.sleep(1500);
193         }
194         else
195         {
196             UMOMessage result = client.send("CustomerRequests", request, null);
197             if (result == null)
198             {
199                 System.out.println(LocaleMessage.requestError());
200             }
201             else
202             {
203                 System.out.println(LocaleMessage.requestResponse(result.getPayload()));
204             }
205         }
206     }
207 
208     public void requestDispatch(int number, String endpoint) throws Exception
209     {
210         for (int i = 0; i < number; i++)
211         {
212             client.dispatch(endpoint, createRequest(), null);
213         }
214     }
215 
216     public List requestSend(int number, String endpoint) throws Exception
217     {
218         return requestSend(number, endpoint, null);
219     }
220     
221     public List requestSend(int number, String endpoint, Map properties) throws Exception
222     {
223         List results = new ArrayList(number);
224         UMOMessage result;
225         for (int i = 0; i < number; i++)
226         {
227             result = client.send(endpoint, createRequest(), properties);
228             if (result != null)
229             {
230                 results.add(result.getPayload());
231             }
232         }
233         return results;
234     }
235 
236     protected void sendRandomRequests(int number, boolean synchronous) throws Exception
237     {
238         if (synchronous)
239         {
240             List list = this.requestSend(number, "CustomerRequests");
241             int i = 1;
242             for (Iterator iterator = list.iterator(); iterator.hasNext(); i++)
243             {
244                 System.out.println(
245                     LocaleMessage.request(i, iterator.next().toString()));
246             }
247         }
248         else
249         {
250             this.requestDispatch(number, "CustomerRequests");
251         }
252     }
253 
254     protected static int readCharacter() throws IOException
255     {
256         byte[] buf = new byte[16];
257         System.in.read(buf);
258         return buf[0];
259     }
260 
261     protected static String readString() throws IOException
262     {
263         byte[] buf = new byte[80];
264         System.in.read(buf);
265         return new String(buf).trim();
266     }
267 
268     protected static int readInt() throws IOException
269     {
270         try
271         {
272             return Integer.parseInt(readString());
273         }
274         catch (NumberFormatException nfex)
275         {
276             return 0;
277         }
278     }
279 
280     protected static double getRandomAmount()
281     {
282         return Math.round(Math.random() * 18000);
283     }
284 
285     protected static int getRandomDuration()
286     {
287         return new Double(Math.random() * 60).intValue();
288     }
289 
290     protected static int getRandomSsn()
291     {
292         return new Double(Math.random() * 6000).intValue();
293     }
294 }
295