View Javadoc

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