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