From 932535693bb8a719c40f63141dbfac786388ed4a Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Tue, 25 Aug 2015 19:04:27 -0400 Subject: [PATCH] Turn ILttngAgentResponse into an abstract class This allows defining a default behavior for the getBytes() method. That behavior consists of only returning the integer return code, which is what most (but not all) subclasses use. Signed-off-by: Alexandre Montplaisir Signed-off-by: Mathieu Desnoyers --- .../java/lttng-ust-agent-common/Makefile.am | 2 +- .../ust/agent/client/ISessiondCommand.java | 2 +- ...tResponse.java => LttngAgentResponse.java} | 40 ++++++------------- .../agent/client/LttngTcpSessiondClient.java | 10 ++--- .../client/SessiondDisableEventCommand.java | 16 ++------ .../client/SessiondEnableEventCommand.java | 4 +- .../client/SessiondListLoggersCommand.java | 6 +-- 7 files changed, 28 insertions(+), 52 deletions(-) rename liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/{ILttngAgentResponse.java => LttngAgentResponse.java} (74%) diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am index 26606b6b..2258fdc0 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am @@ -14,7 +14,7 @@ dist_noinst_JAVA = $(pkgpath)/AbstractLttngAgent.java \ $(pkgpath)/ILttngAgent.java \ $(pkgpath)/ILttngHandler.java \ $(pkgpath)/LTTngAgent.java \ - $(pkgpath)/client/ILttngAgentResponse.java \ + $(pkgpath)/client/LttngAgentResponse.java \ $(pkgpath)/client/ISessiondCommand.java \ $(pkgpath)/client/LttngTcpSessiondClient.java \ $(pkgpath)/client/SessiondCommandHeader.java \ diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java index 0b5a1644..c7d4faf8 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java @@ -58,5 +58,5 @@ interface ISessiondCommand { * The agent on which to execute the command * @return If the command completed successfully or not */ - public ILttngAgentResponse execute(AbstractLttngAgent agent); + public LttngAgentResponse execute(AbstractLttngAgent agent); } \ No newline at end of file diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngAgentResponse.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngAgentResponse.java similarity index 74% rename from liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngAgentResponse.java rename to liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngAgentResponse.java index 7752c950..3c5a27e5 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngAgentResponse.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngAgentResponse.java @@ -28,15 +28,15 @@ import java.nio.ByteOrder; * * @author Alexandre Montplaisir */ -interface ILttngAgentResponse { +abstract class LttngAgentResponse { - int INT_SIZE = 4; + private static final int INT_SIZE = 4; /** * Return codes used in agent responses, to indicate success or different * types of failures of the commands. */ - enum ReturnCode { + protected enum ReturnCode { CODE_SUCCESS_CMD(1), CODE_INVALID_CMD(2), @@ -60,46 +60,32 @@ interface ILttngAgentResponse { * * @return The return code */ - ReturnCode getReturnCode(); + public abstract ReturnCode getReturnCode(); /** * Gets a byte array of the response so that it may be streamed. * * @return The byte array of the response */ - byte[] getBytes(); - - ILttngAgentResponse SUCESS_RESPONSE = new ILttngAgentResponse() { + public byte[] getBytes() { + byte data[] = new byte[INT_SIZE]; + ByteBuffer buf = ByteBuffer.wrap(data); + buf.order(ByteOrder.BIG_ENDIAN); + buf.putInt(getReturnCode().getCode()); + return data; + } + public static final LttngAgentResponse SUCESS_RESPONSE = new LttngAgentResponse() { @Override public ReturnCode getReturnCode() { return ReturnCode.CODE_SUCCESS_CMD; } - - @Override - public byte[] getBytes() { - byte data[] = new byte[INT_SIZE]; - ByteBuffer buf = ByteBuffer.wrap(data); - buf.order(ByteOrder.BIG_ENDIAN); - buf.putInt(getReturnCode().getCode()); - return data; - } }; - ILttngAgentResponse FAILURE_RESPONSE = new ILttngAgentResponse() { - + public static final LttngAgentResponse FAILURE_RESPONSE = new LttngAgentResponse() { @Override public ReturnCode getReturnCode() { return ReturnCode.CODE_INVALID_CMD; } - - @Override - public byte[] getBytes() { - byte data[] = new byte[INT_SIZE]; - ByteBuffer buf = ByteBuffer.wrap(data); - buf.order(ByteOrder.BIG_ENDIAN); - buf.putInt(getReturnCode().getCode()); - return data; - } }; } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java index 3e9e24dd..8fb0f5a5 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java @@ -248,7 +248,7 @@ public class LttngTcpSessiondClient implements Runnable { case CMD_LIST: { ISessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); - ILttngAgentResponse response = listLoggerCmd.execute(logAgent); + LttngAgentResponse response = listLoggerCmd.execute(logAgent); responseData = response.getBytes(); break; } @@ -256,11 +256,11 @@ public class LttngTcpSessiondClient implements Runnable { { if (inputData == null) { /* Invalid command */ - responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes(); + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } ISessiondCommand enableCmd = new SessiondEnableEventCommand(inputData); - ILttngAgentResponse response = enableCmd.execute(logAgent); + LttngAgentResponse response = enableCmd.execute(logAgent); responseData = response.getBytes(); break; } @@ -268,11 +268,11 @@ public class LttngTcpSessiondClient implements Runnable { { if (inputData == null) { /* Invalid command */ - responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes(); + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } ISessiondCommand disableCmd = new SessiondDisableEventCommand(inputData); - ILttngAgentResponse response = disableCmd.execute(logAgent); + LttngAgentResponse response = disableCmd.execute(logAgent); responseData = response.getBytes(); break; } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java index 31d91e72..ee9d519f 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java @@ -45,29 +45,19 @@ class SessiondDisableEventCommand implements ISessiondCommand { } @Override - public ILttngAgentResponse execute(AbstractLttngAgent agent) { + public LttngAgentResponse execute(AbstractLttngAgent agent) { boolean success = agent.eventDisabled(this.eventName); - return (success ? ILttngAgentResponse.SUCESS_RESPONSE : DISABLE_EVENT_FAILURE_RESPONSE); + return (success ? LttngAgentResponse.SUCESS_RESPONSE : DISABLE_EVENT_FAILURE_RESPONSE); } /** * Response sent when the disable-event command asks to disable an * unknown event. */ - private static final ILttngAgentResponse DISABLE_EVENT_FAILURE_RESPONSE = new ILttngAgentResponse() { - + private static final LttngAgentResponse DISABLE_EVENT_FAILURE_RESPONSE = new LttngAgentResponse() { @Override public ReturnCode getReturnCode() { return ReturnCode.CODE_UNK_LOGGER_NAME; } - - @Override - public byte[] getBytes() { - byte data[] = new byte[INT_SIZE]; - ByteBuffer buf = ByteBuffer.wrap(data); - buf.order(ByteOrder.BIG_ENDIAN); - buf.putInt(getReturnCode().getCode()); - return data; - } }; } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java index 5f0fbd94..bb20ac57 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java @@ -51,8 +51,8 @@ class SessiondEnableEventCommand implements ISessiondCommand { } @Override - public ILttngAgentResponse execute(AbstractLttngAgent agent) { + public LttngAgentResponse execute(AbstractLttngAgent agent) { boolean success = agent.eventEnabled(this.eventName); - return (success ? ILttngAgentResponse.SUCESS_RESPONSE : ILttngAgentResponse.FAILURE_RESPONSE); + return (success ? LttngAgentResponse.SUCESS_RESPONSE : LttngAgentResponse.FAILURE_RESPONSE); } } diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java index cb20ea90..f5ad9c50 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java @@ -35,7 +35,7 @@ import org.lttng.ust.agent.AbstractLttngAgent; class SessiondListLoggersCommand implements ISessiondCommand { @Override - public ILttngAgentResponse execute(AbstractLttngAgent agent) { + public LttngAgentResponse execute(AbstractLttngAgent agent) { final List loggerList = new ArrayList(); int dataSize = 0; @@ -47,7 +47,7 @@ class SessiondListLoggersCommand implements ISessiondCommand { return new SessiondListLoggersResponse(loggerList, dataSize); } - private static class SessiondListLoggersResponse implements ILttngAgentResponse { + private static class SessiondListLoggersResponse extends LttngAgentResponse { private final static int SIZE = 12; @@ -62,7 +62,7 @@ class SessiondListLoggersCommand implements ISessiondCommand { @Override public ReturnCode getReturnCode() { /* This command can't really fail */ - return ILttngAgentResponse.SUCESS_RESPONSE.getReturnCode(); + return ReturnCode.CODE_SUCCESS_CMD; } @Override -- 2.34.1