A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

  1. package org.ros.chatto.service;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.PersistenceContext;
  8. import javax.transaction.Transactional;
  9. import org.hibernate.Session;
  10. import org.ros.chatto.config.DBInitializerConfig;
  11. import org.springframework.boot.context.event.ApplicationReadyEvent;
  12. import org.springframework.context.event.EventListener;
  13. import org.springframework.stereotype.Service;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. @Service
  17. @Slf4j
  18. @RequiredArgsConstructor
  19. public class DBInitializerService {
  20. private final DBInitializerConfig dbInitializerConfig;
  21. @PersistenceContext
  22. private EntityManager entityManager;
  23. @EventListener(ApplicationReadyEvent.class)
  24. @Transactional
  25. public void doSomethingAfterStartup() throws SQLException, IOException {
  26. log.info("Application Started - running initializer service");
  27. final Session session = entityManager.unwrap(Session.class);
  28. session.doWork(connection -> {
  29. log.info("Clearing tokens");
  30. clearAllTokens(connection);
  31. });
  32. session.doWork(connection -> {
  33. log.info("Resetting all user sessions");
  34. resetAllUserSessions(connection);
  35. });
  36. }
  37. private void resetAllUserSessions(final Connection connection)
  38. throws SQLException {
  39. final PreparedStatement preparedStatement = connection
  40. .prepareStatement(dbInitializerConfig.getResetSessionsQuery());
  41. preparedStatement.executeUpdate();
  42. }
  43. private void clearAllTokens(final Connection connection)
  44. throws SQLException {
  45. final PreparedStatement preparedStatement = connection
  46. .prepareStatement(dbInitializerConfig.getClearTokensQuery());
  47. preparedStatement.executeUpdate();
  48. }
  49. }