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 pe...@apache.org on 2004/10/20 15:24:29 UTC
cvs commit: ws-axis/c/tests/utils/monitor/org/apache/test TestClientListener.java TestClientThread.java StopTCPMonitor.java TCPMonitor.java
perryan 2004/10/20 06:24:29
Modified: c/tests/utils/monitor/org/apache/test StopTCPMonitor.java
TCPMonitor.java
Added: c/tests/utils/monitor/org/apache/test
TestClientListener.java TestClientThread.java
Log:
Files updated and added to fix Jire defect AXISCPP-211
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS: If this change addresses a PR in the problem report tracking
CVS: database, then enter the PR number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system, such as NCSA,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to Apache by someone else; i.e.,
CVS: they sent us a patch or a new module, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.
Revision Changes Path
1.2 +4 -3 ws-axis/c/tests/utils/monitor/org/apache/test/StopTCPMonitor.java
Index: StopTCPMonitor.java
===================================================================
RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/StopTCPMonitor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StopTCPMonitor.java 27 Sep 2004 13:26:37 -0000 1.1
+++ StopTCPMonitor.java 20 Oct 2004 13:24:29 -0000 1.2
@@ -16,11 +16,12 @@
public void stopMonitor() {
Socket socket = null;
- DataOutputStream dos = null;
+ BufferedWriter dos = null;
try {
+ String stopString = "STOPTCPM";
socket = new Socket(hostname, port);
- dos = new DataOutputStream(socket.getOutputStream());
- dos.writeUTF("STOPTCPM");
+ dos = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
+ dos.write(stopString);
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (ConnectException ce) {
1.3 +75 -146 ws-axis/c/tests/utils/monitor/org/apache/test/TCPMonitor.java
Index: TCPMonitor.java
===================================================================
RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/TCPMonitor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TCPMonitor.java 27 Sep 2004 13:26:37 -0000 1.2
+++ TCPMonitor.java 20 Oct 2004 13:24:29 -0000 1.3
@@ -1,18 +1,10 @@
+
package org.apache.test;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.net.ServerSocket;
-import java.net.Socket;
-//import javax.net.ServerSocketFactory;
-//import javax.net.SocketFactory;
+import java.io.FileWriter;
/**
- * @author hawkeye
+ * @author perryan
* This class is designed to listen on a given port and send the request received on that
* port to the given RequestHandler.
* This class is meant to be used in a test suite scenario. where an instance of this class
@@ -20,10 +12,12 @@
*
*/
public class TCPMonitor {
- private ServerSocket serverSocket;
- private String forwardingURL;
- private String responseFile;
- private int forwardingPort;
+
+ private static TCPMonitor singleton = null;
+ private static FileWriter requestFileWriter;
+ private static FileWriter responseFileWriter;
+ private static boolean responseFileWriterOpen = false;
+ private static TestClientListener T = null;
/**
* Creates a new TCPMonitor listening on the given port for incoming requests (this is always on localhost of course!)
@@ -31,89 +25,75 @@
* @param listenerPort the port to listen for incoming requests
* @throws IOException if any issues occur listening for connections or supporting them.
*/
- public TCPMonitor(
+ private TCPMonitor(
int listenerPort,
- String urlToForwardTo,
- int forwardingPort,
+ String serviceHost,
+ int servicePort,
+ String requestFile,
String responseFile)
- throws IOException {
- serverSocket = new ServerSocket(listenerPort);
- this.forwardingURL = urlToForwardTo;
- this.forwardingPort = forwardingPort;
- this.responseFile = responseFile;
+ throws Exception {
+
+ requestFileWriter = new FileWriter(requestFile);
+ if(! responseFile.equals("")) {
+ responseFileWriter = new FileWriter(responseFile);
+ responseFileWriterOpen = true;
+ }
+
+ /*
+ * Create a thread which listens for incoming requests
+ */
+ T = new TestClientListener(listenerPort, serviceHost, servicePort);
+ T.startListener();
}
- /**
- * Listen for incoming connections and give them to the handler.
- *
- * Only one listen is called on the server socket. If more than one incoming connection is
- * expected then this will not be handled.
- *
- * @param handler
- */
- public void listen(RequestHandler handler) throws IOException {
- Socket clientSocket = serverSocket.accept();
+ public static TCPMonitor getInstance() throws Exception {
+ if(singleton == null) {
+ throw new Exception("TCPMonitor has not been initialised.");
+ }
+ return singleton;
+ }
- // Create a reader to read the request sent from the client
- BufferedReader bufferedReader =
- new BufferedReader(
- new InputStreamReader(clientSocket.getInputStream()));
-
- // Open the socket to the server to forward the request
- Socket outputSocket = new Socket(forwardingURL, forwardingPort);
-
- // Create a writer to write the incoming client request to
- BufferedWriter urlOutputWriter =
- new BufferedWriter(
- new OutputStreamWriter(outputSocket.getOutputStream()));
-
- // We have to read in parallel thread.
- RequestForwarder requestReader =
- new RequestForwarder(bufferedReader, urlOutputWriter, handler);
-
- // spin off the thread to listen for a client request
- requestReader.start();
-
- // Now handle the reply coming back from the forwarded URL
-
- // Create a reader to read the response from the server
- BufferedReader uriBufferedReader =
- new BufferedReader(
- new InputStreamReader(outputSocket.getInputStream()));
-
- // Create the writer to send it back to the client
- BufferedWriter bufferedWriter =
- new BufferedWriter(
- new OutputStreamWriter(clientSocket.getOutputStream()));
-
- RequestForwarder responseReader =
- new RequestForwarder(
- uriBufferedReader,
- bufferedWriter,
- new MyRequestHandler(responseFile));
-
- // spin off the thread to listen for a client request
- responseReader.start();
- try {
- // Wait for the request reader to finish
- requestReader.join(25000);
+ public static TCPMonitor getInstance(
+ int listenerPort,
+ String serviceHost,
+ int servicePort,
+ String requestFile,
+ String responseFile) throws Exception {
+ if(singleton == null) {
+ singleton = new TCPMonitor(listenerPort,serviceHost,servicePort,requestFile,responseFile);
+ }
+ return singleton;
+ }
- // If the response reader is still running then
- // ask it to stop and wait for it.
- if(responseReader.isAlive()) {
- responseReader.cease();
- responseReader.join(2000);
- }
- } catch (Exception me) {
+ public static void stop() {
+ T.stopListener();
+ try {
+ requestFileWriter.close();
+ responseFileWriter.close();
+ } catch (Exception e) {
;
- } finally {
- try {
- outputSocket.close();
- clientSocket.close();
- serverSocket.close();
- } catch (IOException mie) {
- ;
+ }
+ singleton=null;
+ System.exit(0);
+ }
+
+ public void writeRequest(String inputLine) {
+ try {
+ requestFileWriter.write(inputLine);
+ requestFileWriter.flush();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void writeResponse(String inputLine) {
+ try {
+ if(responseFileWriterOpen) {
+ responseFileWriter.write(inputLine);
+ responseFileWriter.flush();
}
+ } catch (Exception e) {
+ e.printStackTrace();
}
}
@@ -123,7 +103,7 @@
int listener_port = 0;
int forward_port = 0;
String forward_host = "";
- String output_file = "";
+ String request_file = "";
String response_file = "";
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-l")) {
@@ -139,7 +119,7 @@
continue;
}
if (args[i].equals("-o")) {
- output_file = new String(args[++i]);
+ request_file = new String(args[++i]);
continue;
}
if (args[i].equals("-r")) {
@@ -150,67 +130,16 @@
if (listener_port == 0
|| forward_port == 0
|| forward_host.equals("")
- || output_file.equals("")) {
+ || request_file.equals("")) {
System.out.println(
"usage: TCPMonitor <-l listen port> <-p forward port> <-h forward host> <-o request output file> [-r response output file]");
return;
}
TCPMonitor monitor =
- new TCPMonitor(listener_port, forward_host, forward_port, response_file);
- long now = System.currentTimeMillis();
- monitor.listen(new MyRequestHandler(output_file));
- System.err.println("**** Listened for " + (System.currentTimeMillis() - now) + " ms" );
- } catch (IOException exception) {
+ TCPMonitor.getInstance(listener_port, forward_host, forward_port, request_file, response_file);
+ } catch (Exception exception) {
exception.printStackTrace();
}
}
}
-class MyRequestHandler extends RequestHandler {
- private FileWriter output;
- private boolean output_set = false;
-
- public MyRequestHandler() {
- output_set = false;
- }
-
- public MyRequestHandler(String output_file) throws IOException {
- if(output_file.equals("")) {
- output_set = false;
- } else {
- output = new FileWriter(output_file);
- output_set = true;
- }
- }
- /* (non-Javadoc)
- * @see RequestHandler#handleWritingException(java.io.IOException)
- */
- public void handleWritingException(IOException exception) {
- exception.printStackTrace();
- }
- /* (non-Javadoc)
- * @see RequestHandler#handleReadingException(java.io.IOException)
- */
- public void handleReadingException(Throwable exception) {
- exception.printStackTrace();
- }
-
- /* (non-Javadoc)
- * @see RequestHandler#incomingRequestLine(java.lang.String)
- */
- public void incomingRequestLine(String line) throws IOException {
- if (output_set) {
- output.write(line);
- }
- }
-
- public void close() {
- if (output_set) {
- try {
- output.close();
- } catch (IOException e) {
- // do nothing
- }
- }
- }
-}
1.1 ws-axis/c/tests/utils/monitor/org/apache/test/TestClientListener.java
Index: TestClientListener.java
===================================================================
package org.apache.test;
import java.lang.*;
import java.io.*;
import java.net.*;
/**
* TestClientListener runs as a thread of the @see TestSingleton class and
* creates a ServerSocket object on port <b>6323<b> and from this creates
* a socket that accepts incoming requests. When a request is received new
* threads are created of type @see TestClientThread which do all the
* communication.
*
* @author Andrew Perry
* @since 1.0
*/
public class TestClientListener implements Runnable {
int listenPort = 0;
int servicePort = 0;
String serviceHost = null;
boolean stayAlive = false;
ServerSocket server = null;
Thread T = null;
public static final int CAPTURE_REQUEST = 1;
public static final int CAPTURE_RESPONSE = 2;
public TestClientListener() { }
public TestClientListener(int listenPort, String serviceHost, int servicePort) {
this.listenPort = listenPort;
this.serviceHost = serviceHost;
this.servicePort = servicePort;
}
public void startListener() {
if (T != null && T.isAlive())
throw new IllegalStateException("ServerManager already running");
try {
server = new ServerSocket(listenPort);
T = new Thread(this);
T.start();
} catch (Exception ioe) {
ioe.printStackTrace(System.err);
}
}
public void stopListener() {
stayAlive = false;
try {
if(T.isAlive()){
T.join(500);
}
} catch (Exception e) {
}
}
/**
* Implementation of @see Runnable run method required for @see Thread
*/
public void run() {
stayAlive = true;
Socket clientSocket = null;
Socket serviceSocket = null;
try {
TestClientThread requestReader = null;
TestClientThread responseReader = null;
while (stayAlive == true) {
server.setSoTimeout(500);
try {
try {
serviceSocket = new Socket(serviceHost, servicePort);
} catch (Exception se) {
System.err.println("Failed to open socket to service");
stayAlive = false;
continue;
}
clientSocket = server.accept();
requestReader = new TestClientThread(clientSocket, serviceSocket, CAPTURE_REQUEST);
responseReader = new TestClientThread(clientSocket, serviceSocket, CAPTURE_RESPONSE);
requestReader.start();
responseReader.start();
try {
if(requestReader.isAlive()) {
requestReader.join(10000);
}
// If the response reader is still running then
// ask it to stop and wait for it.
if(responseReader.isAlive()) {
responseReader.cease();
// Wait for upto another .5 secs for the request reader to finish
responseReader.join(2000);
}
} catch (Exception me) {
;
} finally {
try {
if(clientSocket != null)
clientSocket.close();
if(serviceSocket != null)
serviceSocket.close();
} catch (IOException mie) {
;
}
}
}
catch (SocketTimeoutException ste) {
// interrupt the accept call so the loop can end gracefully
}
}
} catch (Exception e) {
System.out.println("TestClientListener: " + e.getMessage());
}
if (server != null)
try {
if(clientSocket != null)
clientSocket.close();
if(serviceSocket != null)
serviceSocket.close();
server.close();
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
server = null;
stayAlive = false;
}
}
1.1 ws-axis/c/tests/utils/monitor/org/apache/test/TestClientThread.java
Index: TestClientThread.java
===================================================================
package org.apache.test;
import java.io.*;
import java.net.*;
/**
* TestClientThread is a child thread of TestClientListener and
* handles all communication between the original requestor and
* the TCPMonitor class.
*
* @author Andrew Perry
* @since 1.0
* @see TestClientListener
*/
public class TestClientThread extends Thread {
private Socket clientSocket = null;
private Socket serviceSocket = null;
private int mode = 0;
private int clientPort = 0;
boolean continueToRun = true;
public static final int CAPTURE_REQUEST = 1;
public static final int CAPTURE_RESPONSE = 2;
/**
* Class constructor with the client socket used to communitate
* with the client.
* @param socket reference to the socket connected to the client
*/
public TestClientThread(Socket clientSocket, Socket serviceSocket, int mode) {
this.clientSocket = clientSocket;
this.serviceSocket = serviceSocket;
this.mode = mode;
}
public void cease() {
continueToRun = false;
}
/**
* Reads the request from the client and if of a valid format will
* extract the test ID and required data and call the
* TestSingleton class to set or get the information.
* It is assumed that all requests are UTF Strings.
* <p>
* If the incoming request does not contain a test ID, or is not of a
* recognised format then the socket will be closed and this object will
* finish.
* </p>
*/
public void run() {
BufferedWriter dos = null;
BufferedReader dis = null;
String strMode = "unknown";
try {
char[] buffer = new char[4096]; // 4K buffer
if(mode == CAPTURE_REQUEST) {
strMode = "REQUEST - ";
dos = new BufferedWriter(new OutputStreamWriter(serviceSocket.getOutputStream()));
dis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} else if(mode == CAPTURE_RESPONSE) {
strMode = "RESPONSE - ";
dos = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
dis = new BufferedReader(new InputStreamReader(serviceSocket.getInputStream()));
}
int readReturn = 0;
while( (readReturn = dis.read(buffer, 0, 4096)) != -1 ){
String inputLine = new String(buffer, 0 , readReturn);
try {
if(inputLine.indexOf("STOPTCPM") != -1) {
TCPMonitor.getInstance().stop();
continueToRun = false;
break;
}
if(mode == CAPTURE_REQUEST) {
TCPMonitor.getInstance().writeRequest(inputLine);
} else if(mode == CAPTURE_RESPONSE) {
TCPMonitor.getInstance().writeResponse(inputLine);
}
} catch (Exception exe) {
}
dos.write(inputLine);
dos.flush();
}
} catch (EOFException e) {
} catch (IOException e) {
} finally {
try {
dis.close();
dos.close();
} catch (Exception e) { }
}
}
}