01: package samples.transport;
02:
03: /*
04: * Copyright 2001-2004 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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:
19: import org.apache.axis.AxisFault;
20: import org.apache.axis.Message;
21: import org.apache.axis.MessageContext;
22: import org.apache.axis.handlers.BasicHandler;
23:
24: import java.io.File;
25: import java.io.FileInputStream;
26: import java.io.FileOutputStream;
27: import java.util.Date;
28:
29: /**
30: * Just write the XML to a file called xml#.req and wait for
31: * the result in a file called xml#.res
32: *
33: * Not thread-safe - just a dummy sample to show that we can indeed use
34: * something other than HTTP as the transport.
35: *
36: * @author Doug Davis (dug@us.ibm.com)
37: */
38:
39: public class FileSender extends BasicHandler {
40: static int nextNum = 1;
41:
42: public void invoke(MessageContext msgContext) throws AxisFault {
43: Message msg = msgContext.getRequestMessage();
44: byte[] buf = (byte[]) msg.getSOAPPartAsBytes();
45: boolean timedOut = false;
46: try {
47: FileOutputStream fos = new FileOutputStream("xml" + nextNum
48: + ".req");
49:
50: fos.write(buf);
51: fos.close();
52: } catch (Exception e) {
53: e.printStackTrace();
54: }
55:
56: long timeout = Long.MAX_VALUE;
57: if (msgContext.getTimeout() != 0)
58: timeout = (new Date()).getTime() + msgContext.getTimeout();
59:
60: for (; timedOut == false;) {
61: try {
62: Thread.sleep(100);
63: File file = new File("xml" + nextNum + ".res");
64:
65: if ((new Date().getTime()) >= timeout)
66: timedOut = true;
67:
68: if (!file.exists())
69: continue;
70: Thread.sleep(100); // let the other side finish writing
71: FileInputStream fis = new FileInputStream("xml"
72: + nextNum + ".res");
73: msg = new Message(fis);
74: msg.getSOAPPartAsBytes(); // just flush the buffer
75: fis.close();
76: Thread.sleep(100);
77: (new File("xml" + nextNum + ".res")).delete();
78: msgContext.setResponseMessage(msg);
79: break;
80: } catch (Exception e) {
81: // File not there - just loop
82: }
83: }
84: nextNum++;
85: if (timedOut)
86: throw new AxisFault("timeout");
87:
88: }
89: }
|