moved db initializer config properties to seperate class

This commit is contained in:
Rohan Sircar 2019-12-13 13:19:57 +05:30 committed by nova
parent 1ddb2bcd09
commit 5452a2d418
4 changed files with 39 additions and 35 deletions

View File

@ -4,28 +4,16 @@ import java.security.SecureRandom;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.ros.chatto.security.AuthenticationSuccessHandlerImpl; import org.ros.chatto.security.AuthenticationSuccessHandlerImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.core.token.KeyBasedPersistenceTokenService; import org.springframework.security.core.token.KeyBasedPersistenceTokenService;
import org.springframework.security.core.token.TokenService; import org.springframework.security.core.token.TokenService;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
@PropertySource(value = "classpath:queries.properties")
@Configuration @Configuration
public class BeanConfigurations { public class BeanConfigurations {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String password;
@Bean @Bean
public AuthenticationSuccessHandler authenticationSuccessHandler() { public AuthenticationSuccessHandler authenticationSuccessHandler() {
return new AuthenticationSuccessHandlerImpl(); return new AuthenticationSuccessHandlerImpl();

View File

@ -0,0 +1,29 @@
package org.ros.chatto.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import lombok.Getter;
@Component
@EnableConfigurationProperties
@PropertySource(value = "classpath:queries.properties")
@Getter
public class DBInitializerConfig {
private final String dbName;
private final String numTablesQuery;
private final String resetSessionsQuery;
public DBInitializerConfig(@Value("${database-name}") String dbName, @Value("${num-tables}") String numTablesQuery,
@Value("${reset-sessions}") String resetSessionsQuery) {
this.dbName = dbName;
this.numTablesQuery = numTablesQuery;
this.resetSessionsQuery = resetSessionsQuery;
}
}

View File

@ -14,7 +14,6 @@ import org.ros.chatto.repository.ChatMessageRepository;
import org.ros.chatto.repository.MessageCipherRepository; import org.ros.chatto.repository.MessageCipherRepository;
import org.ros.chatto.repository.UserRepository; import org.ros.chatto.repository.UserRepository;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;

View File

@ -12,43 +12,31 @@ import javax.persistence.PersistenceContext;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import org.hibernate.Session; import org.hibernate.Session;
import org.slf4j.Logger; import org.ros.chatto.config.DBInitializerConfig;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Service @Service
@Slf4j @Slf4j
@PropertySource(value = "classpath:queries.properties") @RequiredArgsConstructor
public class DBInitializerService { public class DBInitializerService {
@Value("${database-name}") private final DBInitializerConfig dbInitializerConfig;
private String dbName;
@Value("${num-tables}")
private String numTablesQuery;
@Value("${reset-sessions}")
private String resetSessionsQuery;
private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class);
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
public int getNumTables(final Connection connection) throws SQLException { public int getNumTables(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery); final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getNumTablesQuery());
preparedStatement.setString(1, dbName); preparedStatement.setString(1, dbInitializerConfig.getDbName());
final ResultSet resultSet = preparedStatement.executeQuery(); final ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next(); resultSet.next();
final int numTables = resultSet.getInt("num_tables"); final int numTables = resultSet.getInt("num_tables");
@ -58,13 +46,13 @@ public class DBInitializerService {
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
@Transactional @Transactional
public void doSomethingAfterStartup() throws SQLException, IOException { public void doSomethingAfterStartup() throws SQLException, IOException {
logger.info("Application Started - running initializer service"); log.info("Application Started - running initializer service");
final Session session = entityManager.unwrap(Session.class); final Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> { session.doWork(connection -> {
if (getNumTables(connection) == 0) { if (getNumTables(connection) == 0) {
logger.info("Database is empty. Populating tables and roles"); log.info("Database is empty. Populating tables and roles");
try { try {
populateDB(connection); populateDB(connection);
} catch (final IOException e) { } catch (final IOException e) {
@ -74,7 +62,7 @@ public class DBInitializerService {
}); });
session.doWork(connection -> { session.doWork(connection -> {
logger.info("Resetting all user sessions"); log.info("Resetting all user sessions");
resetAllUserSessions(connection); resetAllUserSessions(connection);
}); });
} }
@ -88,7 +76,7 @@ public class DBInitializerService {
} }
private void resetAllUserSessions(final Connection connection) throws SQLException { private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery); final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getResetSessionsQuery());
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} }