KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > transport > FileSender


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.handlers.BasicHandler;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.util.Date JavaDoc;
28
29
30 /**
31  * Just write the XML to a file called xml#.req and wait for
32  * the result in a file called xml#.res
33  *
34  * Not thread-safe - just a dummy sample to show that we can indeed use
35  * something other than HTTP as the transport.
36  *
37  * @author Doug Davis (dug@us.ibm.com)
38  */

39
40 public class FileSender extends BasicHandler {
41   static int nextNum = 1 ;
42
43   public void invoke(MessageContext msgContext) throws AxisFault {
44     Message msg = msgContext.getRequestMessage();
45     byte[] buf = (byte[]) msg.getSOAPPartAsBytes();
46     boolean timedOut = false;
47     try {
48       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc( "xml" + nextNum + ".req" );
49
50       fos.write( buf );
51       fos.close();
52     }
53     catch( Exception JavaDoc e ) {
54       e.printStackTrace();
55     }
56
57     long timeout = Long.MAX_VALUE;
58     if (msgContext.getTimeout()!=0)
59       timeout=(new Date JavaDoc()).getTime()+msgContext.getTimeout();
60
61     for (; timedOut == false;) {
62       try {
63         Thread.sleep( 100 );
64         File JavaDoc file = new File JavaDoc( "xml" + nextNum + ".res" );
65
66         if ((new Date JavaDoc().getTime())>=timeout)
67             timedOut = true;
68
69         if ( !file.exists() ) continue ;
70         Thread.sleep( 100 ); // let the other side finish writing
71
FileInputStream JavaDoc fis = new FileInputStream JavaDoc( "xml" + nextNum + ".res" );
72         msg = new Message( fis );
73         msg.getSOAPPartAsBytes(); // just flush the buffer
74
fis.close();
75          Thread.sleep( 100 );
76         (new File JavaDoc("xml" + nextNum + ".res")).delete();
77         msgContext.setResponseMessage( msg );
78         break ;
79       }
80       catch( Exception JavaDoc e ) {
81         // File not there - just loop
82
}
83     }
84     nextNum++ ;
85     if (timedOut)
86         throw new AxisFault("timeout");
87
88   }
89 }
90
Popular Tags