View Javadoc

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