From 007951db3ae9f9ba89a726960b0ef1639d63f3df Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 21 Aug 2017 12:52:40 +0300 Subject: ws: cleanup components initialization --- .../main/java/com/juick/ws/WebsocketComponent.java | 8 ++-- .../src/main/java/com/juick/ws/XMPPConnection.java | 45 +++++++++------------- .../ws/configuration/WebsocketConfiguration.java | 10 ----- 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java index f56ba936..24304494 100644 --- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java @@ -57,13 +57,13 @@ public class WebsocketComponent extends TextWebSocketHandler implements Protocol private final List clients = Collections.synchronizedList(new ArrayList()); @Inject - UserService userService; + private UserService userService; @Inject - MessagesService messagesService; + private MessagesService messagesService; @Inject - SubscriptionService subscriptionService; + private SubscriptionService subscriptionService; @Inject - JuickProtocol protocol; + private JuickProtocol protocol; @PostConstruct public void init() { diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index 906fa125..84158445 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -17,7 +17,6 @@ package com.juick.ws; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.User; @@ -26,7 +25,7 @@ import com.juick.service.SubscriptionService; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.env.Environment; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.Assert; import org.springframework.web.socket.TextMessage; import rocks.xmpp.core.XmppException; @@ -38,6 +37,7 @@ import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.component.accept.ExternalComponent; import rocks.xmpp.util.XmppUtils; +import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.xml.bind.JAXBException; import javax.xml.stream.XMLStreamException; @@ -53,36 +53,29 @@ import java.util.stream.Collectors; public class XMPPConnection implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); - private final WebsocketComponent wsHandler; - private final String xmppPassword; - private final ObjectMapper ms; - private final int xmppPort; - private final String wsJid; + @Inject + private WebsocketComponent wsHandler; + @Value("${xmpp_password:secret}") + private String xmppPassword; + @Inject + private ObjectMapper jsonMapper; + @Value("${xmpp_port:5347}") + private int xmppPort; + @Value("${ws_jid:ws.juick.local}") + private String wsJid; private XmppSession xmpp; @Inject - MessagesService messagesService; + private MessagesService messagesService; @Inject - SubscriptionService subscriptionService; + private SubscriptionService subscriptionService; - public XMPPConnection( - final Environment env, final WebsocketComponent wsHandler) { - Assert.notNull(env, "Environment must be initialized"); + @PostConstruct + public void init() { Assert.notNull(wsHandler, "WebsocketComponent must be initialized"); - this.wsHandler = wsHandler; - - xmppPassword = env.getProperty("xmpp_password"); - xmppPort = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347); - wsJid = env.getProperty("ws_jid", "ws.juick.local"); - - ms = new ObjectMapper(); - ms.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - ms.setSerializationInclusion(JsonInclude.Include.NON_NULL); - ms.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() .extensions(Extension.of(com.juick.Message.class)) .debugger(LogbackDebugger.class) @@ -143,7 +136,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickPM(final int uid_to, final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); synchronized (wsHandler.getClients()) { wsHandler.getClients().stream().filter(c -> !c.legacy && c.visitor.getUid() == uid_to).forEach(c -> { try { @@ -157,7 +150,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickMessagePost(final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); List uids = subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()) .stream().map(User::getUid).collect(Collectors.toList()); synchronized (wsHandler.getClients()) { @@ -186,7 +179,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickMessageReply(final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); List threadUsers = subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()) .stream().map(User::getUid).collect(Collectors.toList()); diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java index 21c013e9..a2c2d827 100644 --- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java +++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java @@ -20,9 +20,7 @@ package com.juick.ws.configuration; import com.juick.server.configuration.BaseWebConfiguration; import com.juick.server.protocol.JuickProtocol; import com.juick.ws.WebsocketComponent; -import com.juick.ws.XMPPConnection; import org.springframework.context.annotation.*; -import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.socket.config.annotation.EnableWebSocket; @@ -31,8 +29,6 @@ import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean; -import javax.inject.Inject; - /** * Created by aalexeev on 11/24/16. */ @@ -43,18 +39,12 @@ import javax.inject.Inject; @PropertySource("classpath:juick.conf") @Import(BaseWebConfiguration.class) class WebsocketConfiguration extends WebMvcConfigurationSupport implements WebSocketConfigurer { - @Inject - private Environment env; @Bean public WebsocketComponent wsHandler() { return new WebsocketComponent(); } - @Bean - public XMPPConnection ws() { - return new XMPPConnection(env, wsHandler()); - } @Bean public JuickProtocol protocol() { return new JuickProtocol("https://juick.com/"); -- cgit v1.2.3