diff --git a/src/core/org/luaj/vm/DebugNetSupport.java b/src/core/org/luaj/vm/DebugNetSupport.java new file mode 100644 index 00000000..c0d40a5a --- /dev/null +++ b/src/core/org/luaj/vm/DebugNetSupport.java @@ -0,0 +1,29 @@ +package org.luaj.vm; + +import java.io.IOException; + +public interface DebugNetSupport { + + /** + * Starts the networking for debug support. + * @throws IOException + */ + public abstract void start() throws IOException; + + /** + * Shuts down the networking for the debug support. + */ + public abstract void stop(); + + /** + * Disconnect all connected clients. + */ + public abstract void disconnect(); + + /** + * Disconnect the client with the given id. + * @param id -- client id + */ + public abstract void disconnect(int id); + +} \ No newline at end of file diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 58cf1e7e..58cf9848 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -131,8 +131,8 @@ public class LuaState extends Lua { */ public static LuaState newState() { String isDebugStr - = Platform.getInstance().getProperty(PROPERTY_LUAJ_DEBUG, "false"); - boolean isDebug = Boolean.parseBoolean(isDebugStr); + = Platform.getInstance().getProperty(PROPERTY_LUAJ_DEBUG); + boolean isDebug = (isDebugStr != null && "true".equalsIgnoreCase(isDebugStr)); LuaState vm = null; if ( isDebug ) { diff --git a/src/core/org/luaj/vm/Platform.java b/src/core/org/luaj/vm/Platform.java index 06cc25be..be081bb2 100644 --- a/src/core/org/luaj/vm/Platform.java +++ b/src/core/org/luaj/vm/Platform.java @@ -26,10 +26,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import org.luaj.debug.DebugLuaState; -import org.luaj.debug.net.DebugSupport; -import org.luaj.debug.net.j2se.DebugSupportImpl; - /** * Singleton to manage platform-specific behaviors. * @@ -59,22 +55,15 @@ abstract public class Platform { /** * Assumes J2SE platform, return the corresponding system property */ - public String getProperty(String propertyName, - String defaultValue) { - return System.getProperty(propertyName, defaultValue); + public String getProperty(String propertyName) { + return System.getProperty(propertyName); } /** * Provides a J2SE DebugSupport instance. */ - public DebugSupport getDebugSupport() throws IOException { - String portStr = getProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "-1"); - try { - int port = Integer.parseInt(portStr); - return new DebugSupportImpl(port); - } catch (NumberFormatException e) { - throw new IOException("Bad port number: " + portStr); - } + public DebugNetSupport getDebugSupport() throws IOException { + return null; } }; } @@ -108,14 +97,13 @@ abstract public class Platform { /** * Returns the value for the given platform property. * @param propertyName Property name - * @param defaultValue Default property value * @return Property value */ - abstract public String getProperty(String propertyName, String defaultValue); + abstract public String getProperty(String propertyName); /** * Returns an platform dependent DebugSupport instance. - * @return an plaform dependent DebugSupport instance. + * @return an platform dependent DebugSupport instance. */ - abstract public DebugSupport getDebugSupport() throws IOException; + abstract public DebugNetSupport getDebugSupport() throws IOException; } diff --git a/src/debug/org/luaj/debug/DebugLuaState.java b/src/debug/org/luaj/debug/DebugLuaState.java index 1d12984f..3f33cdab 100644 --- a/src/debug/org/luaj/debug/DebugLuaState.java +++ b/src/debug/org/luaj/debug/DebugLuaState.java @@ -28,7 +28,7 @@ import java.util.Vector; import org.luaj.compiler.LexState; import org.luaj.debug.event.DebugEventBreakpoint; import org.luaj.debug.event.DebugEventError; -import org.luaj.debug.net.DebugSupport; +import org.luaj.debug.net.DebugNetSupportBase; import org.luaj.debug.request.DebugRequestDisconnect; import org.luaj.debug.request.DebugRequestLineBreakpointToggle; import org.luaj.debug.request.DebugRequestListener; @@ -37,6 +37,7 @@ import org.luaj.debug.response.DebugResponseCallgraph; import org.luaj.debug.response.DebugResponseStack; import org.luaj.debug.response.DebugResponseVariables; import org.luaj.vm.CallInfo; +import org.luaj.vm.DebugNetSupport; import org.luaj.vm.LClosure; import org.luaj.vm.LPrototype; import org.luaj.vm.LTable; @@ -70,7 +71,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { protected boolean bSuspendOnStart = false; protected int lastline = -1; protected String lastSource; - protected DebugSupport debugSupport; + protected DebugNetSupportBase debugSupport; protected LuaErrorException lastError; /** @@ -84,31 +85,31 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { Platform platform = Platform.getInstance(); // set if the vm should be suspended at start - String suspendOnStartStr = platform.getProperty(PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START, "false"); - boolean bSuspendOnStart = Boolean.parseBoolean(suspendOnStartStr); + String suspendOnStartStr = platform.getProperty(PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START); + boolean bSuspendOnStart = (suspendOnStartStr != null && "true".equalsIgnoreCase(suspendOnStartStr)); setSuspendAtStart(bSuspendOnStart); // set up the debug networking support try { - DebugSupport debugSupport = platform.getDebugSupport(); + DebugNetSupport debugSupport = platform.getDebugSupport(); if (debugSupport != null) setDebugSupport(debugSupport); else - System.out.println("Warning: DebugSupport is not implemented."); + System.out.println("Warning: DebugNetSupportBase is not implemented."); } catch (IOException e) { // no debug client can talk to VM, but VM can continue functioning System.out.println("Warning: no debug client support due to error: " + e.getMessage()); } } - protected void setDebugSupport(DebugSupport debugSupport) + protected void setDebugSupport(DebugNetSupport debugSupport) throws IOException { if (debugSupport == null) { - throw new IllegalArgumentException("DebugSupport cannot be null"); + throw new IllegalArgumentException("DebugNetSupportBase cannot be null"); } - this.debugSupport = debugSupport; - debugSupport.setDebugStackState(this); + this.debugSupport = (DebugNetSupportBase) debugSupport; + this.debugSupport.setDebugStackState(this); debugSupport.start(); } @@ -304,7 +305,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { public void handleRequest(DebugMessage request) { if (this.debugSupport == null) { throw new IllegalStateException( - "DebugSupport must be defined."); + "DebugNetSupportBase must be defined."); } if (TRACE) @@ -442,7 +443,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { public void disconnect(int connectionId) { if (this.debugSupport == null) { throw new IllegalStateException( - "DebugSupport must be defined."); + "DebugNetSupportBase must be defined."); } reset(); @@ -454,7 +455,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { public void disconnectFromDebugService() { if (this.debugSupport == null) { throw new IllegalStateException( - "DebugSupport must be defined."); + "DebugNetSupportBase must be defined."); } reset(); diff --git a/src/debug/org/luaj/debug/net/DebugSupport.java b/src/debug/org/luaj/debug/net/DebugNetSupportBase.java similarity index 67% rename from src/debug/org/luaj/debug/net/DebugSupport.java rename to src/debug/org/luaj/debug/net/DebugNetSupportBase.java index 30c27d34..9900767b 100644 --- a/src/debug/org/luaj/debug/net/DebugSupport.java +++ b/src/debug/org/luaj/debug/net/DebugNetSupportBase.java @@ -6,25 +6,31 @@ import org.luaj.debug.DebugLuaState; import org.luaj.debug.DebugMessage; import org.luaj.debug.event.DebugEventListener; import org.luaj.debug.request.DebugRequestListener; +import org.luaj.vm.DebugNetSupport; /** - * DebugSupport provides the network communication support between the luaj-vm + * DebugNetSupportBase provides the network communication support between the luaj-vm * and debug clients. */ -public abstract class DebugSupport implements DebugRequestListener, DebugEventListener { +public abstract class DebugNetSupportBase implements DebugRequestListener, DebugEventListener, DebugNetSupport { + /* (non-Javadoc) + * @see org.luaj.debug.net.DebugNetSupport#start() + */ public abstract void start() throws IOException; + /* (non-Javadoc) + * @see org.luaj.debug.net.DebugNetSupport#stop() + */ public abstract void stop(); - /** - * Disconnect all connected clients. + /* (non-Javadoc) + * @see org.luaj.debug.net.DebugNetSupport#disconnect() */ public abstract void disconnect(); - /** - * Disconnect the client with the given id. - * @param id -- client id + /* (non-Javadoc) + * @see org.luaj.debug.net.DebugNetSupport#disconnect(int) */ public abstract void disconnect(int id); diff --git a/src/debug/org/luaj/debug/net/j2me/ClientConnectionTask.java b/src/debug/org/luaj/debug/net/j2me/ClientConnectionTask.java index 63f2b37d..bdb34e18 100644 --- a/src/debug/org/luaj/debug/net/j2me/ClientConnectionTask.java +++ b/src/debug/org/luaj/debug/net/j2me/ClientConnectionTask.java @@ -19,7 +19,7 @@ public class ClientConnectionTask implements Runnable, DebugEventListener { protected String host; protected int port; - DebugSupportImpl debugSupport; + protected DebugSupportImpl debugSupport; protected boolean bDisconnected = false; protected SocketConnection connection; diff --git a/src/debug/org/luaj/debug/net/j2me/DebugSupportImpl.java b/src/debug/org/luaj/debug/net/j2me/DebugSupportImpl.java index 784e4201..463769f4 100644 --- a/src/debug/org/luaj/debug/net/j2me/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/net/j2me/DebugSupportImpl.java @@ -3,14 +3,14 @@ package org.luaj.debug.net.j2me; import java.io.IOException; import org.luaj.debug.DebugMessage; -import org.luaj.debug.net.DebugSupport; +import org.luaj.debug.net.DebugNetSupportBase; /** - * J2ME version of DebugSupport implementation. The vm will connect to a debug + * J2ME version of DebugNetSupportBase implementation. The vm will connect to a debug * service hosted on a remote machine and have the debug service relay the * debugging messages between the vm and the debug client. */ -public class DebugSupportImpl extends DebugSupport { +public class DebugSupportImpl extends DebugNetSupportBase { protected String host; protected int port; ClientConnectionTask clientTask; diff --git a/src/debug/org/luaj/debug/net/j2se/DebugSupportImpl.java b/src/debug/org/luaj/debug/net/j2se/DebugSupportImpl.java index 66102665..4e773373 100644 --- a/src/debug/org/luaj/debug/net/j2se/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/net/j2se/DebugSupportImpl.java @@ -26,14 +26,14 @@ import java.net.ServerSocket; import java.net.Socket; import org.luaj.debug.DebugMessage; -import org.luaj.debug.net.DebugSupport; +import org.luaj.debug.net.DebugNetSupportBase; /** - * J2SE version of DebugSupport. The luaj-vm opens a port accepting the debug + * J2SE version of DebugNetSupportBase. The luaj-vm opens a port accepting the debug * client connections. The current implementation allows the vm to accept one * and only one debug client connection at any time. */ -public class DebugSupportImpl extends DebugSupport { +public class DebugSupportImpl extends DebugNetSupportBase { protected int numClientConnectionsAllowed; protected int numClientConnections = 0; @@ -42,7 +42,7 @@ public class DebugSupportImpl extends DebugSupport { protected ClientConnectionTask clientConnectionTask; /** - * Creates an instance of DebugSupportImpl at the given port + * Creates an instance of DebugNetSupportBase at the given port * @param debugPort * @throws IOException */ diff --git a/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java index c0978691..2f9f5ac2 100644 --- a/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java +++ b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java @@ -21,6 +21,10 @@ ******************************************************************************/ package org.luaj.debug.j2se; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URL; import java.util.Properties; @@ -28,12 +32,63 @@ import junit.framework.TestCase; import org.luaj.debug.DebugLuaState; import org.luaj.debug.j2se.StandardLuaJVM.ParseException; +import org.luaj.debug.net.j2se.DebugSupportImpl; +import org.luaj.vm.DebugNetSupport; import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; /** * Sanity test for StandardLuaJVM. */ public class LuaJVMTest extends TestCase { + protected void setUp() throws Exception { + super.setUp(); + + System.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "1999"); + + Platform.setInstance(new Platform() { + public Reader createReader(InputStream inputStream) { + return new InputStreamReader(inputStream); + } + + public InputStream openFile(String fileName) { + return getClass().getResourceAsStream("/" + fileName); + } + + /** + * Assumes J2SE platform, return the corresponding system property + */ + public String getProperty(String propertyName) { + return System.getProperty(propertyName); + } + + /** + * Provides a J2SE DebugSupport instance. + */ + public DebugNetSupport getDebugSupport() throws IOException { + int port = getDebugPortNumber(); + DebugSupportImpl debugSupport = new DebugSupportImpl(port); + return debugSupport; + } + + private int getDebugPortNumber() throws IOException { + String portStr = + Platform.getInstance().getProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT); + int port = -1; + if (portStr == null) { + throw new IOException("Port number must be specified in the System property"); + } else { + try { + port = Integer.parseInt(portStr); + } catch (NumberFormatException e) { + throw new IOException("Bad port number: " + portStr); + } + } + return port; + } + }); + } + public void testCommandLineParse() { // null arguments String[] args = null;