KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > esb > lender > LenderService


1 /*
2  * $Id: LenderService.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.samples.loanbroker.esb.lender;
12
13 import org.mule.MuleManager;
14 import org.mule.impl.RequestContext;
15 import org.mule.routing.outbound.StaticRecipientList;
16 import org.mule.samples.loanbroker.esb.bank.Bank;
17 import org.mule.samples.loanbroker.esb.message.CreditProfile;
18 import org.mule.samples.loanbroker.esb.message.LoanQuoteRequest;
19
20 /**
21  * <code>DefaultLenderService</code> is responsible for contacting the relivant
22  * banks depending on the amount of the loan
23  *
24  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
25  * @version $Revision: 3798 $
26  */

27 public class LenderService
28 {
29     public void setLenderList(LoanQuoteRequest request)
30     {
31         Bank[] l = getLenderList(request.getCreditProfile(), new Double JavaDoc(request.getCustomerRequest()
32             .getLoanAmount()));
33         request.setLenders(l);
34     }
35
36     public Bank[] getLenderList(CreditProfile creditProfile, Double JavaDoc loanAmount)
37     {
38         Bank[] lenders;
39         if ((loanAmount.doubleValue() >= 20000))
40         {
41             lenders = new Bank[2];
42             lenders[0] = new Bank("Bank1", getEndpoint("Bank1"));
43             lenders[1] = new Bank("Bank2", getEndpoint("Bank2"));
44
45         }
46         else if (((loanAmount.doubleValue() >= 10000) && (loanAmount.doubleValue() <= 19999)))
47         {
48             lenders = new Bank[2];
49             lenders[0] = new Bank("Bank3", getEndpoint("Bank3"));
50             lenders[1] = new Bank("Bank4", getEndpoint("Bank4"));
51         }
52         else
53         {
54             lenders = new Bank[1];
55             lenders[0] = new Bank("Bank5", getEndpoint("Bank5"));
56         }
57
58         StringBuffer JavaDoc recipients = new StringBuffer JavaDoc();
59         for (int i = 0; i < lenders.length; i++)
60         {
61             recipients.append(lenders[i].getEndpoint()).append(",");
62         }
63
64         RequestContext.getEventContext().getMessage().setProperty(StaticRecipientList.RECIPIENTS_PROPERTY,
65             recipients.substring(0, recipients.length() - 1));
66         return lenders;
67     }
68
69     /**
70      * A helper method used to make it easier to configure this sample with differet
71      * endpoints for testing purposes
72      *
73      * @param name
74      * @return the endpoint for the bank
75      */

76     private String JavaDoc getEndpoint(String JavaDoc name)
77     {
78         String JavaDoc endpoint = MuleManager.getInstance().lookupEndpointIdentifier(name, null);
79         if (endpoint.startsWith("glue") || endpoint.startsWith("axis") || endpoint.startsWith("soap"))
80         {
81             int i = endpoint.indexOf('?');
82             if (i > -1)
83             {
84                 endpoint = endpoint.replaceFirst("\\?", "/" + name + "?method=getLoanQuote\\&");
85             }
86             else
87             {
88                 endpoint += "/" + name + "?method=getLoanQuote";
89             }
90         }
91         return endpoint;
92     }
93 }
94
Popular Tags