KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > loanbroker > LoanBroker


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
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 JavaDoc(Constants.LOANBROKER_NS, Constants.LOANBROKER_SERVICE), "input");
47     }
48     
49     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
50         // Provider role
51
if (exchange.getRole() == Role.PROVIDER) {
52             processInputRequest(exchange);
53         // Consumer role
54
} else {
55             ServiceEndpoint ep = exchange.getEndpoint();
56             // Credit agency response
57
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         // Get aggregation
70
String JavaDoc id = (String JavaDoc) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
71         Aggregation ag = (Aggregation) aggregations.get(id);
72         // Get info from quote
73
LoanQuote q = new LoanQuote();
74         q.bank = exchange.getEndpoint().getServiceName().getLocalPart();
75         q.rate = (Double JavaDoc) getOutProperty(exchange, Constants.PROPERTY_RATE);
76         done(exchange);
77         // Check if all quotes are received
78
synchronized (ag) {
79             ag.quotes.add(q);
80             if (ag.quotes.size() == ag.numbers) {
81                 LoanQuote best = null;
82                 for (Iterator JavaDoc 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         // Get aggregation
101
String JavaDoc id = (String JavaDoc) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
102         Aggregation ag = (Aggregation) aggregations.get(id);
103         QName JavaDoc[] recipients = (QName JavaDoc[]) 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         // Get aggregation
123
String JavaDoc id = (String JavaDoc) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
124         Aggregation ag = (Aggregation) aggregations.get(id);
125         // Fill with infos
126
ag.score = (Integer JavaDoc) getOutProperty(exchange, Constants.PROPERTY_SCORE);
127         ag.hlength = (Integer JavaDoc) getOutProperty(exchange, Constants.PROPERTY_HISTORYLENGTH);
128         // Send to lender gateway
129
InOut inout = createInOutExchange(new QName JavaDoc(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             // Create aggregation
144
String JavaDoc id = exchange.getExchangeId();
145             Aggregation ag = new Aggregation();
146             ag.request = exchange;
147             ag.ssn = (String JavaDoc) getInProperty(exchange, Constants.PROPERTY_SSN);
148             ag.amount = (Double JavaDoc) getInProperty(exchange, Constants.PROPERTY_AMOUNT);
149             ag.duration = (Integer JavaDoc) getInProperty(exchange, Constants.PROPERTY_DURATION);
150             aggregations.put(id, ag);
151             
152             InOut inout = createInOutExchange(new QName JavaDoc(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 JavaDoc getProperty(MessageExchange me, String JavaDoc name) {
162         return me.getProperty(name);
163     }
164     
165     protected Object JavaDoc getInProperty(MessageExchange me, String JavaDoc name) {
166         return me.getMessage("in").getProperty(name);
167     }
168     
169     protected Object JavaDoc getOutProperty(MessageExchange me, String JavaDoc 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 JavaDoc ssn;
179         public Double JavaDoc amount;
180         public Integer JavaDoc duration;
181         public Integer JavaDoc score;
182         public Integer JavaDoc hlength;
183         public List JavaDoc quotes = new ArrayList JavaDoc();
184     }
185     
186     public static class LoanQuote {
187         public String JavaDoc bank;
188         public Double JavaDoc rate;
189     }
190
191 }
192
Popular Tags