From ac9df0dd46640179869cc8285b9aadff0e88e242 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 6 Feb 2017 15:35:36 +0300 Subject: juick-xmpp: spring-managed ConnectionRouter --- .../main/java/com/juick/components/XMPPServer.java | 15 ++++-------- .../configuration/XmppAppConfiguration.java | 10 ++++++++ .../com/juick/components/s2s/ConnectionRouter.java | 27 +++++++--------------- 3 files changed, 22 insertions(+), 30 deletions(-) (limited to 'juick-xmpp/src/main/java/com/juick/components') diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java index 72dba642..ffb7e260 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -2,7 +2,6 @@ package com.juick.components; import com.juick.components.s2s.*; import com.juick.service.*; -import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; @@ -38,9 +37,8 @@ public class XMPPServer implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class); public ExecutorService service; - private ConnectionRouter router; - public String HOSTNAME, componentName; + public String HOSTNAME; public String keystore; public String keystorePassword; public List brokenSSLhosts; @@ -51,6 +49,9 @@ public class XMPPServer implements AutoCloseable { private final List outCache = Collections.synchronizedList(new ArrayList<>()); private final List stanzaListeners = Collections.synchronizedList(new ArrayList<>()); + + @Inject + private ConnectionRouter router; @Inject public MessagesService messagesService; @Inject @@ -78,20 +79,12 @@ public class XMPPServer implements AutoCloseable { try { HOSTNAME = env.getProperty("hostname"); session = BasicXmppSession.create(HOSTNAME, configuration); - componentName = env.getProperty("componentname"); - int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); int s2sPort = NumberUtils.toInt(env.getProperty("s2s_port"), 5269); keystore = env.getProperty("keystore"); keystorePassword = env.getProperty("keystore_password"); brokenSSLhosts = Arrays.asList(env.getProperty("broken_ssl_hosts", StringUtils.EMPTY).split(",")); bannedHosts = Arrays.asList(env.getProperty("banned_hosts", StringUtils.EMPTY).split(",")); jid = Jid.of(env.getProperty("xmppbot_jid")); - boolean disabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled", "false")); - - if (!disabled) { - router = new ConnectionRouter(this, componentName, componentPort, env.getProperty("xmpp_password")); - service.submit(router); - } service.submit(() -> { try { diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java index 50bce7b9..9d6fc041 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java @@ -6,8 +6,11 @@ package com.juick.components.configuration; import com.juick.components.XMPPServer; import com.juick.components.s2s.CleaningUp; +import com.juick.components.s2s.ConnectionRouter; import com.juick.components.s2s.JuickBot; import com.juick.configuration.DataConfiguration; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -44,4 +47,11 @@ public class XmppAppConfiguration { public JuickBot bot() { return new JuickBot(xmpp()); } + @Bean + public ConnectionRouter router() { + boolean disabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled", "false")); + return disabled ? null : new ConnectionRouter(xmpp(), env.getProperty("componentname"), + NumberUtils.toInt(env.getProperty("component_port"), 5347), + env.getProperty("xmpp_password")); + } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java index 16c2019a..61b216db 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java @@ -6,8 +6,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.session.Extension; -import rocks.xmpp.core.session.XmppSessionConfiguration; import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.core.stanza.model.Stanza; import rocks.xmpp.extensions.component.accept.ExternalComponent; @@ -27,7 +25,7 @@ import java.util.List; /** * @author ugnich */ -public class ConnectionRouter implements Runnable, AutoCloseable { +public class ConnectionRouter implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(ConnectionRouter.class); @@ -37,20 +35,13 @@ public class ConnectionRouter implements Runnable, AutoCloseable { private ExternalComponent router; private XMPPServer xmpp; - public ConnectionRouter(XMPPServer s2s, String componentName, int componentPort, String password) throws Exception { + public ConnectionRouter(XMPPServer s2s, String componentName, int componentPort, String password) { this.xmpp = s2s; this.componentName = componentName; this.componentPort = componentPort; this.password = password; - } - - @Override - public void run() { logger.info("stream router start"); - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .extensions(Extension.of(com.juick.Message.class)) - .build(); - router = ExternalComponent.create(componentName, password, configuration, "localhost", componentPort); + router = ExternalComponent.create(componentName, password, xmpp.getSession().getConfiguration(), "localhost", componentPort); router.addInboundMessageListener(e -> { Message message = e.getMessage(); Jid jid = message.getTo(); @@ -74,13 +65,11 @@ public class ConnectionRouter implements Runnable, AutoCloseable { route(jid.getDomain(), message); } }); - xmpp.service.submit(() -> { - try { - router.connect(); - } catch (XmppException e) { - logger.warn("xmpp exception", e); - } - }); + try { + router.connect(); + } catch (XmppException e) { + logger.warn("xmpp exception", e); + } } void route(String domain, Stanza stanza) { -- cgit v1.2.3