KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > mortgage > workflow > orchestration > MortgageProfilingUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AbstractMortgageProfiling.java 154 7 juin 2006 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.demo.mortgage.workflow.orchestration;
23
24 import java.util.logging.Logger JavaDoc;
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 JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
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 JavaDoc DEFAULT_NS = "http://petals.objectweb.org/";
41
42     public final static String JavaDoc EVALRATE_SERVICE = "EvalRateService";
43
44     public final static String JavaDoc SENDSUMMARY_SERVICE = "SendSummaryService";
45
46     public final static String JavaDoc EVALRATE_SERVICE_OP = "evaluate";
47
48     public final static String JavaDoc OK_PROFILE = "ok";
49
50     protected DeliveryChannel channel;
51
52     protected Logger JavaDoc l;
53
54     public MortgageProfilingUtil(DeliveryChannel channel, Logger JavaDoc 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 JavaDoc profile) throws HandlingException {
68
69         /*
70          * Create xml content from given profile
71          */

72         profile = "<profile>" + profile + "</profile>";
73
74         String JavaDoc response = "RateNotDefined";
75         try {
76             /*
77              * Create message content from given profile
78              */

79             Source JavaDoc source = SourceHelper.createSource(profile);
80
81             /*
82              * Create a new message exchange for MortgageService
83              */

84             MessageExchange msg = channel.createExchangeFactory()
85                     .createInOutExchange();
86             msg.setService(new QName JavaDoc(DEFAULT_NS, EVALRATE_SERVICE));
87             msg.setOperation(new QName JavaDoc(EVALRATE_SERVICE_OP));
88
89             /*
90              * Create the inMessage to send
91              */

92             NormalizedMessage inNM = msg.createMessage();
93             inNM.setContent(source);
94             ((InOut) msg).setInMessage(inNM);
95
96             /*
97              * Send the message exchange
98              */

99             channel.sendSync(msg);
100
101             /*
102              * Get response from EvalRate service
103              */

104             NormalizedMessage outNM = ((InOut) msg).getOutMessage();
105             if (outNM != null) {
106                 response = SourceHelper.createString(outNM.getContent());
107             }
108
109             // create string content from xml content
110
response = response.substring(response.indexOf("<response>") + 10,
111                     response.indexOf("</response>"));
112         } catch (Exception JavaDoc e) {
113             throw new HandlingException("Error evaluating mortgage rate", e);
114         }
115
116         l.info("##### Evaluated rate : " + response + " #####");
117         return new Float JavaDoc(response).floatValue();
118     }
119
120     protected void doSendSummary(MortgageResponse response)
121             throws HandlingException {
122         /*
123          * Create String summary
124          */

125         String JavaDoc stringResponse = "ERROR";
126         String JavaDoc firstName = response.getFirstName();
127
128         String JavaDoc 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         /*
141          * Create xml content from string content
142          */

143         stringResponse = "<response>" + stringResponse + "</response>";
144
145         try {
146             /*
147              * Create message content from given string summary
148              */

149             Source JavaDoc source = SourceHelper.createSource(stringResponse);
150
151             /*
152              * Create a new message exchange for SendSummaryService
153              */

154             MessageExchange msg = channel.createExchangeFactory()
155                     .createInOnlyExchange();
156             msg.setService(new QName JavaDoc(DEFAULT_NS, SENDSUMMARY_SERVICE));
157
158             /*
159              * Create the inMessage to send
160              */

161             NormalizedMessage inNM = msg.createMessage();
162             inNM.setContent(source);
163             ((InOnly) msg).setInMessage(inNM);
164
165             /*
166              * Send the message exchange
167              */

168             channel.sendSync(msg);
169
170             /*
171              * Check if summary has been correctly sent
172              */

173             if (!ExchangeStatus.DONE.equals(msg.getStatus())) {
174                 l.warning("Profiling summary isn't correctly sent !");
175             }
176         } catch (Exception JavaDoc e) {
177             throw new HandlingException("Error sending summary", e);
178         }
179
180     }
181 }
182
Popular Tags