continuation of previous work to add factory method to create LuaState or DebugLuaState dynamically, refactored DebugSupport so that Platform.getInstance().getDebugSupport() does not depend on debug module

This commit is contained in:
Shu Lei
2007-12-01 00:17:18 +00:00
parent 96864694de
commit ca721124e1
9 changed files with 128 additions and 49 deletions

View File

@@ -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);
}

View File

@@ -131,8 +131,8 @@ public class LuaState extends Lua {
*/ */
public static LuaState newState() { public static LuaState newState() {
String isDebugStr String isDebugStr
= Platform.getInstance().getProperty(PROPERTY_LUAJ_DEBUG, "false"); = Platform.getInstance().getProperty(PROPERTY_LUAJ_DEBUG);
boolean isDebug = Boolean.parseBoolean(isDebugStr); boolean isDebug = (isDebugStr != null && "true".equalsIgnoreCase(isDebugStr));
LuaState vm = null; LuaState vm = null;
if ( isDebug ) { if ( isDebug ) {

View File

@@ -26,10 +26,6 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; 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. * Singleton to manage platform-specific behaviors.
* *
@@ -59,22 +55,15 @@ abstract public class Platform {
/** /**
* Assumes J2SE platform, return the corresponding system property * Assumes J2SE platform, return the corresponding system property
*/ */
public String getProperty(String propertyName, public String getProperty(String propertyName) {
String defaultValue) { return System.getProperty(propertyName);
return System.getProperty(propertyName, defaultValue);
} }
/** /**
* Provides a J2SE DebugSupport instance. * Provides a J2SE DebugSupport instance.
*/ */
public DebugSupport getDebugSupport() throws IOException { public DebugNetSupport getDebugSupport() throws IOException {
String portStr = getProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "-1"); return null;
try {
int port = Integer.parseInt(portStr);
return new DebugSupportImpl(port);
} catch (NumberFormatException e) {
throw new IOException("Bad port number: " + portStr);
}
} }
}; };
} }
@@ -108,14 +97,13 @@ abstract public class Platform {
/** /**
* Returns the value for the given platform property. * Returns the value for the given platform property.
* @param propertyName Property name * @param propertyName Property name
* @param defaultValue Default property value
* @return 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. * 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;
} }

View File

@@ -28,7 +28,7 @@ import java.util.Vector;
import org.luaj.compiler.LexState; import org.luaj.compiler.LexState;
import org.luaj.debug.event.DebugEventBreakpoint; import org.luaj.debug.event.DebugEventBreakpoint;
import org.luaj.debug.event.DebugEventError; 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.DebugRequestDisconnect;
import org.luaj.debug.request.DebugRequestLineBreakpointToggle; import org.luaj.debug.request.DebugRequestLineBreakpointToggle;
import org.luaj.debug.request.DebugRequestListener; 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.DebugResponseStack;
import org.luaj.debug.response.DebugResponseVariables; import org.luaj.debug.response.DebugResponseVariables;
import org.luaj.vm.CallInfo; import org.luaj.vm.CallInfo;
import org.luaj.vm.DebugNetSupport;
import org.luaj.vm.LClosure; import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype; import org.luaj.vm.LPrototype;
import org.luaj.vm.LTable; import org.luaj.vm.LTable;
@@ -70,7 +71,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
protected boolean bSuspendOnStart = false; protected boolean bSuspendOnStart = false;
protected int lastline = -1; protected int lastline = -1;
protected String lastSource; protected String lastSource;
protected DebugSupport debugSupport; protected DebugNetSupportBase debugSupport;
protected LuaErrorException lastError; protected LuaErrorException lastError;
/** /**
@@ -84,31 +85,31 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
Platform platform = Platform.getInstance(); Platform platform = Platform.getInstance();
// set if the vm should be suspended at start // set if the vm should be suspended at start
String suspendOnStartStr = platform.getProperty(PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START, "false"); String suspendOnStartStr = platform.getProperty(PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START);
boolean bSuspendOnStart = Boolean.parseBoolean(suspendOnStartStr); boolean bSuspendOnStart = (suspendOnStartStr != null && "true".equalsIgnoreCase(suspendOnStartStr));
setSuspendAtStart(bSuspendOnStart); setSuspendAtStart(bSuspendOnStart);
// set up the debug networking support // set up the debug networking support
try { try {
DebugSupport debugSupport = platform.getDebugSupport(); DebugNetSupport debugSupport = platform.getDebugSupport();
if (debugSupport != null) if (debugSupport != null)
setDebugSupport(debugSupport); setDebugSupport(debugSupport);
else else
System.out.println("Warning: DebugSupport is not implemented."); System.out.println("Warning: DebugNetSupportBase is not implemented.");
} catch (IOException e) { } catch (IOException e) {
// no debug client can talk to VM, but VM can continue functioning // 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()); 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 { throws IOException {
if (debugSupport == null) { if (debugSupport == null) {
throw new IllegalArgumentException("DebugSupport cannot be null"); throw new IllegalArgumentException("DebugNetSupportBase cannot be null");
} }
this.debugSupport = debugSupport; this.debugSupport = (DebugNetSupportBase) debugSupport;
debugSupport.setDebugStackState(this); this.debugSupport.setDebugStackState(this);
debugSupport.start(); debugSupport.start();
} }
@@ -304,7 +305,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
public void handleRequest(DebugMessage request) { public void handleRequest(DebugMessage request) {
if (this.debugSupport == null) { if (this.debugSupport == null) {
throw new IllegalStateException( throw new IllegalStateException(
"DebugSupport must be defined."); "DebugNetSupportBase must be defined.");
} }
if (TRACE) if (TRACE)
@@ -442,7 +443,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
public void disconnect(int connectionId) { public void disconnect(int connectionId) {
if (this.debugSupport == null) { if (this.debugSupport == null) {
throw new IllegalStateException( throw new IllegalStateException(
"DebugSupport must be defined."); "DebugNetSupportBase must be defined.");
} }
reset(); reset();
@@ -454,7 +455,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
public void disconnectFromDebugService() { public void disconnectFromDebugService() {
if (this.debugSupport == null) { if (this.debugSupport == null) {
throw new IllegalStateException( throw new IllegalStateException(
"DebugSupport must be defined."); "DebugNetSupportBase must be defined.");
} }
reset(); reset();

View File

@@ -6,25 +6,31 @@ import org.luaj.debug.DebugLuaState;
import org.luaj.debug.DebugMessage; import org.luaj.debug.DebugMessage;
import org.luaj.debug.event.DebugEventListener; import org.luaj.debug.event.DebugEventListener;
import org.luaj.debug.request.DebugRequestListener; 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. * 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; public abstract void start() throws IOException;
/* (non-Javadoc)
* @see org.luaj.debug.net.DebugNetSupport#stop()
*/
public abstract void stop(); public abstract void stop();
/** /* (non-Javadoc)
* Disconnect all connected clients. * @see org.luaj.debug.net.DebugNetSupport#disconnect()
*/ */
public abstract void disconnect(); public abstract void disconnect();
/** /* (non-Javadoc)
* Disconnect the client with the given id. * @see org.luaj.debug.net.DebugNetSupport#disconnect(int)
* @param id -- client id
*/ */
public abstract void disconnect(int id); public abstract void disconnect(int id);

View File

@@ -19,7 +19,7 @@ public class ClientConnectionTask implements Runnable, DebugEventListener {
protected String host; protected String host;
protected int port; protected int port;
DebugSupportImpl debugSupport; protected DebugSupportImpl debugSupport;
protected boolean bDisconnected = false; protected boolean bDisconnected = false;
protected SocketConnection connection; protected SocketConnection connection;

View File

@@ -3,14 +3,14 @@ package org.luaj.debug.net.j2me;
import java.io.IOException; import java.io.IOException;
import org.luaj.debug.DebugMessage; 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 * service hosted on a remote machine and have the debug service relay the
* debugging messages between the vm and the debug client. * debugging messages between the vm and the debug client.
*/ */
public class DebugSupportImpl extends DebugSupport { public class DebugSupportImpl extends DebugNetSupportBase {
protected String host; protected String host;
protected int port; protected int port;
ClientConnectionTask clientTask; ClientConnectionTask clientTask;

View File

@@ -26,14 +26,14 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import org.luaj.debug.DebugMessage; 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 * client connections. The current implementation allows the vm to accept one
* and only one debug client connection at any time. * and only one debug client connection at any time.
*/ */
public class DebugSupportImpl extends DebugSupport { public class DebugSupportImpl extends DebugNetSupportBase {
protected int numClientConnectionsAllowed; protected int numClientConnectionsAllowed;
protected int numClientConnections = 0; protected int numClientConnections = 0;
@@ -42,7 +42,7 @@ public class DebugSupportImpl extends DebugSupport {
protected ClientConnectionTask clientConnectionTask; protected ClientConnectionTask clientConnectionTask;
/** /**
* Creates an instance of DebugSupportImpl at the given port * Creates an instance of DebugNetSupportBase at the given port
* @param debugPort * @param debugPort
* @throws IOException * @throws IOException
*/ */

View File

@@ -21,6 +21,10 @@
******************************************************************************/ ******************************************************************************/
package org.luaj.debug.j2se; 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.net.URL;
import java.util.Properties; import java.util.Properties;
@@ -28,12 +32,63 @@ import junit.framework.TestCase;
import org.luaj.debug.DebugLuaState; import org.luaj.debug.DebugLuaState;
import org.luaj.debug.j2se.StandardLuaJVM.ParseException; 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.LuaState;
import org.luaj.vm.Platform;
/** /**
* Sanity test for StandardLuaJVM. * Sanity test for StandardLuaJVM.
*/ */
public class LuaJVMTest extends TestCase { 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() { public void testCommandLineParse() {
// null arguments // null arguments
String[] args = null; String[] args = null;