You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ha...@apache.org on 2004/12/23 11:16:46 UTC

cvs commit: ws-axis/c/tests/utils/ServiceEmulator/org/apache/axis/test/mockserver MockServer.java

hawkeye     2004/12/23 02:16:46

  Modified:    c/tests/utils/ServiceEmulator/org/apache/axis/test/mockserver
                        MockServer.java
  Log:
  Fixed some issues with the MockServer. It now sends byte[] instead of single byte. Single bytes was causing it to not work after one cycle - somekind of wierd Java bug as far as I could tell. I had to reboot my machine after each cycle - it would work for the first cycle then fail. When it failed the service would then cycle using 80% CPU !
  
  Revision  Changes    Path
  1.2       +127 -50   ws-axis/c/tests/utils/ServiceEmulator/org/apache/axis/test/mockserver/MockServer.java
  
  Index: MockServer.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/c/tests/utils/ServiceEmulator/org/apache/axis/test/mockserver/MockServer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockServer.java	17 Nov 2004 10:55:21 -0000	1.1
  +++ MockServer.java	23 Dec 2004 10:16:46 -0000	1.2
  @@ -20,6 +20,7 @@
   package org.apache.axis.test.mockserver;
   
   import java.io.BufferedInputStream;
  +import java.io.BufferedOutputStream;
   import java.io.BufferedReader;
   import java.io.DataInputStream;
   import java.io.File;
  @@ -34,13 +35,13 @@
   import java.io.Reader;
   import java.net.ServerSocket;
   import java.net.Socket;
  +import java.net.SocketException;
   import java.net.SocketImpl;
   
   import javax.net.ServerSocketFactory;
   
   import org.apache.xerces.parsers.IntegratedParserConfiguration;
   
  -
   /**
    * This class is meant to solve the problem of how we debug problems when we don't have the service at the back end e.g. when 
    * a user puts an issue on the mailing list and only gives us the captured output from e.g. tcpmon. 
  @@ -50,8 +51,10 @@
    * The response files must have the complete http header information (I suggest the response file is the captured output from tcpmon).
    * This program does not add anything to the outgoing stream.
    * 
  - * 
    * The input to the service is ignored.
  + *
  + * Usage: can be found in printUsage()
  + * 
    * 
    * Possible enhancements to this program:
    * 1. allow multiple response files by looking to see what service is being called and having a lookup table of response files
  @@ -61,33 +64,66 @@
   public class MockServer
   {
       private int port;
  -    File responseFile; 
  +    File responseFile;
       public static final void printUsage()
       {
  -        System.out.println("Usage: java MockServer <port> <responseFile>");
  +        System.out.println(
  +            "Usage: java MockServer <port> <responseFile> <howManyTimesToListen>");
  +        System.out.println("port: the port to listen for requests on");
  +        System.out.println(
  +            "responseFile: The file to write out when a request is received");
  +        System.out.println(
  +            "howManyTimesToListen: How many times for the server to listen on the port and send the file - this should the same as the amount of requests you need serving");
       }
   
       public static void main(String[] args)
       {
           // check that we have the required params
  -        if (args.length < 2)
  +        if (args.length < 3)
           {
               printUsage();
               System.exit(-1);
           }
   
  -        // We have the params now let's run the server !
  -	try
  -	{
  -	    MockServer server = new MockServer(args[0], args[1]);
  -	   //    server.accept();
  -        
  -        
  -           server.start();
  +        // try to get the number of loops to do
  +        int loops = 0;
  +        try
  +        {
  +
  +            loops = new Integer(args[2]).intValue();
  +        }
  +        catch (NumberFormatException e)
  +        {
  +            System.out.println(
  +                "NumberFormatException getting the number of loops to do");
  +            System.out.println("Got loops of " + args[2]);
  +            printUsage();
  +            System.exit(-4);
           }
  -        catch(IOException ioException)
  +
  +        // We have the params now let's run the server !
  +
  +        // Reduce the thread priority so it does not overload the buffer
  +        // becuase we are writing out and the exe does not get enough thread time
  +        // to consume
  +        System.out.println(
  +            "Thread priority = " + Thread.currentThread().getPriority());
  +        Thread.currentThread().setPriority(
  +            Thread.currentThread().getPriority() - 1);
  +
  +        // Dumbest (but works) way to do the multiple listening is to put the loop here
  +        for (int i = 0; i < loops; i++)
           {
  -        	ioException.printStackTrace();
  +            try
  +            {
  +                MockServer server = new MockServer(args[0], args[1]);
  +
  +                server.start();
  +            }
  +            catch (IOException ioException)
  +            {
  +                ioException.printStackTrace();
  +            }
           }
       }
   
  @@ -100,10 +136,11 @@
       private void start() throws IOException
       {
           System.out.println("MockServer listening on port " + port);
  +        System.out.println("Output file = " + responseFile);
   
  -		ServerSocket serverSocket = new ServerSocket(port); 
  -		Socket incoming = serverSocket.accept( );        
  -		System.out.println("Got the socket " + incoming);
  +        ServerSocket serverSocket = new ServerSocket(port);
  +        Socket incoming = serverSocket.accept();
  +//        System.out.println("Got the socket " + incoming);
           InputStream inputStream = incoming.getInputStream();
   
           InputStreamReader inputStreamReader =
  @@ -112,52 +149,90 @@
           // For debug for now print out what we get.
           // read until the \n\n
           String completeInput = "";
  -        int nextByte=0;
  +        int nextByte = 0;
           System.out.println("Reading in from the socket ");
  -        
  -        while (!completeInput.endsWith("\n\n") && !completeInput.toLowerCase().endsWith("connection: keep-alive")&& ( nextByte = inputStreamReader.read()) != -1 )
  +
  +        while (!completeInput.endsWith("\n\n")
  +            && !completeInput.toLowerCase().endsWith("connection: keep-alive")
  +            && (nextByte = inputStreamReader.read()) != -1)
           {
  -        	completeInput+=(char)nextByte;
  -//        	System.out.println(completeInput);
  +            completeInput += (char) nextByte;
  +            System.out.println(completeInput);
           }
  -	// System.out.println(completeInput);
  -	System.out.println("Read the incoming now writing out the responseFile");
  -        
  +        // System.out.println(completeInput);
  +        System.out.println(
  +            "Read the incoming now writing out the responseFile");
  +
           // reply with the file
           outputFile(incoming.getOutputStream());
  -        
  +
           // Now close the socket
  +        incoming.getOutputStream().close();
           incoming.close();
  -        
  +        serverSocket.close();
  +
  +        System.out.println("Written out response");
       }
  -    
  +
       /**
        * this method opens the input file and streams it out to the given outputstream
        * @param outputStream the stream to output the file to 
        */
  -    private void outputFile(OutputStream outputStream) throws FileNotFoundException, IOException
  +    private void outputFile(OutputStream outputStream)
  +        throws FileNotFoundException, IOException
       {
  -    	// open the file
  -    	FileReader reader = new FileReader(responseFile);
  -    	
  -    	while(reader.ready())
  -    	{
  -    		int readInt = reader.read();
  -    		outputStream.write((byte)readInt);
  -    	}
  +        int loop = 1;
  +
  +        // open the file for reading
  +        FileReader reader = new FileReader(responseFile);
  +        BufferedReader bufferedReader = new BufferedReader(reader);
  +
  +        // Create a buffered output stream for performance
  +        BufferedOutputStream bufferedOutputStream =
  +            new BufferedOutputStream(outputStream);
  +        int readInt = 0;
  +        char[] charBuffer = new char[2000];
  +        while (readInt != -1)
  +        {
  +            readInt = bufferedReader.read(charBuffer);
  +
  +            if (readInt != -1)
  +            {
  +
  +                // got to convert from char[] to byte[];
  +                String tmpString = new String(charBuffer);
  +                byte[] outArray = tmpString.getBytes();
  +                try
  +                {
  +                    bufferedOutputStream.write(outArray);
  +                    System.out.println(">" + outArray);
  +                }
  +                catch (SocketException exception)
  +                {
  +                    System.out.println("loop = " + loop);
  +                    throw exception;
  +                }
  +                bufferedOutputStream.flush();
  +                loop++;
  +            }
  +        }
  +
  +        System.out.println(
  +            "Finished writing out the file and closed the stream.");
       }
   
       /**
        * @param portNumber the port to listen on
        * @param responseFile the responseFile to send back
        */
  -    public MockServer(String portNumber, String responseFile) throws IOException
  +    public MockServer(String portNumber, String responseFile)
  +        throws IOException
       {
  -    	// parse the portNumber
  -    	try
  -    	{
  -    	port = Integer.parseInt(portNumber);
  -    	}
  +        // parse the portNumber
  +        try
  +        {
  +            port = Integer.parseInt(portNumber);
  +        }
           catch (NumberFormatException numberFormatException)
           {
               System.out.println(
  @@ -165,15 +240,17 @@
               printUsage();
               System.exit(-2);
           }
  -        
  +
           // check the responsefile is there
           this.responseFile = new File(responseFile);
  -        
  -        if(!this.responseFile.canRead())
  +
  +        if (!this.responseFile.canRead())
           {
  -        	System.out.println("Can't read the response file <"+responseFile+">");
  -        	printUsage();
  -        	System.exit(-3);
  +            System.out.println(
  +                "Can't read the response file <" + responseFile + ">");
  +            printUsage();
  +            System.exit(-3);
           }
  +
       }
   }