KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > transport > FileReader


1 package samples.transport ;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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.server.AxisServer;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28
29 /**
30  * Waits for the XML to appear in a file called xml#.req and writes
31  * the response in a file called xml#.res
32  *
33  * @author Doug Davis (dug@us.ibm.com)
34  */

35 public class FileReader extends Thread JavaDoc {
36   static int nextNum = 1 ;
37   boolean pleaseStop = false ;
38   boolean halted = false ;
39
40   public void run() {
41     AxisServer server = new AxisServer();
42     server.init();
43
44     while( !pleaseStop ) {
45       try {
46         Thread.sleep( 100 );
47         File JavaDoc file = new File JavaDoc( "xml" + nextNum + ".req" );
48         if ( !file.exists() ) continue ;
49           
50           // avoid race condition where file comes to exist but we were halted -- RobJ
51
if (pleaseStop) continue;
52
53         Thread.sleep( 100 ); // let the other side finish writing
54
FileInputStream JavaDoc fis = new FileInputStream JavaDoc( file );
55
56         int thisNum = nextNum++; // increment early to avoid infinite loops
57

58         Message msg = new Message( fis );
59         msg.getSOAPPartAsBytes();
60
61         fis.close();
62         file.delete();
63
64         MessageContext msgContext = new MessageContext(server);
65         msgContext.setRequestMessage( msg );
66
67         try {
68             server.invoke( msgContext );
69             msg = msgContext.getResponseMessage();
70         } catch (AxisFault af) {
71             msg = new Message(af);
72             msg.setMessageContext(msgContext);
73         } catch (Exception JavaDoc e) {
74             msg = new Message(new AxisFault(e.toString()));
75             msg.setMessageContext(msgContext);
76         }
77         
78         byte[] buf = msg.getSOAPPartAsBytes();
79         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc( "xml" + thisNum + ".res" );
80         fos.write( buf );
81         fos.close();
82       }
83       catch( Exception JavaDoc e ) {
84         if ( !(e instanceof FileNotFoundException JavaDoc) )
85           e.printStackTrace();
86       }
87     }
88
89     halted = true;
90     System.out.println("FileReader halted.");
91   }
92
93   public void halt() {
94     pleaseStop = true ;
95     while (!halted) {
96       try {
97         Thread.sleep(100);
98       } catch (InterruptedException JavaDoc ie) {
99         break;
100       }
101     }
102   }
103 }
104
Popular Tags