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.example.loanbroker.messages.CreditProfile;
10  import org.mule.example.loanbroker.messages.Customer;
11  import org.mule.example.loanbroker.messages.CustomerQuoteRequest;
12  import org.mule.example.loanbroker.messages.LoanBrokerQuoteRequest;
13  import org.mule.example.loanbroker.messages.LoanQuote;
14  
15  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * <code>LoanBroker</code> is the Service that starts the loan request process. The
21   * broker also receives the final quote.
22   */
23  public class DefaultLoanBroker implements LoanBrokerService
24  {
25      /**
26       * logger used by this class
27       */
28      protected final Log logger = LogFactory.getLog(getClass());
29      
30      private final AtomicInteger quotes = new AtomicInteger(0);
31      private final AtomicInteger requests = new AtomicInteger(0);
32      private final AtomicInteger profiles = new AtomicInteger(0);
33  
34      public Object getLoanQuote(CustomerQuoteRequest request) throws LoanBrokerException
35      {
36          int requests = incRequests();
37          if (logger.isInfoEnabled())
38          {
39              String[] params = new String[] { String.valueOf(requests), 
40                  request.getCustomer().getName(), 
41                  String.valueOf(request.getCustomer().getSsn()), 
42                  String.valueOf(request.getLoanAmount()),
43                  String.valueOf(request.getLoanDuration()) };
44  
45              logger.info("\n***** " + LocaleMessage.receivedRequest(params));
46          }
47          return request;
48      }
49  
50      public LoanBrokerQuoteRequest receiveLoanBrokerQuoteRequest(LoanBrokerQuoteRequest request)
51      {
52          // Just pass through
53          return request;
54      }
55  
56      public Customer receiveCustomer(Customer customer)
57      {
58          // Just pass through
59          return customer;
60      }
61  
62      public Object receiveCreditProfile(CreditProfile profile)
63      {
64          int profiles = incProfiles();
65          if (logger.isInfoEnabled())
66          {
67              String[] params = new String[] { String.valueOf(profiles), 
68                  String.valueOf(profile.getCreditScore()),
69                  String.valueOf(profile.getCreditHistory()) };
70  
71              logger.info("\n***** " + LocaleMessage.receivedProfile(params));
72          }
73          return profile;
74      }
75  
76      public Object receiveQuote(LoanQuote quote)
77      {
78          int quotes = incQuotes();
79          if (logger.isInfoEnabled())
80          {
81              String[] params = new String[] { String.valueOf(quotes), 
82                  quote.toString() };
83              logger.info("\n***** " + LocaleMessage.receivedQuote(params));
84          }
85          return quote;
86      }
87  
88      protected int incQuotes()
89      {
90          return quotes.incrementAndGet();
91      }
92  
93      protected int incRequests()
94      {
95          return requests.incrementAndGet();
96      }
97  
98      protected int incProfiles()
99      {
100         return profiles.incrementAndGet();
101     }
102 }