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() {
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 ) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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
*/

View File

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