From 6604dc96558028c0095f33eca1852de5e61f8466 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Fri, 11 Nov 2016 01:29:01 +0700 Subject: common data configuration is added --- .../com/juick/configuration/DataConfiguration.java | 51 ++++++++++++++++++++++ .../configuration/CrosspostConfiguration.java | 30 +++++-------- .../configuration/CrosspostInitializer.java | 14 +++--- .../configuration/NotificationsConfiguration.java | 13 +++--- .../configuration/NotificationsInitializer.java | 13 +++--- .../ws/configuration/WebsocketConfiguration.java | 31 ++++++------- .../ws/configuration/WebsocketInitializer.java | 13 +++--- .../configuration/XMPPConfiguration.java | 20 +++------ .../components/configuration/XMPPInitializer.java | 11 +++-- 9 files changed, 119 insertions(+), 77 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/configuration/DataConfiguration.java diff --git a/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java new file mode 100644 index 00000000..b140b873 --- /dev/null +++ b/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java @@ -0,0 +1,51 @@ +package com.juick.configuration; + +import org.apache.commons.dbcp2.BasicDataSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.annotation.TransactionManagementConfigurer; + +import javax.annotation.Resource; +import javax.sql.DataSource; + +/** + * Created by aalexeev on 11/11/16. + */ +@Configuration +@EnableTransactionManagement +@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) +public class DataConfiguration implements TransactionManagementConfigurer { + @Resource + private Environment env; + + @Bean + public DataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + + dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(env.getProperty("datasource_url")); + + return dataSource; + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new DataSourceTransactionManager(dataSource()); + } + + @Override + public PlatformTransactionManager annotationDrivenTransactionManager() { + return transactionManager(); + } + + @Bean + public JdbcTemplate jdbc() { + return new JdbcTemplate(dataSource()); + } +} diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java index 0b213cc9..069037ee 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java @@ -8,7 +8,6 @@ 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.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -34,28 +33,17 @@ import java.util.concurrent.Executors; */ @Configuration @ComponentScan(basePackages = {"com.juick"}) -@PropertySource(value = "classpath:juick.conf", ignoreResourceNotFound = true) +@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) public class CrosspostConfiguration extends WebMvcConfigurationSupport { @Inject - Environment env; - @Inject - ExecutorService service; - @Inject - JdbcTemplate jdbc; - - @Bean - JdbcTemplate jdbc() { - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("datasource_url")); - return new JdbcTemplate(dataSource); - } - + private Environment env; @Inject private ServletContext servletContext; + @Inject + private JdbcTemplate jdbc; @Bean - public Loader templateLoader(){ + public Loader templateLoader() { return new ServletLoader(servletContext); } @@ -78,12 +66,15 @@ public class CrosspostConfiguration extends WebMvcConfigurationSupport { viewResolver.setPrefix("/WEB-INF/templates/"); viewResolver.setSuffix(".html"); viewResolver.setPebbleEngine(pebbleEngine()); + return viewResolver; } + @Bean public Crosspost crosspost() { - return new Crosspost(env, service, jdbc); + return new Crosspost(env, service(), jdbc); } + @Bean public ExecutorService service() { return Executors.newCachedThreadPool(); @@ -95,6 +86,7 @@ public class CrosspostConfiguration extends WebMvcConfigurationSupport { mapping.setUseSuffixPatternMatch(false); return mapping; } + @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.setOrder(0); @@ -109,9 +101,11 @@ public class CrosspostConfiguration extends WebMvcConfigurationSupport { .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-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java index e85d7a2a..0768f834 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java @@ -1,4 +1,6 @@ package com.juick.components.configuration; + +import com.juick.configuration.DataConfiguration; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -10,24 +12,24 @@ import javax.servlet.Filter; public class CrosspostInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { - return new Class[] {CrosspostConfiguration.class}; + return new Class[]{DataConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { - return null; + return new Class[]{CrosspostConfiguration.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}; + + return new Filter[]{characterEncodingFilter}; } } diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsConfiguration.java index 0c7a857c..d42549ef 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsConfiguration.java @@ -33,22 +33,20 @@ import java.util.concurrent.Executors; */ @Configuration @ComponentScan(basePackages = {"com.juick"}) -@PropertySource(value = "classpath:juick.conf", ignoreResourceNotFound = true) +@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) public class NotificationsConfiguration extends WebMvcConfigurationSupport { @Inject Environment env; @Inject - ExecutorService service; + private ServletContext servletContext; @Bean RestTemplate rest() { return new RestTemplate(); } - @Inject - private ServletContext servletContext; @Bean - public Loader templateLoader(){ + public Loader templateLoader() { return new ServletLoader(servletContext); } @@ -73,10 +71,12 @@ public class NotificationsConfiguration extends WebMvcConfigurationSupport { viewResolver.setPebbleEngine(pebbleEngine()); return viewResolver; } + @Bean public Notifications push() { - return new Notifications(env, service); + return new Notifications(env, service()); } + @Bean public ExecutorService service() { return Executors.newCachedThreadPool(); @@ -88,6 +88,7 @@ public class NotificationsConfiguration extends WebMvcConfigurationSupport { mapping.setUseSuffixPatternMatch(false); return mapping; } + @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.setOrder(0); diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java index 1449a4f8..417cc7fa 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java @@ -1,4 +1,6 @@ package com.juick.components.configuration; + +import com.juick.configuration.DataConfiguration; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -10,24 +12,23 @@ import javax.servlet.Filter; public class NotificationsInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { - return new Class[] {NotificationsConfiguration.class}; + return new Class[]{DataConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { - return null; + return new Class[]{NotificationsConfiguration.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}; + return new Filter[]{characterEncodingFilter}; } } 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 5e035dac..db37edc4 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 @@ -9,7 +9,6 @@ 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.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -40,29 +39,22 @@ import java.util.concurrent.Executors; @Configuration @EnableWebSocket @ComponentScan(basePackages = {"com.juick"}) -@PropertySource(value = "classpath:juick.conf", ignoreResourceNotFound = true) +@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) public class WebsocketConfiguration extends WebMvcConfigurationSupport implements WebSocketConfigurer { @Inject - Environment env; + private Environment env; @Inject - ExecutorService service; + private JdbcTemplate jdbc; + @Inject + private ServletContext servletContext; + @Bean - WebsocketComponent wsHandler() { + public WebsocketComponent wsHandler() { return new WebsocketComponent(); } @Bean - JdbcTemplate jdbc() { - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("datasource_url")); - return new JdbcTemplate(dataSource); - } - @Inject - private ServletContext servletContext; - - @Bean - public Loader templateLoader(){ + public Loader templateLoader() { return new ServletLoader(servletContext); } @@ -87,10 +79,12 @@ public class WebsocketConfiguration extends WebMvcConfigurationSupport implement viewResolver.setPebbleEngine(pebbleEngine()); return viewResolver; } + @Bean public XMPPConnection ws() { - return new XMPPConnection(env, service); + return new XMPPConnection(env, service()); } + @Bean public ExecutorService service() { return Executors.newCachedThreadPool(); @@ -102,9 +96,10 @@ public class WebsocketConfiguration extends WebMvcConfigurationSupport implement mapping.setUseSuffixPatternMatch(false); return mapping; } + @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - ((ServletWebSocketHandlerRegistry)registry).setOrder(2); + ((ServletWebSocketHandlerRegistry) registry).setOrder(2); registry.addHandler(wsHandler(), "/**").setAllowedOrigins("*"); } diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java index 89017f8b..4829710f 100644 --- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java +++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java @@ -1,4 +1,6 @@ package com.juick.ws.configuration; + +import com.juick.configuration.DataConfiguration; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -10,24 +12,23 @@ import javax.servlet.Filter; public class WebsocketInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { - return new Class[] {WebsocketConfiguration.class}; + return new Class[]{DataConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { - return null; + return new Class[]{WebsocketConfiguration.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}; + return new Filter[]{characterEncodingFilter}; } } 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 index 9e7e6cad..c4bdd117 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XMPPConfiguration.java @@ -8,7 +8,6 @@ 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.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -33,25 +32,18 @@ import java.util.concurrent.Executors; */ @Configuration @ComponentScan(basePackages = {"com.juick"}) -@PropertySource(value = "classpath:juick.conf", ignoreResourceNotFound = true) +@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) public class XMPPConfiguration extends WebMvcConfigurationSupport { @Inject - Environment env; + private Environment env; @Inject - ExecutorService service; + private JdbcTemplate jdbc; - @Bean - JdbcTemplate jdbc() { - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("datasource_url")); - return new JdbcTemplate(dataSource); - } @Inject private ServletContext servletContext; @Bean - public Loader templateLoader(){ + public Loader templateLoader() { return new ServletLoader(servletContext); } @@ -76,10 +68,12 @@ public class XMPPConfiguration extends WebMvcConfigurationSupport { viewResolver.setPebbleEngine(pebbleEngine()); return viewResolver; } + @Bean public XMPPServer xmpp() { - return new XMPPServer(env, service, jdbc()); + 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 604516c4..3942f889 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 @@ -1,4 +1,6 @@ package com.juick.components.configuration; + +import com.juick.configuration.DataConfiguration; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -10,24 +12,25 @@ import javax.servlet.Filter; public class XMPPInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { - return new Class[] {XMPPConfiguration.class}; + return new Class[]{DataConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { - return null; + return new Class[]{XMPPConfiguration.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}; + return new Filter[]{characterEncodingFilter}; } } -- cgit v1.2.3