added getDebugHost() and getDebugPort() methods to Platform for convenience

This commit is contained in:
Shu Lei
2007-12-01 01:04:34 +00:00
parent 9fd34436fe
commit 3c05880012
4 changed files with 134 additions and 117 deletions

View File

@@ -70,9 +70,13 @@ import org.luaj.lib.TableLib;
*/ */
public class LuaState extends Lua { public class LuaState extends Lua {
public static final String PROPERTY_LUAJ_DEBUG = "Luaj-Debug";
protected static final String DEBUG_CLASS_NAME = "org.luaj.debug.DebugLuaState"; protected static final String DEBUG_CLASS_NAME = "org.luaj.debug.DebugLuaState";
public static final String PROPERTY_LUAJ_DEBUG = "Luaj-Debug";
public static final String PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START = "Luaj-Debug-SuspendAtStart";
public static final String PROPERTY_LUAJ_DEBUG_HOST = "Luaj-Debug-Host";
public static final String PROPERTY_LUAJ_DEBUG_PORT = "Luaj-Debug-Port";
/* thread status; 0 is OK */ /* thread status; 0 is OK */
private static final int LUA_YIELD = 1; private static final int LUA_YIELD = 1;
private static final int LUA_ERRRUN = 2; private static final int LUA_ERRRUN = 2;

View File

@@ -29,7 +29,8 @@ import java.io.Reader;
/** /**
* Singleton to manage platform-specific behaviors. * Singleton to manage platform-specific behaviors.
* *
* @deprecated - will probably be replaced with Config, LuaConfig or something similar. * @deprecated - will probably be replaced with Config, LuaConfig or something
* similar.
*/ */
abstract public class Platform { abstract public class Platform {
private static Platform instance; private static Platform instance;
@@ -37,9 +38,8 @@ abstract public class Platform {
/** /**
* Singleton to be used for platform operations. * Singleton to be used for platform operations.
* *
* The default Platform gets files as resources, * The default Platform gets files as resources, and converts them to
* and converts them to characters using the default * characters using the default InputStreamReader class.
* InputStreamReader class.
*/ */
public static Platform getInstance() { public static Platform getInstance() {
if (instance == null) { if (instance == null) {
@@ -53,7 +53,8 @@ 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) {
return System.getProperty(propertyName); return System.getProperty(propertyName);
@@ -73,8 +74,8 @@ abstract public class Platform {
/** /**
* Set the Platform instance. * Set the Platform instance.
* *
* This may be useful to define a file search path, * This may be useful to define a file search path, or custom character
* or custom character encoding conversion properties. * encoding conversion properties.
*/ */
public static void setInstance(Platform platform) { public static void setInstance(Platform platform) {
instance = platform; instance = platform;
@@ -82,28 +83,60 @@ abstract public class Platform {
/** /**
* Return an InputStream or null if not found for a particular file name. * Return an InputStream or null if not found for a particular file name.
* @param fileName Name of the file to open *
* @param fileName
* Name of the file to open
* @return InputStream or null if not found. * @return InputStream or null if not found.
*/ */
abstract public InputStream openFile(String fileName); abstract public InputStream openFile(String fileName);
/** /**
* Create Reader from an InputStream * Create Reader from an InputStream
* @param inputStream InputStream to read from *
* @param inputStream
* InputStream to read from
* @return Reader instance to use for character input * @return Reader instance to use for character input
*/ */
abstract public Reader createReader(InputStream inputStream); abstract public Reader createReader(InputStream inputStream);
/** /**
* Returns the value for the given platform property. * Returns the value for the given platform property.
* @param propertyName Property name *
* @param propertyName
* Property name
* @return Property value * @return Property value
*/ */
abstract public String getProperty(String propertyName); abstract public String getProperty(String propertyName);
/** /**
* Returns an platform dependent DebugSupport instance. * Returns an platform dependent DebugSupport instance.
*
* @return an platform dependent DebugSupport instance. * @return an platform dependent DebugSupport instance.
*/ */
abstract public DebugNetSupport getDebugSupport() throws IOException; abstract public DebugNetSupport getDebugSupport() throws IOException;
/**
* Convenience method for the subclasses to figure out the debug host.
* @return the debug host property. If it is not present, null is returned.
*/
protected String getDebugHost() {
String host = getProperty(LuaState.PROPERTY_LUAJ_DEBUG_HOST);
return host;
}
/**
* Convenience method for the subclasses to figure out the debug port.
* @return -1 if the port is not found in the platform properties; the port
* as an integer if it is present in the platform properties and valid.
*/
protected int getDebugPort() {
String portStr = getProperty(LuaState.PROPERTY_LUAJ_DEBUG_PORT);
int port = -1;
if (portStr != null) {
try {
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {}
}
return port;
}
} }

View File

@@ -50,10 +50,6 @@ import org.luaj.vm.Platform;
public class DebugLuaState extends LuaState implements DebugRequestListener { public class DebugLuaState extends LuaState implements DebugRequestListener {
public static final String PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START = "Luaj-Debug-SuspendAtStart";
public static final String PROPERTY_LUAJ_DEBUG_HOST = "Luaj-Debug-Host";
public static final String PROPERTY_LUAJ_DEBUG_PORT = "Luaj-Debug-Port";
private static final boolean TRACE = (null != System.getProperty("TRACE")); private static final boolean TRACE = (null != System.getProperty("TRACE"));
// stepping constants and stepping state // stepping constants and stepping state

View File

@@ -66,26 +66,10 @@ public class LuaJVMTest extends TestCase {
* Provides a J2SE DebugSupport instance. * Provides a J2SE DebugSupport instance.
*/ */
public DebugNetSupport getDebugSupport() throws IOException { public DebugNetSupport getDebugSupport() throws IOException {
int port = getDebugPortNumber(); int port = getDebugPort();
DebugSupportImpl debugSupport = new DebugSupportImpl(port); DebugSupportImpl debugSupport = new DebugSupportImpl(port);
return debugSupport; return debugSupport;
} }
private int getDebugPortNumber() throws IOException {
String portStr =
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;
}
}); });
} }