diff --git a/broker/src/main/java/org/eclipse/moquette/interception/DefaultInterceptHandler.java b/broker/src/main/java/org/eclipse/moquette/interception/DefaultInterceptHandler.java new file mode 100644 index 00000000..44cbd748 --- /dev/null +++ b/broker/src/main/java/org/eclipse/moquette/interception/DefaultInterceptHandler.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012-2014 The original author or authors + * ------------------------------------------------------ + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ +package org.eclipse.moquette.interception; + +import org.eclipse.moquette.proto.messages.*; +import org.eclipse.moquette.spi.impl.subscriptions.Subscription; + +/** + * Default empty implementation that can be used to create custom handlers + * without defining every method. + * + * @author Wagner Macedo + */ +public class DefaultInterceptHandler implements InterceptHandler { + @Override + public void onConnect(ConnectMessage msg) { + } + + @Override + public void onDisconnect(String clientID) { + } + + @Override + public void onPublish(PublishMessage msg) { + } + + @Override + public void onSubscribe(Subscription sub) { + } + + @Override + public void onUnsubscribe(Subscription sub) { + } +} diff --git a/broker/src/main/java/org/eclipse/moquette/interception/InterceptHandler.java b/broker/src/main/java/org/eclipse/moquette/interception/InterceptHandler.java new file mode 100644 index 00000000..97fca7fd --- /dev/null +++ b/broker/src/main/java/org/eclipse/moquette/interception/InterceptHandler.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2012-2014 The original author or authors + * ------------------------------------------------------ + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ +package org.eclipse.moquette.interception; + +import org.eclipse.moquette.proto.messages.*; +import org.eclipse.moquette.spi.impl.subscriptions.Subscription; + +/** + * This interface is used to inject code for intercepting broker events. + *
+ * The events can act only as observers. + *
+ * Almost every method receives a subclass of {@link AbstractMessage}, except
+ * onDisconnect that receives the client id string and
+ * onSubscribe and onUnsubscribe that receive a
+ * {@link Subscription} object.
+ *
+ * @author Wagner Macedo
+ */
+public interface InterceptHandler {
+
+ void onConnect(ConnectMessage msg);
+
+ void onDisconnect(String clientID);
+
+ void onPublish(PublishMessage msg);
+
+ void onSubscribe(Subscription sub);
+
+ void onUnsubscribe(Subscription sub);
+}
diff --git a/broker/src/main/java/org/eclipse/moquette/interception/Interceptor.java b/broker/src/main/java/org/eclipse/moquette/interception/Interceptor.java
new file mode 100644
index 00000000..62dfe1f8
--- /dev/null
+++ b/broker/src/main/java/org/eclipse/moquette/interception/Interceptor.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2012-2014 The original author or authors
+ * ------------------------------------------------------
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Apache License v2.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * The Apache License v2.0 is available at
+ * http://www.opensource.org/licenses/apache2.0.php
+ *
+ * You may elect to redistribute this code under either of these licenses.
+ */
+package org.eclipse.moquette.interception;
+
+import org.eclipse.moquette.proto.messages.ConnectMessage;
+import org.eclipse.moquette.proto.messages.PublishMessage;
+import org.eclipse.moquette.spi.impl.subscriptions.Subscription;
+
+/**
+ * This interface is to be used internally by the broker components.
+ *
+ * An interface is used instead of a class to allow more flexibility in changing + * an implementation. For instance, this interface provide a lightweight + * no-handler interceptor. + *
+ * Interceptor implementations forward notifications to a InterceptHandler,
+ * that is normally a field. So, the implementations should act as a proxy to a custom
+ * intercept handler.
+ *
+ * @see InterceptHandler
+ * @author Wagner Macedo
+ */
+public interface Interceptor {
+ /**
+ * When no custom handler is provided, this interceptor should be used.
+ */
+ Interceptor NO_HANDLER_INTERCEPTOR = new NoHandlerInterceptor();
+
+ void notifyClientConnected(ConnectMessage msg);
+
+ void notifyClientDisconnected(String clientID);
+
+ void notifyTopicPublished(PublishMessage msg);
+
+ void notifyTopicSubscribed(Subscription sub);
+
+ void notifyTopicUnsubscribed(Subscription sub);
+
+}
diff --git a/broker/src/main/java/org/eclipse/moquette/interception/NoHandlerInterceptor.java b/broker/src/main/java/org/eclipse/moquette/interception/NoHandlerInterceptor.java
new file mode 100644
index 00000000..b1dcf293
--- /dev/null
+++ b/broker/src/main/java/org/eclipse/moquette/interception/NoHandlerInterceptor.java
@@ -0,0 +1,33 @@
+package org.eclipse.moquette.interception;
+
+import org.eclipse.moquette.proto.messages.ConnectMessage;
+import org.eclipse.moquette.proto.messages.PublishMessage;
+import org.eclipse.moquette.spi.impl.subscriptions.Subscription;
+
+/**
+ * @author Wagner Macedo
+ */
+public class NoHandlerInterceptor implements Interceptor {
+ NoHandlerInterceptor() {
+ }
+
+ @Override
+ public void notifyClientConnected(ConnectMessage msg) {
+ }
+
+ @Override
+ public void notifyClientDisconnected(String clientID) {
+ }
+
+ @Override
+ public void notifyTopicPublished(PublishMessage msg) {
+ }
+
+ @Override
+ public void notifyTopicSubscribed(Subscription sub) {
+ }
+
+ @Override
+ public void notifyTopicUnsubscribed(Subscription sub) {
+ }
+}
diff --git a/broker/src/main/java/org/eclipse/moquette/server/Server.java b/broker/src/main/java/org/eclipse/moquette/server/Server.java
index 5f5a2573..b07ecc9f 100644
--- a/broker/src/main/java/org/eclipse/moquette/server/Server.java
+++ b/broker/src/main/java/org/eclipse/moquette/server/Server.java
@@ -15,6 +15,7 @@
*/
package org.eclipse.moquette.server;
+import org.eclipse.moquette.interception.InterceptHandler;
import org.eclipse.moquette.server.netty.NettyAcceptor;
import org.eclipse.moquette.spi.impl.SimpleMessaging;
import org.slf4j.Logger;
@@ -86,8 +87,12 @@ public class Server {
*
*/
public void startServer(Properties configProps) throws IOException {
- ConfigurationParser confParser = new ConfigurationParser(configProps);
+ ConfigurationParser confParser = new ConfigurationParser(configProps);
m_properties = confParser.getProperties();
+ final String handlerProp = System.getProperty("intercept.handler");
+ if (handlerProp != null) {
+ m_properties.setProperty("intercept.handler", handlerProp);
+ }
LOG.info("Persistent store file: " + m_properties.get(PERSISTENT_STORE_PROPERTY_NAME));
messaging = SimpleMessaging.getInstance();
messaging.init(m_properties);
diff --git a/broker/src/main/java/org/eclipse/moquette/spi/impl/BrokerInterceptor.java b/broker/src/main/java/org/eclipse/moquette/spi/impl/BrokerInterceptor.java
new file mode 100644
index 00000000..b39ac197
--- /dev/null
+++ b/broker/src/main/java/org/eclipse/moquette/spi/impl/BrokerInterceptor.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012-2014 The original author or authors
+ * ------------------------------------------------------
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Apache License v2.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * The Apache License v2.0 is available at
+ * http://www.opensource.org/licenses/apache2.0.php
+ *
+ * You may elect to redistribute this code under either of these licenses.
+ */
+package org.eclipse.moquette.spi.impl;
+
+import org.eclipse.moquette.interception.InterceptHandler;
+import org.eclipse.moquette.interception.Interceptor;
+import org.eclipse.moquette.proto.messages.ConnectMessage;
+import org.eclipse.moquette.proto.messages.PublishMessage;
+import org.eclipse.moquette.spi.impl.subscriptions.Subscription;
+
+/**
+ * An interceptor that execute the interception tasks asynchronously.
+ *
+ * @author Wagner Macedo
+ */
+final class BrokerInterceptor implements Interceptor {
+ private final InterceptHandler handler;
+
+ BrokerInterceptor(InterceptHandler handler) {
+ this.handler = handler;
+ }
+
+ @Override
+ public void notifyClientConnected(final ConnectMessage msg) {
+ handler.onConnect(msg);
+ }
+
+ @Override
+ public void notifyClientDisconnected(final String clientID) {
+ handler.onDisconnect(clientID);
+ }
+
+ @Override
+ public void notifyTopicPublished(final PublishMessage msg) {
+ handler.onPublish(msg);
+ }
+
+ @Override
+ public void notifyTopicSubscribed(final Subscription sub) {
+ handler.onSubscribe(sub);
+ }
+
+ @Override
+ public void notifyTopicUnsubscribed(final Subscription sub) {
+ handler.onUnsubscribe(sub);
+ }
+}
diff --git a/broker/src/main/java/org/eclipse/moquette/spi/impl/ProtocolProcessor.java b/broker/src/main/java/org/eclipse/moquette/spi/impl/ProtocolProcessor.java
index 6cc139ae..5e363c8e 100644
--- a/broker/src/main/java/org/eclipse/moquette/spi/impl/ProtocolProcessor.java
+++ b/broker/src/main/java/org/eclipse/moquette/spi/impl/ProtocolProcessor.java
@@ -25,6 +25,7 @@ import java.util.concurrent.Executors;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.binary.StringUtils;
+import org.eclipse.moquette.interception.Interceptor;
import org.eclipse.moquette.server.netty.NettyChannel;
import org.eclipse.moquette.spi.IMatchingCondition;
import org.eclipse.moquette.spi.IMessagesStore;
@@ -105,6 +106,8 @@ class ProtocolProcessor implements EventHandler