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.esn;
8   
9   import org.mule.example.loanbroker.AbstractLoanBrokerApp;
10  import org.mule.example.loanbroker.LocaleMessage;
11  import org.mule.example.loanbroker.messages.LoanQuote;
12  import org.mule.util.DateUtils;
13  import org.mule.util.StringMessageUtils;
14  import org.mule.util.StringUtils;
15  import org.mule.util.SystemUtils;
16  
17  import java.io.IOException;
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Runs the LoanBroker ESN example application.
25   */
26  public class LoanBrokerApp extends AbstractLoanBrokerApp
27  {
28      public static final String CLI_OPTIONS[][] = {
29              { "config", "true", "Configuration File(s)" },
30              { "main", "true", "LoanBrokerApp Class"},
31              { "req", "true", "Number of loan requests to use"},
32              { "sync", "true", "Whether to run in synchronous mode or not"}
33          };
34  
35      private static boolean synchronous = false;
36  
37      // Needed for webapp version!
38      // TODO Travis ... sadly, it doesn't quite work
39      public LoanBrokerApp() throws Exception
40      {
41          super();
42      }
43  
44      public LoanBrokerApp(String config) throws Exception
45      {
46          super(config);
47      }
48  
49      public static void main(String[] args) throws Exception
50      {
51          LoanBrokerApp loanBrokerApp = null;
52          
53          /////////////////////////////////////////
54          // Command-line config
55          /////////////////////////////////////////
56          Map<String, Object> options = SystemUtils.getCommandLineOptions(args, CLI_OPTIONS);
57          String config = (String)options.get("config");
58          if (StringUtils.isNotBlank(config))
59          {
60              loanBrokerApp = new LoanBrokerApp(config);
61  
62              int i = 100;
63              String requests = (String)options.get("req");
64              if (requests != null)
65              {
66                  i = Integer.parseInt(requests);
67              }
68  
69              String sync = (String)options.get("sync");
70              if (sync != null)
71              {
72                  synchronous = Boolean.valueOf(sync).booleanValue();
73              }
74  
75              if (synchronous)
76              {
77                  long start = System.currentTimeMillis();
78                  List<Object> results = loanBrokerApp.requestSend(i, "CustomerRequests");
79                  System.out.println(LocaleMessage.responseNumQuotes(results.size()));
80                  List<String> output = new ArrayList<String>(results.size());
81                  int x = 1;
82                  for (Iterator<Object> iterator = results.iterator(); iterator.hasNext(); x++)
83                  {
84                      LoanQuote quote = (LoanQuote) iterator.next();
85                      output.add(x + ". " + quote.toString());
86                  }
87                  
88                  System.out.println(StringMessageUtils.getBoilerPlate(output, '*', 80));
89                  long cur = System.currentTimeMillis();
90                  System.out.println(DateUtils.getFormattedDuration(cur - start));
91                  System.out.println(LocaleMessage.responseAvgRequest(((cur - start) / x)));
92              }
93              else
94              {
95                  loanBrokerApp.requestDispatch(i, "CustomerRequests");
96              }
97          }
98          /////////////////////////////////////////
99          // Interactive config
100         /////////////////////////////////////////
101         else
102         {
103             loanBrokerApp = new LoanBrokerApp(getInteractiveConfig());
104             loanBrokerApp.run(synchronous);
105         }
106     }
107 
108     protected static String getInteractiveConfig() throws IOException
109     {
110         System.out.println(StringMessageUtils.getBoilerPlate(LocaleMessage.welcome()));
111                     
112         int response = 0;
113         String mode = null;
114         while (response != 'a' && response != 's')
115         {
116             System.out.println("\n" + LocaleMessage.menuOptionMode());
117             response = readCharacter();
118             switch (response)
119             {
120                 case 'a' :
121                 {
122                     System.out.println(LocaleMessage.loadingAsync());
123                     mode = "async";
124                     break;
125                 }
126 
127                 case 's' :
128                 {
129                     System.out.println(LocaleMessage.loadingSync());
130                     mode = "sync";
131                     break;
132                 }
133             }
134         }
135 
136         String config = "loan-broker-" + mode + "-config.xml, loan-broker-cxf-endpoints-config.xml";
137         return config;
138     }
139 }