KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > interop > testclient > InteropStub


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

17 package org.apache.sandesha.interop.testclient;
18
19
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Message;
22 import org.apache.axis.SimpleChain;
23 import org.apache.axis.client.Call;
24 import org.apache.axis.client.Service;
25 import org.apache.axis.components.logger.LogFactory;
26 import org.apache.axis.message.SOAPEnvelope;
27 import org.apache.commons.logging.Log;
28 import org.apache.sandesha.Constants;
29 import org.apache.sandesha.SandeshaContext;
30 import org.apache.sandesha.client.ClientHandlerUtil;
31 import org.apache.sandesha.client.ClientStorageManager;
32 import org.apache.sandesha.server.Sender;
33 import org.apache.sandesha.util.PropertyLoader;
34
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * This is class has the client for the interop testing. interop.jsp
39  * simply calls the runPing and runEcho methods.
40  *
41  * @author Chamikara Jayalath
42  */

43
44
45 public class InteropStub {
46
47     private InteropStub() {
48     }
49
50     private static Sender sender = null;
51     private static ClientStorageManager storageManager = new ClientStorageManager();
52     private static final Log log = LogFactory.getLog(InteropStub.class.getName());
53
54     private static InteropStub stub = null;
55
56     public static InteropStub getInstance() {
57
58         if (stub != null) {
59             return stub;
60         } else {
61             stub = new InteropStub();
62             return stub;
63         }
64     }
65
66     public static InteropCallback getCallback() {
67         return callback;
68     }
69
70     public static void setCallback(InteropCallback callback) {
71         InteropStub.callback = callback;
72     }
73
74     private static InteropCallback callback = null;
75
76     private void configureContext(SandeshaContext ctx, InteropBean bean) {
77         String JavaDoc from = bean.getFrom();
78         String JavaDoc replyTo = bean.getReplyto();
79         String JavaDoc acksTo = bean.getAcksTo();
80         String JavaDoc faultTo = bean.getFaultto();
81
82         boolean sendOffer = false;
83         if (bean.getOffer().equalsIgnoreCase("yes"))
84             sendOffer = true;
85
86         if (replyTo != null && replyTo.equalsIgnoreCase("anonymous")) {
87             ctx.setReplyToURL(Constants.WSA.NS_ADDRESSING_ANONYMOUS);
88         } else if (replyTo != null) {
89             ctx.setReplyToURL(bean.getReplyto());
90         }
91
92         if (from != null && from.equalsIgnoreCase("anonymous")) {
93             ctx.setFromURL(Constants.WSA.NS_ADDRESSING_ANONYMOUS);
94         } else if (from != null) {
95             ctx.setFromURL(from);
96         }
97
98         if (acksTo != null && acksTo.equalsIgnoreCase("anonymous")) {
99             ctx.setAcksToURL(Constants.WSA.NS_ADDRESSING_ANONYMOUS);
100         } else if (acksTo != null) {
101             ctx.setAcksToURL(acksTo);
102         }
103
104         if (faultTo != null && faultTo.equalsIgnoreCase("anonymous")) {
105             ctx.setFaultToURL(Constants.WSA.NS_ADDRESSING_ANONYMOUS);
106         } else if (faultTo != null) {
107             ctx.setFaultToURL(bean.getFaultto());
108         }
109
110
111         if (sendOffer)
112             ctx.setSendOffer(true);
113
114     }
115
116     public synchronized void runPing(InteropBean bean) {
117         if (log.isDebugEnabled()) {
118             log.debug("=========== RUNNING THE \"Ping\" INTEROP TEST ==========");
119         }
120         String JavaDoc target = bean.getTarget();
121         int msgs = bean.getNoOfMsgs();
122         try {
123
124             Service service = new Service();
125             Call call = (Call) service.createCall();
126
127             SandeshaContext ctx = new SandeshaContext(true);
128             ctx.setSourceURL(bean.getSourceURL());
129             configureContext(ctx, bean);
130             ctx.initCall(call, target, "urn:wsrm:Ping", Constants.ClientProperties.IN_ONLY);
131
132             for (int i = 1; i <= msgs; i++) {
133                 if (i == msgs) {
134                     ctx.setLastMessage(call);
135                 }
136                 call.invoke(new Message(getPingSOAPEnvelope(i)));
137             }
138
139             //InteropStub.stopClient();
140
ctx.endSequence();
141
142         } catch (Exception JavaDoc e) {
143             if (callback != null)
144                 callback.onError(e);
145             log.error(e);
146         }
147     }
148
149
150     private static String JavaDoc getPingSOAPEnvelope(int i) {
151         return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">\n" +
152                 " <soapenv:Header>\n" + " </soapenv:Header>\n" + " <soapenv:Body>\n" + " <Ping xmlns=\"http://tempuri.org/\">\n" +
153                 " <Text>Sandesha Ping Message " + i + "</Text>\n" + " </Ping>\n" + " </soapenv:Body></soapenv:Envelope>";
154
155     }
156
157     private static String JavaDoc getEchoSOAPEnvelope(int i, String JavaDoc seq) {
158         return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">\n" +
159                 "<soapenv:Header>\n" +
160                 "</soapenv:Header>\n" +
161                 "<soapenv:Body>\n" +
162                 " <echoString xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
163                 " xmlns=\"http://tempuri.org/\">\n" +
164                 " <Text>Sandesha Echo Message " + i + "</Text>\n" +
165                 " <Sequence>" + seq + "</Sequence>\n" +
166                 " </echoString>\n" +
167                 "</soapenv:Body></soapenv:Envelope>";
168
169     }
170
171     public synchronized void runEcho(InteropBean bean) {
172
173         String JavaDoc target = bean.getTarget();
174         int messages = bean.getNoOfMsgs();
175         String JavaDoc seq = new Long JavaDoc(System.currentTimeMillis()).toString();
176
177         try {
178             if (log.isDebugEnabled()) {
179                 log.debug("=========== RUNNING THE \"echoString\" INTEROP TEST ==========");
180             }
181
182             //We start the listener to be in the safe side.
183
//User may specify some external(not in sandesha endpoint) replyTo address, then
184
//he/she will not be able to retrieve the responses to this client, yet they can verify
185
//the reliablility of the sent messages.
186

187
188             Service service = new Service();
189             Call call = (Call) service.createCall();
190
191             SandeshaContext ctx = new SandeshaContext(true);
192             ctx.setSourceURL(bean.getSourceURL());
193
194             configureContext(ctx, bean);
195             ctx.initCall(call, target, "urn:wsrm:echoString", Constants.ClientProperties.IN_OUT);
196
197             for (int i = 1; i <= messages; i++) {
198                 if (i == messages) {
199                     ctx.setLastMessage(call);
200                 }
201
202                 SOAPEnvelope env = call.invoke(new Message(getEchoSOAPEnvelope(i, seq)));
203                 if (log.isDebugEnabled()) {
204                     log.debug("Got response from server " + env.toString());
205                 }
206             }
207
208             ctx.endSequence();
209
210         } catch (Exception JavaDoc e) {
211             if (callback != null)
212                 callback.onError(e);
213             log.error(e);
214         }
215     }
216
217
218     public static void initClient() throws AxisFault {
219         if (log.isDebugEnabled()) {
220             log.debug("STARTING SENDER FOR THE CLIENT .......");
221         }
222         sender = new Sender(storageManager);
223         SimpleChain reqChain = null;
224         SimpleChain resChain = null;
225         try {
226             reqChain = getRequestChain();
227             resChain = getResponseChain();
228         } catch (Exception JavaDoc e) {
229             throw new AxisFault(e.getMessage());
230         }
231         if (reqChain != null)
232             sender.setRequestChain(reqChain);
233         if (resChain != null)
234             sender.setResponseChain(resChain);
235         sender.startSender();
236     }
237
238
239     public static void stopClientByForce() throws AxisFault {
240         sender.stop();
241         throw new AxisFault("Inactivity Timeout Reached, No Response from the Server");
242     }
243
244     private static SimpleChain getRequestChain() {
245         ArrayList JavaDoc arr = PropertyLoader.getRequestHandlerNames();
246         return ClientHandlerUtil.getHandlerChain(arr);
247     }
248
249
250     private static SimpleChain getResponseChain() {
251
252         ArrayList JavaDoc arr = PropertyLoader.getResponseHandlerNames();
253         return ClientHandlerUtil.getHandlerChain(arr);
254     }
255
256 }
257
Popular Tags