From 194a9a763c6b11d207e682b2f93de94475c473b4 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 12 Nov 2016 01:34:07 +0700 Subject: extracted application configuration from Mvc configuration with application specific beans; extracted bean initialization from constructor; force using properties; --- .../main/java/com/juick/components/XMPPServer.java | 60 ++++++----- .../configuration/XMPPConfiguration.java | 112 --------------------- .../components/configuration/XMPPInitializer.java | 36 ------- .../configuration/XmppAppConfiguration.java | 38 +++++++ .../components/configuration/XmppInitializer.java | 41 ++++++++ .../configuration/XmppMvcConfiguration.java | 77 ++++++++++++++ 6 files changed, 191 insertions(+), 173 deletions(-) delete mode 100644 juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java delete mode 100644 juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java create mode 100644 juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java create mode 100644 juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java create mode 100644 juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java (limited to 'juick-xmpp/src/main/java/com') 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 463d8c2a..e26e0a1e 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -1,7 +1,10 @@ package com.juick.components; import com.juick.components.s2s.*; -import com.juick.xmpp.*; +import com.juick.util.ThreadHelper; +import com.juick.xmpp.JID; +import com.juick.xmpp.Stanza; +import com.juick.xmpp.StanzaChild; import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; @@ -23,11 +26,9 @@ import java.util.*; import java.util.concurrent.ExecutorService; /** - * * @author ugnich */ public class XMPPServer implements DisposableBean { - private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class); public ExecutorService service; @@ -39,15 +40,19 @@ public class XMPPServer implements DisposableBean { public String keystorePassword; public List brokenSSLhosts; public List bannedHosts; + private final List inConnections = Collections.synchronizedList(new ArrayList<>()); private final List outConnections = Collections.synchronizedList(new ArrayList<>()); private final List outCache = Collections.synchronizedList(new ArrayList<>()); + public JdbcTemplate jdbc; final public HashMap childParsers = new HashMap<>(); + public XMPPServer(Environment env, ExecutorService service, JdbcTemplate jdbc) { this.service = service; this.jdbc = jdbc; + logger.info("component initialized"); try { HOSTNAME = env.getProperty("hostname"); @@ -64,7 +69,9 @@ public class XMPPServer implements DisposableBean { childParsers.put(JuickMessage.XMLNS, new JuickMessage()); router = new ConnectionRouter(this, componentName, componentPort, env.getProperty("xmpp_password")); + service.submit(router); + service.submit(() -> { final ServerSocket listener = new ServerSocket(s2sPort); logger.info("s2s listener ready"); @@ -79,6 +86,7 @@ public class XMPPServer implements DisposableBean { } } }); + service.submit(new CleaningUp(this)); } catch (Exception e) { @@ -86,6 +94,29 @@ public class XMPPServer implements DisposableBean { } } + @Override + public void destroy() throws Exception { + synchronized (getOutConnections()) { + for (Iterator i = getOutConnections().iterator(); i.hasNext(); ) { + ConnectionOut c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + synchronized (getInConnections()) { + for (Iterator i = getInConnections().iterator(); i.hasNext(); ) { + ConnectionIn c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + ThreadHelper.shutdownAndAwaitTermination(service); + + logger.info("Xmpp server destroyed"); + } + public void addConnectionIn(ConnectionIn c) { synchronized (getInConnections()) { getInConnections().add(c); @@ -113,7 +144,7 @@ public class XMPPServer implements DisposableBean { public String getFromCache(String hostname) { CacheEntry ret = null; synchronized (getOutCache()) { - for (Iterator i = getOutCache().iterator(); i.hasNext();) { + for (Iterator i = getOutCache().iterator(); i.hasNext(); ) { CacheEntry c = i.next(); if (c.hostname != null && c.hostname.equals(hostname)) { ret = c; @@ -202,27 +233,6 @@ public class XMPPServer implements DisposableBean { } } - @Override - public void destroy() { - synchronized (getOutConnections()) { - for (Iterator i = getOutConnections().iterator(); i.hasNext();) { - ConnectionOut c = i.next(); - c.closeConnection(); - i.remove(); - } - } - - synchronized (getInConnections()) { - for (Iterator i = getInConnections().iterator(); i.hasNext();) { - ConnectionIn c = i.next(); - c.closeConnection(); - i.remove(); - } - } - service.shutdown(); - logger.info("component destroyed"); - } - public ConnectionRouter getRouter() { return router; } diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java deleted file mode 100644 index 13ac2a9d..00000000 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.juick.components.configuration; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import com.juick.components.XMPPServer; -import com.mitchellbosecke.pebble.PebbleEngine; -import com.mitchellbosecke.pebble.loader.Loader; -import com.mitchellbosecke.pebble.loader.ServletLoader; -import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; -import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; -import org.springframework.beans.factory.config.PlaceholderConfigurerSupport; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.core.env.Environment; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; - -import javax.inject.Inject; -import javax.servlet.ServletContext; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * Created by vitalyster on 28.06.2016. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick"}) -@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) -public class XMPPConfiguration extends WebMvcConfigurationSupport { - @Inject - private Environment env; - @Inject - private JdbcTemplate jdbc; - - @Inject - private ServletContext servletContext; - - @Bean - public static PlaceholderConfigurerSupport propertySourcesPlaceholderConfigurer() { - PlaceholderConfigurerSupport configurer = new PropertySourcesPlaceholderConfigurer(); - - configurer.setFileEncoding("utf-8"); - configurer.setOrder(1); - return configurer; - } - - @Bean - public Loader templateLoader() { - return new ServletLoader(servletContext); - } - - @Bean - public SpringExtension springExtension() { - return new SpringExtension(); - } - - @Bean - public PebbleEngine pebbleEngine() { - return new PebbleEngine.Builder() - .loader(this.templateLoader()) - .extension(springExtension()) - .build(); - } - - @Bean - public ViewResolver viewResolver() { - PebbleViewResolver viewResolver = new PebbleViewResolver(); - viewResolver.setPrefix("/WEB-INF/templates/"); - viewResolver.setSuffix(".html"); - viewResolver.setPebbleEngine(pebbleEngine()); - return viewResolver; - } - - @Bean - public XMPPServer xmpp() { - return new XMPPServer(env, service(), jdbc); - } - - @Bean - public ExecutorService service() { - return Executors.newCachedThreadPool(); - } - - @Override - protected void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.setOrder(0); - registry.addResourceHandler("/scripts.js").addResourceLocations("/"); - registry.addResourceHandler("/style.css").addResourceLocations("/"); - } - - @Override - protected void configureMessageConverters(List> converters) { - Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() - .serializationInclusion(JsonInclude.Include.NON_DEFAULT) - .serializationInclusion(JsonInclude.Include.NON_NULL) - .serializationInclusion(JsonInclude.Include.NON_ABSENT) - .serializationInclusion(JsonInclude.Include.NON_EMPTY); - MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build()); - converter.getObjectMapper().registerModule(new Jdk8Module()); - converters.add(converter); - super.configureMessageConverters(converters); - } -} diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java deleted file mode 100644 index 3942f889..00000000 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.juick.components.configuration; - -import com.juick.configuration.DataConfiguration; -import org.springframework.web.filter.CharacterEncodingFilter; -import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; - -import javax.servlet.Filter; - -/** - * Created by vt on 09/02/16. - */ -public class XMPPInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { - @Override - protected Class[] getRootConfigClasses() { - return new Class[]{DataConfiguration.class}; - } - - @Override - protected Class[] getServletConfigClasses() { - return new Class[]{XMPPConfiguration.class}; - } - - @Override - protected String[] getServletMappings() { - return new String[]{ - "/" - }; - } - - @Override - protected Filter[] getServletFilters() { - CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); - characterEncodingFilter.setEncoding("UTF-8"); - return new Filter[]{characterEncodingFilter}; - } -} 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 new file mode 100644 index 00000000..eb3dd726 --- /dev/null +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java @@ -0,0 +1,38 @@ +package com.juick.components.configuration; + +/** + * Created by aalexeev on 11/12/16. + */ + +import com.juick.components.XMPPServer; +import com.juick.configuration.DataConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.inject.Inject; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +@Configuration +@PropertySource("classpath:juick.conf") +@Import(DataConfiguration.class) +public class XmppAppConfiguration { + @Inject + private Environment env; + @Inject + private JdbcTemplate jdbc; + + @Bean + public XMPPServer xmpp() { + return new XMPPServer(env, service(), jdbc); + } + + @Bean + public ExecutorService service() { + return Executors.newCachedThreadPool(); + } +} diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java new file mode 100644 index 00000000..89dd69e1 --- /dev/null +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java @@ -0,0 +1,41 @@ +package com.juick.components.configuration; + +import com.juick.configuration.DataConfiguration; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +import javax.servlet.Filter; + +/** + * Created by vt on 09/02/16. + */ +public class XmppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return new Class[]{XmppAppConfiguration.class, DataConfiguration.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{XmppMvcConfiguration.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } + + @Override + protected Filter[] getServletFilters() { + CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); + characterEncodingFilter.setEncoding("UTF-8"); + + return new Filter[]{characterEncodingFilter}; + } + + @Override + protected String getServletName() { + return "Xmpp dispatcher servlet"; + } +} diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java new file mode 100644 index 00000000..4a0e8ced --- /dev/null +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java @@ -0,0 +1,77 @@ +package com.juick.components.configuration; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.loader.Loader; +import com.mitchellbosecke.pebble.loader.ServletLoader; +import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; +import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +import java.util.List; + +/** + * Created by vitalyster on 28.06.2016. + */ +@Configuration +@ComponentScan(basePackages = {"com.juick.components.controllers"}) +@PropertySource("classpath:juick.conf") +public class XmppMvcConfiguration extends WebMvcConfigurationSupport { + + @Bean + public Loader templateLoader() { + return new ServletLoader(getServletContext()); + } + + @Bean + public SpringExtension springExtension() { + return new SpringExtension(); + } + + @Bean + public PebbleEngine pebbleEngine() { + return new PebbleEngine.Builder() + .loader(this.templateLoader()) + .extension(springExtension()) + .build(); + } + + @Bean + public ViewResolver viewResolver() { + PebbleViewResolver viewResolver = new PebbleViewResolver(); + viewResolver.setPrefix("/WEB-INF/templates/"); + viewResolver.setSuffix(".html"); + viewResolver.setPebbleEngine(pebbleEngine()); + return viewResolver; + } + + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.setOrder(0); + registry.addResourceHandler("/scripts.js").addResourceLocations("/"); + registry.addResourceHandler("/style.css").addResourceLocations("/"); + } + + @Override + protected void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() + .serializationInclusion(JsonInclude.Include.NON_DEFAULT) + .serializationInclusion(JsonInclude.Include.NON_NULL) + .serializationInclusion(JsonInclude.Include.NON_ABSENT) + .serializationInclusion(JsonInclude.Include.NON_EMPTY); + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build()); + converter.getObjectMapper().registerModule(new Jdk8Module()); + converters.add(converter); + super.configureMessageConverters(converters); + } +} -- cgit v1.2.3