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