View Javadoc

1   /*
2    * $Id: Bank.java 10790 2008-02-12 20:53:32Z dfeist $
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.example.loanbroker.bank;
12  
13  import org.mule.api.config.ConfigurationException;
14  import org.mule.api.endpoint.ImmutableEndpoint;
15  import org.mule.api.service.Service;
16  import org.mule.api.service.ServiceAware;
17  import org.mule.config.i18n.MessageFactory;
18  import org.mule.example.loanbroker.LocaleMessage;
19  import org.mule.example.loanbroker.messages.LoanBrokerQuoteRequest;
20  import org.mule.example.loanbroker.messages.LoanQuote;
21  
22  import java.io.Serializable;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * <code>Bank</code> is a representation of a bank from which to obtain loan
30   * quotes.
31   */
32  
33  public class Bank implements ServiceAware, Serializable, BankService
34  {
35      /**
36       * Serial version
37       */
38      private static final long serialVersionUID = 4108271137166107769L;
39  
40      /**
41       * logger used by this class
42       */
43      protected static final Log logger = LogFactory.getLog(Bank.class);
44  
45      private String bankName;
46      private double primeRate;
47      
48      /**
49       * Incoming endpoint for the bank, this is used to create a static recipient list based on a list of banks.
50       */
51      private String endpoint;
52  
53      public Bank()
54      {
55          this.primeRate = Math.random() * 10;
56      }
57  
58      public Bank(String bankname)
59      {
60          this();
61          this.bankName = bankname;
62  
63          // For simplicity, the endpoint for the bank is the same as the bank's name.
64          this.endpoint = bankName;
65      }
66  
67      // TODO This method doesn't help us with the Static Recipient list because the list of banks is created 
68      // programatically in DefaultLenderService (they should be looked up from the config/registry).
69      public void setService(Service service) throws ConfigurationException 
70      {
71          this.bankName = service.getName(); 
72  
73          List endpoints = service.getInboundRouter().getEndpoints();
74          if ((endpoints == null) || (endpoints.size() != 1))
75          {
76              throw new ConfigurationException(MessageFactory.createStaticMessage("Bank is expected to have exactly 1 incoming endpoint."));
77          }
78          // TODO This gives us the endpoint the bank is listening on, but the endpoint for sending to the bank 
79          // is different in the ESB config ("Bank1In" vs. "Bank1")
80          this.endpoint = ((ImmutableEndpoint) endpoints.get(0)).getName();
81      }
82  
83      public LoanQuote getLoanQuote(LoanBrokerQuoteRequest request)
84      {
85          LoanQuote quote = new LoanQuote();
86          quote.setBankName(getBankName());
87          quote.setInterestRate(primeRate);
88          logger.info(LocaleMessage.receivedRate(quote));
89  
90          return quote;
91      }
92  
93      public String getBankName()
94      {
95          return bankName;
96      }
97  
98      public void setBankName(String bankName)
99      {
100         this.bankName = bankName;
101     }
102 
103     public double getPrimeRate()
104     {
105         return primeRate;
106     }
107 
108     public void setPrimeRate(double primeRate)
109     {
110         this.primeRate = primeRate;
111     }
112 
113     public String getEndpoint()
114     {
115         return endpoint;
116     }
117     public void setEndpoint(String endpoint)
118     {
119         this.endpoint = endpoint;
120     }
121 }