Author: veithen Date: Mon Dec 29 07:59:10 2008 New Revision: 729922 URL: http://svn.apache.org/viewvc?rev=729922&view=rev Log: Refactored the common code in the two SocketRR implementations into a common base class.
Added: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java (contents, props changed) - copied, changed from r729870, webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml webservices/commons/trunk/modules/tcpmon/modules/tcpmon-eclipse-plugin/pom.xml webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/eclipse/ui/SocketRR.java Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml?rev=729922&r1=729921&r2=729922&view=diff ============================================================================== --- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml (original) +++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml Mon Dec 29 07:59:10 2008 @@ -40,6 +40,7 @@ <target>1.3</target> <includes> <include>org/apache/ws/commons/tcpmon/SlowLinkSimulator.java</include> + <include>org/apache/ws/commons/tcpmon/core/**/*.java</include> </includes> </configuration> </plugin> Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-eclipse-plugin/pom.xml URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-eclipse-plugin/pom.xml?rev=729922&r1=729921&r2=729922&view=diff ============================================================================== --- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-eclipse-plugin/pom.xml (original) +++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-eclipse-plugin/pom.xml Mon Dec 29 07:59:10 2008 @@ -85,6 +85,7 @@ <Bundle-SymbolicName>${groupId}.${artifactId};singleton:=true</Bundle-SymbolicName> <Export-Package> org.apache.ws.commons.tcpmon, + org.apache.ws.commons.tcpmon.core, org.apache.ws.commons.tcpmon.eclipse.* </Export-Package> <Bundle-Activator>org.apache.ws.commons.tcpmon.eclipse.plugin.TcpmonitorPlugin</Bundle-Activator> Modified: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java?rev=729922&r1=729921&r2=729922&view=diff ============================================================================== --- webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java (original) +++ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java Mon Dec 29 07:59:10 2008 @@ -18,6 +18,9 @@ import javax.swing.JTextArea; import javax.swing.table.TableModel; + +import org.apache.ws.commons.tcpmon.core.AbstractSocketRR; + import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; @@ -26,47 +29,11 @@ * this class handles the pumping of data from the incoming socket to the * outgoing socket */ -class SocketRR extends Thread { - - /** - * Field inSocket - */ - Socket inSocket = null; - - /** - * Field outSocket - */ - Socket outSocket = null; - +class SocketRR extends AbstractSocketRR { /** * Field textArea */ JTextArea textArea; - - /** - * Field in - */ - InputStream in = null; - - /** - * Field out - */ - OutputStream out = null; - - /** - * Field xmlFormat - */ - boolean xmlFormat; - - /** - * Field done - */ - volatile boolean done = false; - - /** - * Field tmodel - */ - volatile long elapsed = 0; /** * Field tmodel @@ -79,21 +46,11 @@ int tableIndex = 0; /** - * Field type - */ - String type = null; - - /** * Field myConnection */ Connection myConnection = null; /** - * Field slowLink - */ - SlowLinkSimulator slowLink; - - /** * Constructor SocketRR * * @param c @@ -113,241 +70,31 @@ OutputStream outputStream, JTextArea _textArea, boolean format, TableModel tModel, int index, final String type, SlowLinkSimulator slowLink) { - inSocket = inputSocket; - in = inputStream; - outSocket = outputSocket; - out = outputStream; + super(inputSocket, inputStream, outputSocket, outputStream, format, type, slowLink); textArea = _textArea; - xmlFormat = format; tmodel = tModel; tableIndex = index; - this.type = type; myConnection = c; - this.slowLink = slowLink; start(); } - /** - * Method isDone - * - * @return boolean - */ - public boolean isDone() { - return done; + protected boolean isSaveFirstLine() { + return tmodel != null; } - - public String getElapsed() { - return String.valueOf(elapsed); + + protected String getSavedFirstLine() { + return (String) tmodel.getValueAt(tableIndex, TCPMon.REQ_COLUMN); } - /** - * Method run - */ - public void run() { - try { - byte[] buffer = new byte[4096]; - byte[] tmpbuffer = new byte[8192]; - int saved = 0; - int len; - int i1, i2; - int i; - int reqSaved = 0; - int tabWidth = 3; - boolean atMargin = true; - int thisIndent = -1, nextIndent = -1, previousIndent = -1; - if (tmodel != null) { - String tmpStr = (String) tmodel.getValueAt(tableIndex, - TCPMon.REQ_COLUMN); - if (!"".equals(tmpStr)) { - reqSaved = tmpStr.length(); - } - } - long start = System.currentTimeMillis(); - a: - for (; ;) { - - elapsed = System.currentTimeMillis() - start; - - if (done) { - break; - } - - // try{ - // len = in.available(); - // }catch(Exception e){len=0;} - len = buffer.length; - - // Used to be 1, but if we block it doesn't matter - // however 1 will break with some servers, including apache - if (len == 0) { - len = buffer.length; - } - if (saved + len > buffer.length) { - len = buffer.length - saved; - } - int len1 = 0; - while (len1 == 0) { - try { - len1 = in.read(buffer, saved, len); - } catch (Exception ex) { - if (done && (saved == 0)) { - break a; - } - len1 = -1; - break; - } - } - len = len1; - if ((len == -1) && (saved == 0)) { - break; - } - if (len == -1) { - done = true; - } - - // No matter how we may (or may not) format it, send it - // on unformatted - we don't want to mess with how its - // sent to the other side, just how its displayed - if ((out != null) && (len > 0)) { - slowLink.pump(len); - out.write(buffer, saved, len); - } - - if ((tmodel != null) && (reqSaved < 50)) { - String old = (String) tmodel.getValueAt(tableIndex, - TCPMon.REQ_COLUMN); - old = old + new String(buffer, saved, len); - if (old.length() > 50) { - old = old.substring(0, 50); - } - reqSaved = old.length(); - if ((i = old.indexOf('\n')) > 0) { - old = old.substring(0, i - 1); - reqSaved = 50; - } - tmodel.setValueAt(old, tableIndex, TCPMon.REQ_COLUMN); - } - - - if (xmlFormat) { - - // Do XML Formatting - boolean inXML = false; - int bufferLen = saved; - if (len != -1) { - bufferLen += len; - } - i1 = 0; - i2 = 0; - saved = 0; - for (; i1 < bufferLen; i1++) { - - // Except when we're at EOF, saved last char - if ((len != -1) && (i1 + 1 == bufferLen)) { - saved = 1; - break; - } - thisIndent = -1; - if ((buffer[i1] == '<') - && (buffer[i1 + 1] != '/')) { - previousIndent = nextIndent++; - thisIndent = nextIndent; - inXML = true; - } - if ((buffer[i1] == '<') - && (buffer[i1 + 1] == '/')) { - if (previousIndent > nextIndent) { - thisIndent = nextIndent; - } - previousIndent = nextIndent--; - inXML = true; - } - if ((buffer[i1] == '/') - && (buffer[i1 + 1] == '>')) { - previousIndent = nextIndent--; - inXML = true; - } - if (thisIndent != -1) { - if (thisIndent > 0) { - tmpbuffer[i2++] = (byte) '\n'; - } - for (i = tabWidth * thisIndent; i > 0; i--) { - tmpbuffer[i2++] = (byte) ' '; - } - } - atMargin = ((buffer[i1] == '\n') - || (buffer[i1] == '\r')); - if (!inXML || !atMargin) { - tmpbuffer[i2++] = buffer[i1]; - } - } - - textArea.append(new String(tmpbuffer, 0, i2)); - - // Shift saved bytes to the beginning - for (i = 0; i < saved; i++) { - buffer[i] = buffer[bufferLen - saved + i]; - } - } else { - textArea.append(new String(buffer, 0, len)); - } - } - - } catch (Exception e) { - e.printStackTrace(); - } finally { - done = true; - try { - if (out != null) { - out.flush(); - if (null != outSocket) { - outSocket.shutdownOutput(); - } else { - out.close(); - } - out = null; - } - } catch (Exception e) { - } - try { - if (in != null) { - if (inSocket != null) { - inSocket.shutdownInput(); - } else { - in.close(); - } - in = null; - } - } catch (Exception e) { - } - myConnection.wakeUp(); - } + protected void setSavedFirstLine(String value) { + tmodel.setValueAt(value, tableIndex, TCPMon.REQ_COLUMN); + } + + protected void appendData(String data) { + textArea.append(data); } - /** - * Method halt - */ - public void halt() { - try { - if (inSocket != null) { - inSocket.close(); - } - if (outSocket != null) { - outSocket.close(); - } - inSocket = null; - outSocket = null; - if (in != null) { - in.close(); - } - if (out != null) { - out.close(); - } - in = null; - out = null; - done = true; - } catch (Exception e) { - e.printStackTrace(); - } + protected void onFinish() { + myConnection.wakeUp(); } } Copied: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java (from r729870, webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java) URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java?p2=webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java&p1=webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java&r1=729870&r2=729922&rev=729922&view=diff ============================================================================== --- webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/SocketRR.java (original) +++ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java Mon Dec 29 07:59:10 2008 @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.apache.ws.commons.tcpmon; +package org.apache.ws.commons.tcpmon.core; + +import org.apache.ws.commons.tcpmon.SlowLinkSimulator; -import javax.swing.JTextArea; -import javax.swing.table.TableModel; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; @@ -26,7 +26,7 @@ * this class handles the pumping of data from the incoming socket to the * outgoing socket */ -class SocketRR extends Thread { +public abstract class AbstractSocketRR extends Thread { /** * Field inSocket @@ -39,11 +39,6 @@ Socket outSocket = null; /** - * Field textArea - */ - JTextArea textArea; - - /** * Field in */ InputStream in = null; @@ -69,26 +64,11 @@ volatile long elapsed = 0; /** - * Field tmodel - */ - TableModel tmodel = null; - - /** - * Field tableIndex - */ - int tableIndex = 0; - - /** * Field type */ String type = null; /** - * Field myConnection - */ - Connection myConnection = null; - - /** * Field slowLink */ SlowLinkSimulator slowLink; @@ -108,23 +88,17 @@ * @param type * @param slowLink */ - public SocketRR(Connection c, Socket inputSocket, + public AbstractSocketRR(Socket inputSocket, InputStream inputStream, Socket outputSocket, - OutputStream outputStream, JTextArea _textArea, - boolean format, TableModel tModel, int index, + OutputStream outputStream, boolean format, final String type, SlowLinkSimulator slowLink) { inSocket = inputSocket; in = inputStream; outSocket = outputSocket; out = outputStream; - textArea = _textArea; xmlFormat = format; - tmodel = tModel; - tableIndex = index; this.type = type; - myConnection = c; this.slowLink = slowLink; - start(); } /** @@ -155,9 +129,8 @@ int tabWidth = 3; boolean atMargin = true; int thisIndent = -1, nextIndent = -1, previousIndent = -1; - if (tmodel != null) { - String tmpStr = (String) tmodel.getValueAt(tableIndex, - TCPMon.REQ_COLUMN); + if (isSaveFirstLine()) { + String tmpStr = getSavedFirstLine(); if (!"".equals(tmpStr)) { reqSaved = tmpStr.length(); } @@ -213,9 +186,8 @@ out.write(buffer, saved, len); } - if ((tmodel != null) && (reqSaved < 50)) { - String old = (String) tmodel.getValueAt(tableIndex, - TCPMon.REQ_COLUMN); + if (isSaveFirstLine() && (reqSaved < 50)) { + String old = getSavedFirstLine(); old = old + new String(buffer, saved, len); if (old.length() > 50) { old = old.substring(0, 50); @@ -225,7 +197,7 @@ old = old.substring(0, i - 1); reqSaved = 50; } - tmodel.setValueAt(old, tableIndex, TCPMon.REQ_COLUMN); + setSavedFirstLine(old); } @@ -282,14 +254,14 @@ } } - textArea.append(new String(tmpbuffer, 0, i2)); + appendData(new String(tmpbuffer, 0, i2)); // Shift saved bytes to the beginning for (i = 0; i < saved; i++) { buffer[i] = buffer[bufferLen - saved + i]; } } else { - textArea.append(new String(buffer, 0, len)); + appendData(new String(buffer, 0, len)); } } @@ -320,7 +292,7 @@ } } catch (Exception e) { } - myConnection.wakeUp(); + onFinish(); } } @@ -350,4 +322,10 @@ e.printStackTrace(); } } + + protected abstract boolean isSaveFirstLine(); + protected abstract String getSavedFirstLine(); + protected abstract void setSavedFirstLine(String value); + protected abstract void appendData(String data); + protected abstract void onFinish(); } Propchange: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/AbstractSocketRR.java ------------------------------------------------------------------------------ svn:mergeinfo = Modified: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/eclipse/ui/SocketRR.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/eclipse/ui/SocketRR.java?rev=729922&r1=729921&r2=729922&view=diff ============================================================================== --- webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/eclipse/ui/SocketRR.java (original) +++ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/eclipse/ui/SocketRR.java Mon Dec 29 07:59:10 2008 @@ -23,54 +23,19 @@ import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; import org.apache.ws.commons.tcpmon.SlowLinkSimulator; +import org.apache.ws.commons.tcpmon.core.AbstractSocketRR; /** * this class handles the pumping of data from the incoming socket to the * outgoing socket Same as the swing one except for the use of SWT components */ -class SocketRR extends Thread { - - /** - * Field inSocket - */ - Socket inSocket = null; - - /** - * Field outSocket - */ - Socket outSocket = null; - +class SocketRR extends AbstractSocketRR { /** * Field textArea */ Text textArea; /** - * Field in - */ - InputStream in = null; - - /** - * Field out - */ - OutputStream out = null; - - /** - * Field xmlFormat - */ - boolean xmlFormat; - - /** - * Field done - */ - volatile boolean done = false; - - /** - * Field tmodel - */ - volatile long elapsed = 0; - - /** * Field tmodel */ Table tmodel = null; @@ -81,21 +46,11 @@ int tableIndex = 0; /** - * Field type - */ - String type = null; - - /** * Field myConnection */ Connection myConnection = null; /** - * Field slowLink - */ - SlowLinkSimulator slowLink; - - /** * Constructor SocketRR * * @param c @@ -115,278 +70,45 @@ OutputStream outputStream, Text _textArea, boolean format, Table tModel, int index, final String type, SlowLinkSimulator slowLink) { - inSocket = inputSocket; - in = inputStream; - outSocket = outputSocket; - out = outputStream; + super(inputSocket, inputStream, outputSocket, outputStream, format, type, slowLink); textArea = _textArea; - xmlFormat = format; tmodel = tModel; tableIndex = index; - this.type = type; myConnection = c; - this.slowLink = slowLink; start(); } - /** - * Method isDone - * - * @return boolean - */ - public boolean isDone() { - return done; - } - - public String getElapsed() { - return String.valueOf(elapsed); + protected boolean isSaveFirstLine() { + return tmodel != null; } - /** - * Method run - */ - public void run() { - try { - byte[] buffer = new byte[4096]; - byte[] tmpbuffer = new byte[8192]; - int saved = 0; - int len; - int i1, i2; - int i; - int reqSaved = 0; - int tabWidth = 3; - boolean atMargin = true; - int thisIndent = -1, nextIndent = -1, previousIndent = -1; - - final int[] result = new int[] { reqSaved }; - MainView.display.syncExec(new Runnable() { - public void run() { - if (tmodel != null) { - String tmpStr = tmodel.getItem(tableIndex).getText(MainView.REQ_COLUMN); - if (!"".equals(tmpStr)) { - result[0] = tmpStr.length(); - } - } - } - }); - reqSaved = result[0]; - - long start = System.currentTimeMillis(); - a: - for (; ;) { - - elapsed = System.currentTimeMillis() - start; - - if (done) { - break; - } - - // try{ - // len = in.available(); - // }catch(Exception e){len=0;} - len = buffer.length; - - // Used to be 1, but if we block it doesn't matter - // however 1 will break with some servers, including apache - if (len == 0) { - len = buffer.length; - } - if (saved + len > buffer.length) { - len = buffer.length - saved; - } - int len1 = 0; - while (len1 == 0) { - try { - len1 = in.read(buffer, saved, len); - } catch (Exception ex) { - if (done && (saved == 0)) { - break a; - } - len1 = -1; - break; - } - } - len = len1; - if ((len == -1) && (saved == 0)) { - break; - } - if (len == -1) { - done = true; - } - - // No matter how we may (or may not) format it, send it - // on unformatted - we don't want to mess with how its - // sent to the other side, just how its displayed - if ((out != null) && (len > 0)) { - slowLink.pump(len); - out.write(buffer, saved, len); - } - - final boolean[] outBool = new boolean[1]; - MainView.display.syncExec(new Runnable() { - public void run() { - outBool[0] = (tmodel != null); - } - }); - - - if (outBool[0] && (reqSaved < 50)) { - final String[] outString = new String[1]; - MainView.display.syncExec(new Runnable() { - public void run() { - outString[0] = tmodel.getItem(tableIndex).getText(MainView.REQ_COLUMN); - } - }); - - String old = outString[0]; - old = old + new String(buffer, saved, len); - if (old.length() > 50) { - old = old.substring(0, 50); - } - reqSaved = old.length(); - if ((i = old.indexOf('\n')) > 0) { - old = old.substring(0, i - 1); - reqSaved = 50; - } - - final String inputString = old; - MainView.display.syncExec(new Runnable() { - public void run() { - tmodel.getItem(tableIndex).setText(MainView.REQ_COLUMN, inputString); - } - }); - } - - - if (xmlFormat) { - - // Do XML Formatting - boolean inXML = false; - int bufferLen = saved; - if (len != -1) { - bufferLen += len; - } - i1 = 0; - i2 = 0; - saved = 0; - for (; i1 < bufferLen; i1++) { - - // Except when we're at EOF, saved last char - if ((len != -1) && (i1 + 1 == bufferLen)) { - saved = 1; - break; - } - thisIndent = -1; - if ((buffer[i1] == '<') - && (buffer[i1 + 1] != '/')) { - previousIndent = nextIndent++; - thisIndent = nextIndent; - inXML = true; - } - if ((buffer[i1] == '<') - && (buffer[i1 + 1] == '/')) { - if (previousIndent > nextIndent) { - thisIndent = nextIndent; - } - previousIndent = nextIndent--; - inXML = true; - } - if ((buffer[i1] == '/') - && (buffer[i1 + 1] == '>')) { - previousIndent = nextIndent--; - inXML = true; - } - if (thisIndent != -1) { - if (thisIndent > 0) { - tmpbuffer[i2++] = (byte) '\n'; - } - for (i = tabWidth * thisIndent; i > 0; i--) { - tmpbuffer[i2++] = (byte) ' '; - } - } - atMargin = ((buffer[i1] == '\n') - || (buffer[i1] == '\r')); - if (!inXML || !atMargin) { - tmpbuffer[i2++] = buffer[i1]; - } - } - - final String inputString = new String(tmpbuffer, 0, i2); - MainView.display.syncExec(new Runnable() { - public void run() { - textArea.append(inputString); - } - }); - - // Shift saved bytes to the beginning - for (i = 0; i < saved; i++) { - buffer[i] = buffer[bufferLen - saved + i]; - } - } else { - final String inputString = new String(buffer, 0, len); - MainView.display.syncExec(new Runnable() { - public void run() { - textArea.append(inputString); - } - }); - } + protected String getSavedFirstLine() { + final String[] result = new String[1]; + MainView.display.syncExec(new Runnable() { + public void run() { + result[0] = tmodel.getItem(tableIndex).getText(MainView.REQ_COLUMN); } - - } catch (Exception e) { - e.printStackTrace(); - } finally { - done = true; - try { - if (out != null) { - out.flush(); - if (null != outSocket) { - outSocket.shutdownOutput(); - } else { - out.close(); - } - out = null; - } - } catch (Exception e) { + }); + return result[0]; + } + + protected void setSavedFirstLine(final String value) { + MainView.display.syncExec(new Runnable() { + public void run() { + tmodel.getItem(tableIndex).setText(MainView.REQ_COLUMN, value); } - try { - if (in != null) { - if (inSocket != null) { - inSocket.shutdownInput(); - } else { - in.close(); - } - in = null; - } - } catch (Exception e) { + }); + } + + protected void appendData(final String data) { + MainView.display.syncExec(new Runnable() { + public void run() { + textArea.append(data); } - myConnection.wakeUp(); - } + }); } - /** - * Method halt - */ - public void halt() { - try { - if (inSocket != null) { - inSocket.close(); - } - if (outSocket != null) { - outSocket.close(); - } - inSocket = null; - outSocket = null; - if (in != null) { - in.close(); - } - if (out != null) { - out.close(); - } - in = null; - out = null; - done = true; - } catch (Exception e) { - e.printStackTrace(); - } + protected void onFinish() { + myConnection.wakeUp(); } }