View Javadoc

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