/* * Copyright (C) 2008-2017, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.juick.configuration; import com.juick.service.search.SearchService; 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 javax.sql.DataSource; 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 DataSource dataSource() { org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); 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 searchInAllMessages(String searchString, int messageIdBefore) { return Collections.emptyList(); } @Override public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { return Collections.emptyList(); } }; } }