1 22 package org.objectweb.petals.demo.mortgage.workflow.orchestration; 23 24 import java.util.logging.Logger ; 25 26 import javax.jbi.messaging.DeliveryChannel; 27 import javax.jbi.messaging.ExchangeStatus; 28 import javax.jbi.messaging.InOnly; 29 import javax.jbi.messaging.InOut; 30 import javax.jbi.messaging.MessageExchange; 31 import javax.jbi.messaging.NormalizedMessage; 32 import javax.xml.namespace.QName ; 33 import javax.xml.transform.Source ; 34 35 import org.objectweb.petals.component.common.HandlingException; 36 import org.objectweb.petals.component.common.util.SourceHelper; 37 38 public class MortgageProfilingUtil { 39 40 public final static String DEFAULT_NS = "http://petals.objectweb.org/"; 41 42 public final static String EVALRATE_SERVICE = "EvalRateService"; 43 44 public final static String SENDSUMMARY_SERVICE = "SendSummaryService"; 45 46 public final static String EVALRATE_SERVICE_OP = "evaluate"; 47 48 public final static String OK_PROFILE = "ok"; 49 50 protected DeliveryChannel channel; 51 52 protected Logger l; 53 54 public MortgageProfilingUtil(DeliveryChannel channel, Logger l) { 55 super(); 56 this.channel = channel; 57 this.l = l; 58 } 59 60 protected MortgageResponse createMortgageResponse(float rate, 61 MortgageRequest request) { 62 MortgageResponse response = new MortgageResponse( 63 request.getFirstName(), request.getLastName(), rate); 64 return response; 65 } 66 67 protected float doEvalMortgageRate(String profile) throws HandlingException { 68 69 72 profile = "<profile>" + profile + "</profile>"; 73 74 String response = "RateNotDefined"; 75 try { 76 79 Source source = SourceHelper.createSource(profile); 80 81 84 MessageExchange msg = channel.createExchangeFactory() 85 .createInOutExchange(); 86 msg.setService(new QName (DEFAULT_NS, EVALRATE_SERVICE)); 87 msg.setOperation(new QName (EVALRATE_SERVICE_OP)); 88 89 92 NormalizedMessage inNM = msg.createMessage(); 93 inNM.setContent(source); 94 ((InOut) msg).setInMessage(inNM); 95 96 99 channel.sendSync(msg); 100 101 104 NormalizedMessage outNM = ((InOut) msg).getOutMessage(); 105 if (outNM != null) { 106 response = SourceHelper.createString(outNM.getContent()); 107 } 108 109 response = response.substring(response.indexOf("<response>") + 10, 111 response.indexOf("</response>")); 112 } catch (Exception e) { 113 throw new HandlingException("Error evaluating mortgage rate", e); 114 } 115 116 l.info("##### Evaluated rate : " + response + " #####"); 117 return new Float (response).floatValue(); 118 } 119 120 protected void doSendSummary(MortgageResponse response) 121 throws HandlingException { 122 125 String stringResponse = "ERROR"; 126 String firstName = response.getFirstName(); 127 128 String lastName = response.getLastName(); 129 130 float rate = response.getMortgageRate(); 131 132 if (rate < 0) { 133 stringResponse = "Mortgage refused for the customer " + lastName 134 + " " + firstName; 135 } else { 136 stringResponse = "Mortgage accepted for the customer " + lastName 137 + " " + firstName + " with the following rate : " + rate; 138 } 139 140 143 stringResponse = "<response>" + stringResponse + "</response>"; 144 145 try { 146 149 Source source = SourceHelper.createSource(stringResponse); 150 151 154 MessageExchange msg = channel.createExchangeFactory() 155 .createInOnlyExchange(); 156 msg.setService(new QName (DEFAULT_NS, SENDSUMMARY_SERVICE)); 157 158 161 NormalizedMessage inNM = msg.createMessage(); 162 inNM.setContent(source); 163 ((InOnly) msg).setInMessage(inNM); 164 165 168 channel.sendSync(msg); 169 170 173 if (!ExchangeStatus.DONE.equals(msg.getStatus())) { 174 l.warning("Profiling summary isn't correctly sent !"); 175 } 176 } catch (Exception e) { 177 throw new HandlingException("Error sending summary", e); 178 } 179 180 } 181 } 182
| Popular Tags
|