001: package samples.transport;
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.Message;
021: import org.apache.axis.MessageContext;
022: import org.apache.axis.server.AxisServer;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.FileOutputStream;
028:
029: /**
030: * Waits for the XML to appear in a file called xml#.req and writes
031: * the response in a file called xml#.res
032: *
033: * @author Doug Davis (dug@us.ibm.com)
034: */
035: public class FileReader extends Thread {
036: static int nextNum = 1;
037: boolean pleaseStop = false;
038: boolean halted = false;
039:
040: public void run() {
041: AxisServer server = new AxisServer();
042: server.init();
043:
044: while (!pleaseStop) {
045: try {
046: Thread.sleep(100);
047: File file = new File("xml" + nextNum + ".req");
048: if (!file.exists())
049: continue;
050:
051: // avoid race condition where file comes to exist but we were halted -- RobJ
052: if (pleaseStop)
053: continue;
054:
055: Thread.sleep(100); // let the other side finish writing
056: FileInputStream fis = new FileInputStream(file);
057:
058: int this Num = nextNum++; // increment early to avoid infinite loops
059:
060: Message msg = new Message(fis);
061: msg.getSOAPPartAsBytes();
062:
063: fis.close();
064: file.delete();
065:
066: MessageContext msgContext = new MessageContext(server);
067: msgContext.setRequestMessage(msg);
068:
069: try {
070: server.invoke(msgContext);
071: msg = msgContext.getResponseMessage();
072: } catch (AxisFault af) {
073: msg = new Message(af);
074: msg.setMessageContext(msgContext);
075: } catch (Exception e) {
076: msg = new Message(new AxisFault(e.toString()));
077: msg.setMessageContext(msgContext);
078: }
079:
080: byte[] buf = msg.getSOAPPartAsBytes();
081: FileOutputStream fos = new FileOutputStream("xml"
082: + this Num + ".res");
083: fos.write(buf);
084: fos.close();
085: } catch (Exception e) {
086: if (!(e instanceof FileNotFoundException))
087: e.printStackTrace();
088: }
089: }
090:
091: halted = true;
092: System.out.println("FileReader halted.");
093: }
094:
095: public void halt() {
096: pleaseStop = true;
097: while (!halted) {
098: try {
099: Thread.sleep(100);
100: } catch (InterruptedException ie) {
101: break;
102: }
103: }
104: }
105: }
|