aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/configuration
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server-jdbc/src/main/java/com/juick/configuration')
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java79
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java43
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java30
3 files changed, 152 insertions, 0 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java
new file mode 100644
index 00000000..b3c8c6bd
--- /dev/null
+++ b/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java
@@ -0,0 +1,79 @@
+package com.juick.configuration;
+
+import com.juick.service.search.SearchService;
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.context.annotation.*;
+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 java.util.Collections;
+import java.util.List;
+
+/**
+ * Created by aalexeev on 11/11/16.
+ */
+@Configuration
+@EnableTransactionManagement
+@PropertySource(value = {"classpath:juick.conf"})
+@ComponentScan(basePackages = {"com.juick.service"})
+public class DataConfiguration implements TransactionManagementConfigurer {
+ @Resource
+ private Environment env;
+
+ // NOTE: The close() method will be called automatically with default @Bean settings
+ // But Datasource interface has no close() method
+ @Bean
+ public BasicDataSource dataSource() {
+ BasicDataSource dataSource = new BasicDataSource();
+
+ dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver"));
+ dataSource.setUrl(env.getProperty("datasource_url"));
+ dataSource.setUsername(env.getProperty("datasource_user", StringUtils.EMPTY));
+ dataSource.setPassword(env.getProperty("datasource_password", StringUtils.EMPTY));
+
+ dataSource.setValidationQuery("select 1");
+
+ return dataSource;
+ }
+
+ @Bean
+ public PlatformTransactionManager transactionManager() {
+ return new DataSourceTransactionManager(dataSource());
+ }
+
+ @Override
+ public PlatformTransactionManager annotationDrivenTransactionManager() {
+ return transactionManager();
+ }
+
+ @Bean
+ @DependsOn("dataSource")
+ public JdbcTemplate jdbcTemplate() {
+ return new JdbcTemplate(dataSource());
+ }
+
+ @Bean
+ public SearchService emptySearchService() {
+ return new SearchService() {
+ @Override
+ public void setMaxResult(int maxResult) {
+ }
+
+ @Override
+ public List<Integer> searchInAllMessages(String searchString, int messageIdBefore) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Integer> searchByStringAndUser(String searchString, int userId, int messageIdBefore) {
+ return Collections.emptyList();
+ }
+ };
+ }
+}
diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java
new file mode 100644
index 00000000..e6b446b6
--- /dev/null
+++ b/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java
@@ -0,0 +1,43 @@
+package com.juick.configuration;
+
+import com.juick.service.search.SearchService;
+import com.juick.service.search.SphinxSearchServiceImpl;
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.env.Environment;
+
+import javax.annotation.Resource;
+
+/**
+ * Created by aalexeev on 11/18/16.
+ */
+@Configuration
+@PropertySource(value = {"classpath:juick.conf"})
+public class SearchConfiguration {
+ @Resource
+ private Environment env;
+
+ // NOTE: The close() method will be called automatically with default @Bean settings
+ // But Datasource interface has no close() method
+ @Bean
+ public BasicDataSource searchDataSource() {
+ BasicDataSource dataSource = new BasicDataSource();
+
+ dataSource.setDriverClassName(env.getProperty("sphinx_driver", "com.mysql.jdbc.Driver"));
+ dataSource.setUrl(env.getProperty("sphinx_url"));
+ dataSource.setUsername(env.getProperty("sphinx_user", StringUtils.EMPTY));
+ dataSource.setPassword(env.getProperty("sphinx_password", StringUtils.EMPTY));
+
+ return dataSource;
+ }
+
+ @Bean
+ @DependsOn("searchDataSource")
+ public SearchService searchService() {
+ return new SphinxSearchServiceImpl(searchDataSource());
+ }
+}
diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java
new file mode 100644
index 00000000..b3c0424c
--- /dev/null
+++ b/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java
@@ -0,0 +1,30 @@
+package com.juick.configuration;
+
+import com.juick.database.MySqlUpdater;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.transaction.PlatformTransactionManager;
+
+import javax.inject.Inject;
+
+/**
+ * Created by aalexeev on 12/13/16.
+ */
+@Configuration
+public class UpdaterConfiguration {
+ @Inject
+ JdbcTemplate jdbcTemplate;
+ @Inject
+ PlatformTransactionManager transactionManager;
+
+ @Bean
+ @DependsOn({"jdbcTemplate", "transactionManager"})
+ public MySqlUpdater updater() {
+ return new MySqlUpdater(
+ jdbcTemplate,
+ transactionManager,
+ "update.sql");
+ }
+}