1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class Bank implements ServiceAware, Serializable, BankService |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
private static final long serialVersionUID = 4108271137166107769L; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | protected static final Log logger = LogFactory.getLog(Bank.class); |
43 | |
|
44 | |
private String bankName; |
45 | |
private double primeRate; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
private String endpoint; |
51 | |
|
52 | |
public Bank() |
53 | 0 | { |
54 | 0 | this.primeRate = Math.random() * 10; |
55 | 0 | } |
56 | |
|
57 | |
public Bank(String bankname) |
58 | |
{ |
59 | 0 | this(); |
60 | 0 | this.bankName = bankname; |
61 | |
|
62 | |
|
63 | 0 | this.endpoint = bankName; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
public void setService(Service service) |
69 | |
{ |
70 | 0 | this.bankName = service.getName(); |
71 | |
|
72 | 0 | if (!(service.getMessageSource() instanceof ServiceCompositeMessageSource)) |
73 | |
{ |
74 | 0 | throw new IllegalStateException("Only 'ServiceCompositeMessageSource' is supported"); |
75 | |
} |
76 | |
|
77 | 0 | List endpoints = ((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoints(); |
78 | 0 | if ((endpoints == null) || (endpoints.size() != 1)) |
79 | |
{ |
80 | 0 | throw new IllegalArgumentException("Bank is expected to have exactly 1 incoming endpoint."); |
81 | |
} |
82 | |
|
83 | |
|
84 | 0 | this.endpoint = ((ImmutableEndpoint) endpoints.get(0)).getName(); |
85 | 0 | } |
86 | |
|
87 | |
public LoanQuote getLoanQuote(LoanBrokerQuoteRequest request) |
88 | |
{ |
89 | 0 | LoanQuote quote = new LoanQuote(); |
90 | 0 | quote.setBankName(getBankName()); |
91 | 0 | quote.setInterestRate(primeRate); |
92 | 0 | logger.info(LocaleMessage.receivedRate(quote)); |
93 | |
|
94 | 0 | return quote; |
95 | |
} |
96 | |
|
97 | |
public String getBankName() |
98 | |
{ |
99 | 0 | return bankName; |
100 | |
} |
101 | |
|
102 | |
public void setBankName(String bankName) |
103 | |
{ |
104 | 0 | this.bankName = bankName; |
105 | 0 | } |
106 | |
|
107 | |
public double getPrimeRate() |
108 | |
{ |
109 | 0 | return primeRate; |
110 | |
} |
111 | |
|
112 | |
public void setPrimeRate(double primeRate) |
113 | |
{ |
114 | 0 | this.primeRate = primeRate; |
115 | 0 | } |
116 | |
|
117 | |
public String getEndpoint() |
118 | |
{ |
119 | 0 | return endpoint; |
120 | |
} |
121 | |
public void setEndpoint(String endpoint) |
122 | |
{ |
123 | 0 | this.endpoint = endpoint; |
124 | 0 | } |
125 | |
} |