1 18 package loanbroker; 19 20 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.servicemix.MessageExchangeListener; 25 import org.apache.servicemix.components.util.ComponentSupport; 26 27 import javax.jbi.messaging.ExchangeStatus; 28 import javax.jbi.messaging.InOut; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.messaging.MessagingException; 31 import javax.jbi.messaging.NormalizedMessage; 32 import javax.jbi.messaging.MessageExchange.Role; 33 import javax.jbi.servicedesc.ServiceEndpoint; 34 import javax.xml.namespace.QName ; 35 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 public class LoanBroker extends ComponentSupport implements MessageExchangeListener { 42 43 private static final Log log = LogFactory.getLog(LoanBroker.class); 44 45 public LoanBroker() { 46 super(new QName (Constants.LOANBROKER_NS, Constants.LOANBROKER_SERVICE), "input"); 47 } 48 49 public void onMessageExchange(MessageExchange exchange) throws MessagingException { 50 if (exchange.getRole() == Role.PROVIDER) { 52 processInputRequest(exchange); 53 } else { 55 ServiceEndpoint ep = exchange.getEndpoint(); 56 if (ep.getServiceName().getLocalPart().equals(Constants.CREDITAGENCY_SERVICE)) { 58 processCreditAgencyResponse(exchange); 59 } else if (ep.getServiceName().getLocalPart().equals(Constants.LENDERGATEWAY_SERVICE)) { 60 processLenderGatewayResponse(exchange); 61 } else { 62 processLoanQuote(exchange); 63 } 64 } 65 } 66 67 private void processLoanQuote(MessageExchange exchange) throws MessagingException { 68 log.info("Receiving loan quote"); 69 String id = (String ) getProperty(exchange, Constants.PROPERTY_CORRELATIONID); 71 Aggregation ag = (Aggregation) aggregations.get(id); 72 LoanQuote q = new LoanQuote(); 74 q.bank = exchange.getEndpoint().getServiceName().getLocalPart(); 75 q.rate = (Double ) getOutProperty(exchange, Constants.PROPERTY_RATE); 76 done(exchange); 77 synchronized (ag) { 79 ag.quotes.add(q); 80 if (ag.quotes.size() == ag.numbers) { 81 LoanQuote best = null; 82 for (Iterator iter = ag.quotes.iterator(); iter.hasNext();) { 83 q = (LoanQuote) iter.next(); 84 if (best == null || q.rate.doubleValue() < best.rate.doubleValue()) { 85 best = q; 86 } 87 } 88 NormalizedMessage response = ag.request.createMessage(); 89 response.setProperty(Constants.PROPERTY_RATE, best.rate); 90 response.setProperty(Constants.PROPERTY_BANK, best.bank); 91 ag.request.setMessage(response, "out"); 92 send(ag.request); 93 aggregations.remove(id); 94 } 95 } 96 } 97 98 private void processLenderGatewayResponse(MessageExchange exchange) throws MessagingException { 99 log.info("Receiving lender gateway response"); 100 String id = (String ) getProperty(exchange, Constants.PROPERTY_CORRELATIONID); 102 Aggregation ag = (Aggregation) aggregations.get(id); 103 QName [] recipients = (QName []) getOutProperty(exchange, Constants.PROPERTY_RECIPIENTS); 104 ag.numbers = recipients.length; 105 for (int i = 0; i < recipients.length; i++) { 106 InOut inout = createInOutExchange(recipients[i], null, null); 107 inout.setProperty(Constants.PROPERTY_CORRELATIONID, id); 108 NormalizedMessage msg = inout.createMessage(); 109 msg.setProperty(Constants.PROPERTY_SSN, ag.ssn); 110 msg.setProperty(Constants.PROPERTY_AMOUNT, ag.amount); 111 msg.setProperty(Constants.PROPERTY_DURATION, ag.duration); 112 msg.setProperty(Constants.PROPERTY_SCORE, ag.score); 113 msg.setProperty(Constants.PROPERTY_HISTORYLENGTH, ag.hlength); 114 inout.setInMessage(msg); 115 send(inout); 116 } 117 done(exchange); 118 } 119 120 private void processCreditAgencyResponse(MessageExchange exchange) throws MessagingException { 121 log.info("Receiving credit agency response"); 122 String id = (String ) getProperty(exchange, Constants.PROPERTY_CORRELATIONID); 124 Aggregation ag = (Aggregation) aggregations.get(id); 125 ag.score = (Integer ) getOutProperty(exchange, Constants.PROPERTY_SCORE); 127 ag.hlength = (Integer ) getOutProperty(exchange, Constants.PROPERTY_HISTORYLENGTH); 128 InOut inout = createInOutExchange(new QName (Constants.LOANBROKER_NS, Constants.LENDERGATEWAY_SERVICE), null, null); 130 inout.setProperty(Constants.PROPERTY_CORRELATIONID, id); 131 NormalizedMessage msg = inout.createMessage(); 132 msg.setProperty(Constants.PROPERTY_SCORE, ag.score); 133 msg.setProperty(Constants.PROPERTY_HISTORYLENGTH, ag.hlength); 134 msg.setProperty(Constants.PROPERTY_AMOUNT, ag.amount); 135 inout.setInMessage(msg); 136 send(inout); 137 done(exchange); 138 } 139 140 private void processInputRequest(MessageExchange exchange) throws MessagingException { 141 if (exchange.getStatus() == ExchangeStatus.ACTIVE) { 142 log.info("Receiving loan request"); 143 String id = exchange.getExchangeId(); 145 Aggregation ag = new Aggregation(); 146 ag.request = exchange; 147 ag.ssn = (String ) getInProperty(exchange, Constants.PROPERTY_SSN); 148 ag.amount = (Double ) getInProperty(exchange, Constants.PROPERTY_AMOUNT); 149 ag.duration = (Integer ) getInProperty(exchange, Constants.PROPERTY_DURATION); 150 aggregations.put(id, ag); 151 152 InOut inout = createInOutExchange(new QName (Constants.LOANBROKER_NS, Constants.CREDITAGENCY_SERVICE), null, null); 153 inout.setProperty(Constants.PROPERTY_CORRELATIONID, id); 154 NormalizedMessage msg = inout.createMessage(); 155 msg.setProperty(Constants.PROPERTY_SSN, ag.ssn); 156 inout.setInMessage(msg); 157 send(inout); 158 } 159 } 160 161 protected Object getProperty(MessageExchange me, String name) { 162 return me.getProperty(name); 163 } 164 165 protected Object getInProperty(MessageExchange me, String name) { 166 return me.getMessage("in").getProperty(name); 167 } 168 169 protected Object getOutProperty(MessageExchange me, String name) { 170 return me.getMessage("out").getProperty(name); 171 } 172 173 private Map aggregations = new ConcurrentHashMap(); 174 175 public static class Aggregation { 176 public MessageExchange request; 177 public int numbers; 178 public String ssn; 179 public Double amount; 180 public Integer duration; 181 public Integer score; 182 public Integer hlength; 183 public List quotes = new ArrayList (); 184 } 185 186 public static class LoanQuote { 187 public String bank; 188 public Double rate; 189 } 190 191 } 192 | Popular Tags |