View Javadoc

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