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