Added LightOptionParser, NullOutputStream, bugfixes.

LightOptionParser
-----------------

Lightweight, fairly featureful option parsing mechanism idiomatic to Groovy.
Takes in a map of parameter definitions and returns a map of option values and
arguments.

NullOutputStream
----------------

Ignores all data sent its way.

ParameterizedSocket
-------------------

Added trace and debug logging to the message handling code.

SmartConfig
-----------

Fixed a bug where a property lookup on a non-existent property with no default
given caused a null pointer exception trying to resolve the class of the default
value.
This commit is contained in:
Jonathan Bernard
2012-11-27 08:40:17 -06:00
parent a186d6a09d
commit c887a49f8c
5 changed files with 114 additions and 6 deletions

View File

@ -12,6 +12,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
public class ParameterizedSocket {
@ -50,6 +52,7 @@ public class ParameterizedSocket {
private Socket socket;
private byte[] buffer;
private Charset charset;
private Logger log = LoggerFactory.getLogger(getClass());
public ParameterizedSocket(Socket socket, int bufferSize) {
this.socket = socket;
@ -58,7 +61,8 @@ public class ParameterizedSocket {
public ParameterizedSocket(String ipAddress, int port, int bufferSize)
throws UnknownHostException, IOException {
this(new Socket(ipAddress, port), bufferSize); }
this(new Socket(ipAddress, port), bufferSize);
log.trace("Opened a socket to {}:{}", ipAddress, port); }
public ParameterizedSocket(Socket socket)
throws UnknownHostException, IOException {
@ -76,6 +80,7 @@ public class ParameterizedSocket {
public void writeMessage(Message message) throws IOException {
if (message == null || message.parts.size() == 0) return;
log.debug("Writing a message: [{}].", message);
byte[] messageBytes = formatMessage(message);
socket.getOutputStream().write(messageBytes); }
@ -83,6 +88,8 @@ public class ParameterizedSocket {
List<String> messageList = new ArrayList<String>();
Map<String, String> namedParameters = new HashMap<String, String>();
log.trace("Reading a message.");
if (socket.getInputStream().read() != START_TOKEN_BYTE) {
byte[] errMsg = formatMessage("ERROR", "Invalid command (expected START_TOKEN).");
socket.getOutputStream().write(errMsg); }
@ -135,7 +142,9 @@ public class ParameterizedSocket {
else namedParameters.put(paramName,
new String(buffer, 0, bufIdx, charset)); }
return new Message(messageList, namedParameters); }
Message message = new Message(messageList, namedParameters);
log.debug("Message read: [{}].", message);
return message; }
protected byte[] formatMessage(String... parts) {
return formatMessage(new Message(parts)); }