diff options
author | Alexander Alexeev | 2016-11-12 01:34:07 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-12 11:14:56 +0300 |
commit | 194a9a763c6b11d207e682b2f93de94475c473b4 (patch) | |
tree | 196317662017b716066eadfa413ecbdc7532c52d /juick-xmpp/src/main/java/com/juick | |
parent | 7d86347a351aa5263c19cd03aa0bda4650c8bfca (diff) |
extracted application configuration from Mvc configuration with application specific beans;
extracted bean initialization from constructor;
force using properties;
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick')
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/XMPPServer.java | 60 | ||||
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java | 38 | ||||
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java (renamed from juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java) | 17 | ||||
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java (renamed from juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java) | 43 |
4 files changed, 88 insertions, 70 deletions
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<String> brokenSSLhosts; public List<String> bannedHosts; + private final List<ConnectionIn> inConnections = Collections.synchronizedList(new ArrayList<>()); private final List<ConnectionOut> outConnections = Collections.synchronizedList(new ArrayList<>()); private final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>()); + public JdbcTemplate jdbc; final public HashMap<String, StanzaChild> 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<ConnectionOut> i = getOutConnections().iterator(); i.hasNext(); ) { + ConnectionOut c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + synchronized (getInConnections()) { + for (Iterator<ConnectionIn> 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<CacheEntry> i = getOutCache().iterator(); i.hasNext();) { + for (Iterator<CacheEntry> 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<ConnectionOut> i = getOutConnections().iterator(); i.hasNext();) { - ConnectionOut c = i.next(); - c.closeConnection(); - i.remove(); - } - } - - synchronized (getInConnections()) { - for (Iterator<ConnectionIn> 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/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 index 3942f889..89dd69e1 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPInitializer.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppInitializer.java @@ -9,28 +9,33 @@ import javax.servlet.Filter; /** * Created by vt on 09/02/16. */ -public class XMPPInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { +public class XmppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + @Override protected Class<?>[] getRootConfigClasses() { - return new Class[]{DataConfiguration.class}; + return new Class<?>[]{XmppAppConfiguration.class, DataConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { - return new Class[]{XMPPConfiguration.class}; + return new Class<?>[]{XmppMvcConfiguration.class}; } @Override protected String[] getServletMappings() { - return new String[]{ - "/" - }; + 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/XMPPConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java index 13ac2a9d..4a0e8ced 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppMvcConfiguration.java @@ -2,60 +2,35 @@ 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; - } +@ComponentScan(basePackages = {"com.juick.components.controllers"}) +@PropertySource("classpath:juick.conf") +public class XmppMvcConfiguration extends WebMvcConfigurationSupport { @Bean public Loader templateLoader() { - return new ServletLoader(servletContext); + return new ServletLoader(getServletContext()); } @Bean @@ -80,16 +55,6 @@ public class XMPPConfiguration extends WebMvcConfigurationSupport { 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); |