From c5ea09d09d03bcf3fad2da8eee53546213d0035e Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 15 Mar 2020 14:35:18 +0100 Subject: [PATCH] messaging/broker : update handlers --- .../messagebroker/BaseMessageHandler.java | 11 +++++++ .../java/messagebroker/MessageBroker.java | 10 ++++-- .../java/messagebroker/MessageHandler.java | 1 + .../messagebroker/MessageHandlerFactory.java | 5 +-- .../handlers/AddHandlerMessageHandler.java | 1 + .../handlers/EchoMessageHandler.java | 2 +- .../handlers/ListHandlersMessageHandler.java | 32 +++++++++++++++++++ .../messagebroker/handlers/NewHandler.java | 19 ----------- 8 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 messaging/src/main/java/messagebroker/handlers/ListHandlersMessageHandler.java delete mode 100644 messaging/src/main/java/messagebroker/handlers/NewHandler.java diff --git a/messaging/src/main/java/messagebroker/BaseMessageHandler.java b/messaging/src/main/java/messagebroker/BaseMessageHandler.java index e0e07c9..19ada37 100644 --- a/messaging/src/main/java/messagebroker/BaseMessageHandler.java +++ b/messaging/src/main/java/messagebroker/BaseMessageHandler.java @@ -1,8 +1,12 @@ package messagebroker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.PrintWriter; public abstract class BaseMessageHandler implements MessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(BaseMessageHandler.class); private MessageHandler nextHandler; private final MessageHandlerContext context; @@ -14,9 +18,11 @@ public abstract class BaseMessageHandler implements MessageHandler { public void handleMessage(String message) throws UnknownMessageFormatException { if (nextHandler != null) { + LOGGER.debug("Trying next handler in chain: {}", nextHandler); nextHandler.handleMessage(message); } else { String msg = "Unable to handle message " + message; + LOGGER.warn(msg); throw new UnknownMessageFormatException(msg); } } @@ -24,6 +30,11 @@ public abstract class BaseMessageHandler implements MessageHandler { @Override public void setNextHandler(MessageHandler nextHandler) { this.nextHandler = nextHandler; + LOGGER.debug("Next handler of {} set to {}", getClass().getCanonicalName(), nextHandler.getClass().getCanonicalName()); + } + + public MessageHandler getNextHandler() { + return nextHandler; } protected MessageHandlerContext getContext() { diff --git a/messaging/src/main/java/messagebroker/MessageBroker.java b/messaging/src/main/java/messagebroker/MessageBroker.java index 4897e8c..6111e5a 100644 --- a/messaging/src/main/java/messagebroker/MessageBroker.java +++ b/messaging/src/main/java/messagebroker/MessageBroker.java @@ -1,12 +1,13 @@ package messagebroker; +import messagebroker.handlers.AddHandlerMessageHandler; +import messagebroker.handlers.ListHandlersMessageHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.lang.reflect.InvocationTargetException; import java.net.ServerSocket; import java.net.Socket; @@ -44,6 +45,10 @@ public class MessageBroker { messageHandlerFactory.addHandler(handlerName); } + public MessageHandler getHandler() { + return messageHandlerFactory.getHandler(); + } + private class ClientHandler implements Runnable { private final Socket socket; @@ -82,7 +87,8 @@ public class MessageBroker { int port = Integer.parseInt(args[0]); MessageBroker broker = new MessageBroker(port); - broker.addHandler("messagebroker.handlers.AddHandler"); + broker.addHandler(AddHandlerMessageHandler.class.getCanonicalName()); + broker.addHandler(ListHandlersMessageHandler.class.getCanonicalName()); broker.run(); } } diff --git a/messaging/src/main/java/messagebroker/MessageHandler.java b/messaging/src/main/java/messagebroker/MessageHandler.java index 1290dab..d575baa 100644 --- a/messaging/src/main/java/messagebroker/MessageHandler.java +++ b/messaging/src/main/java/messagebroker/MessageHandler.java @@ -2,5 +2,6 @@ package messagebroker; public interface MessageHandler { void handleMessage(String message) throws UnknownMessageFormatException; + MessageHandler getNextHandler(); void setNextHandler(MessageHandler nextHandler); } diff --git a/messaging/src/main/java/messagebroker/MessageHandlerFactory.java b/messaging/src/main/java/messagebroker/MessageHandlerFactory.java index b42bf50..7cce60f 100644 --- a/messaging/src/main/java/messagebroker/MessageHandlerFactory.java +++ b/messaging/src/main/java/messagebroker/MessageHandlerFactory.java @@ -3,9 +3,6 @@ package messagebroker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.reflect.InvocationTargetException; -import java.util.*; - class MessageHandlerFactory { private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandlerFactory.class); @@ -23,7 +20,7 @@ class MessageHandlerFactory { Class handlerClass = Class.forName(handlerName) .asSubclass(MessageHandler.class); - LOGGER.debug("Class {} found for handler name {}", handlerClass, handlerName); + LOGGER.debug("Class {} found for handler name {}", handlerClass.getCanonicalName(), handlerName); MessageHandler next = newMessageHandler(handlerClass); if (firstHandler == null) { firstHandler = next; diff --git a/messaging/src/main/java/messagebroker/handlers/AddHandlerMessageHandler.java b/messaging/src/main/java/messagebroker/handlers/AddHandlerMessageHandler.java index 8734c8b..b5048fa 100644 --- a/messaging/src/main/java/messagebroker/handlers/AddHandlerMessageHandler.java +++ b/messaging/src/main/java/messagebroker/handlers/AddHandlerMessageHandler.java @@ -33,6 +33,7 @@ public class AddHandlerMessageHandler extends BaseMessageHandler { sendResponse(msg); } } else { + LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", msgPattern, message); super.handleMessage(message); } } diff --git a/messaging/src/main/java/messagebroker/handlers/EchoMessageHandler.java b/messaging/src/main/java/messagebroker/handlers/EchoMessageHandler.java index 879d25f..6eafe26 100644 --- a/messaging/src/main/java/messagebroker/handlers/EchoMessageHandler.java +++ b/messaging/src/main/java/messagebroker/handlers/EchoMessageHandler.java @@ -6,7 +6,6 @@ import messagebroker.UnknownMessageFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.PrintWriter; import java.util.regex.Pattern; public class EchoMessageHandler extends BaseMessageHandler { @@ -25,6 +24,7 @@ public class EchoMessageHandler extends BaseMessageHandler { LOGGER.info("Parsed message {}", parsed); sendResponse(parsed); } else { + LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message); super.handleMessage(message); } } diff --git a/messaging/src/main/java/messagebroker/handlers/ListHandlersMessageHandler.java b/messaging/src/main/java/messagebroker/handlers/ListHandlersMessageHandler.java new file mode 100644 index 0000000..14f649d --- /dev/null +++ b/messaging/src/main/java/messagebroker/handlers/ListHandlersMessageHandler.java @@ -0,0 +1,32 @@ +package messagebroker.handlers; + +import messagebroker.BaseMessageHandler; +import messagebroker.MessageHandlerContext; +import messagebroker.UnknownMessageFormatException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ListHandlersMessageHandler extends BaseMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(EchoMessageHandler.class); + private static final String PATTERN = "CONTROL.LIST_HANDLERS"; + + public ListHandlersMessageHandler(MessageHandlerContext context) { + super(context); + } + + @Override + public void handleMessage(String message) throws UnknownMessageFormatException { + if (message.equals(PATTERN)) { + LOGGER.info("Received message {}", message); + var response = new StringBuilder(); + for (var nextHandler = getContext().getBroker().getHandler(); nextHandler != null; nextHandler = nextHandler.getNextHandler()){ + response.append(nextHandler.getClass().getCanonicalName()) + .append("\n"); + } + sendResponse(response.toString()); + } else { + LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message); + super.handleMessage(message); + } + } +} diff --git a/messaging/src/main/java/messagebroker/handlers/NewHandler.java b/messaging/src/main/java/messagebroker/handlers/NewHandler.java deleted file mode 100644 index 4844af1..0000000 --- a/messaging/src/main/java/messagebroker/handlers/NewHandler.java +++ /dev/null @@ -1,19 +0,0 @@ -package messagebroker.handlers; - -import messagebroker.BaseMessageHandler; -import messagebroker.MessageHandlerContext; -import messagebroker.UnknownMessageFormatException; - -public class NewHandler extends BaseMessageHandler { - private final MessageHandlerContext context; - - public NewHandler(MessageHandlerContext context) { - super(context); - this.context = context; - } - - @Override - public void handleMessage(String message) throws UnknownMessageFormatException { - super.handleMessage(message); - } -}