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