Updated files to work properly with applogger. Reworking how commands are called?
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Locusworks Team/eight-track/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Locusworks Team/eight-track/pipeline/head This commit looks good
				
			This commit is contained in:
		@@ -0,0 +1,12 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.annotations;
 | 
			
		||||
 | 
			
		||||
import java.lang.annotation.ElementType;
 | 
			
		||||
import java.lang.annotation.Retention;
 | 
			
		||||
import java.lang.annotation.RetentionPolicy;
 | 
			
		||||
import java.lang.annotation.Target;
 | 
			
		||||
 | 
			
		||||
@Retention(RetentionPolicy.RUNTIME)
 | 
			
		||||
@Target(ElementType.TYPE)
 | 
			
		||||
public @interface DiscordEventListener {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -34,6 +34,9 @@ import javax.sql.DataSource;
 | 
			
		||||
 | 
			
		||||
import org.flywaydb.core.Flyway;
 | 
			
		||||
import org.flywaydb.core.api.configuration.FluentConfiguration;
 | 
			
		||||
import org.flywaydb.core.api.logging.Log;
 | 
			
		||||
import org.flywaydb.core.api.logging.LogCreator;
 | 
			
		||||
import org.flywaydb.core.api.logging.LogFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
@@ -48,6 +51,8 @@ import org.springframework.orm.jpa.vendor.Database;
 | 
			
		||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
 | 
			
		||||
import org.springframework.transaction.PlatformTransactionManager;
 | 
			
		||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
 | 
			
		||||
@Configuration(value="net.locusworks.discord.eighttrack.database.config.EightTrackBeanConfiguration")
 | 
			
		||||
@EnableTransactionManagement
 | 
			
		||||
@@ -140,6 +145,7 @@ public class EightTrackBeanConfiguration {
 | 
			
		||||
 | 
			
		||||
  @Bean(name="flyway", initMethod="migrate")
 | 
			
		||||
  public Flyway flyway() {
 | 
			
		||||
    LogFactory.setLogCreator(new FlywayLogCreator());
 | 
			
		||||
    FluentConfiguration fc = Flyway.configure();
 | 
			
		||||
    fc.schemas("eighttrack");
 | 
			
		||||
    fc.table("_flyway_migration");
 | 
			
		||||
@@ -149,5 +155,53 @@ public class EightTrackBeanConfiguration {
 | 
			
		||||
 | 
			
		||||
    return fc.load();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private class FlywayLogCreator implements LogCreator {
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public Log createLogger(Class<?> clazz) {
 | 
			
		||||
      return new FlywayLog(clazz);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private class FlywayLog implements Log {
 | 
			
		||||
    
 | 
			
		||||
    private ApplicationLogger logger;
 | 
			
		||||
    
 | 
			
		||||
    public FlywayLog(Class<?> clazz) {
 | 
			
		||||
      logger = ApplicationLoggerFactory.getLogger(clazz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isDebugEnabled() {
 | 
			
		||||
      return logger.isDebugEnabled();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void debug(String message) {
 | 
			
		||||
      logger.debug(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void info(String message) {
 | 
			
		||||
      logger.info(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void warn(String message) {
 | 
			
		||||
      logger.warn(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void error(String message) {
 | 
			
		||||
      logger.error(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void error(String message, Exception e) {
 | 
			
		||||
      logger.error(message, e);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -36,14 +36,14 @@ import java.util.stream.Collectors;
 | 
			
		||||
import javax.sql.DataSource;
 | 
			
		||||
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.jdbc.DataSourceBuilder;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Primary;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
 | 
			
		||||
@Primary
 | 
			
		||||
@Component
 | 
			
		||||
@@ -62,7 +62,7 @@ public class EightTrackDataSource {
 | 
			
		||||
    mysqlProperties.put("serverTimezone", TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT));
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private Logger logger = LoggerFactory.getLogger(EightTrackDataSource.class);
 | 
			
		||||
  private ApplicationLogger logger = ApplicationLoggerFactory.getLogger(EightTrackDataSource.class);
 | 
			
		||||
  
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private ConfigurationService confService;
 | 
			
		||||
@@ -103,7 +103,7 @@ public class EightTrackDataSource {
 | 
			
		||||
    
 | 
			
		||||
    url = String.format("%s?%s", url, StringUtils.join(params, "&"));
 | 
			
		||||
    
 | 
			
		||||
    logger.debug("Database connection string: %s", url);
 | 
			
		||||
    logger.debug(String.format("Database connection string: %s", url));
 | 
			
		||||
    
 | 
			
		||||
    return DataSourceBuilder
 | 
			
		||||
        .create()
 | 
			
		||||
 
 | 
			
		||||
@@ -106,33 +106,6 @@ public class DiscordEventHandler extends ListenerAdapter {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public void onGuildJoin(GuildJoinEvent event) {
 | 
			
		||||
    try {
 | 
			
		||||
      long guildId = event.getGuild().getIdLong();
 | 
			
		||||
      DiscordGuild discordGuild =  guildSongRepoService.getGuildRepo().findByGuildId(guildId);
 | 
			
		||||
      if (discordGuild != null) {
 | 
			
		||||
        guildSongRepoService.logEntry(guildId, "Eight Track joined guild %s", discordGuild.getGuildName());
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      logger.debug("Joining Server: " + event.getGuild().getName());
 | 
			
		||||
 | 
			
		||||
      discordGuild = new DiscordGuild();
 | 
			
		||||
      discordGuild.setGuildId(guildId);
 | 
			
		||||
      discordGuild.setGuildName(event.getGuild().getName());
 | 
			
		||||
      discordGuild.setDateJoined(new Date());
 | 
			
		||||
 | 
			
		||||
      guildSongRepoService.getGuildRepo().save(discordGuild);
 | 
			
		||||
      
 | 
			
		||||
      guildSongRepoService.logEntry(guildId, "Eight Track joined guild %s", discordGuild.getGuildName());
 | 
			
		||||
      
 | 
			
		||||
    } catch (Exception ex) {
 | 
			
		||||
      logger.error("Unable to persist server information to database: " + ex.getMessage(), ex);
 | 
			
		||||
      guildSongRepoService.logEntry(event.getGuild().getIdLong(), ex, "Unable to persist guild server info to database: %s", ex.getMessage());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
 | 
			
		||||
    if (event.getAuthor().isBot()) return;
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,50 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.listeners;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
 | 
			
		||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
 | 
			
		||||
import net.locusworks.discord.eighttrack.annotations.DiscordEventListener;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@DiscordEventListener
 | 
			
		||||
public class DiscordGuildJoinEventListener extends ListenerAdapter {
 | 
			
		||||
  
 | 
			
		||||
  private ApplicationLogger logger = ApplicationLoggerFactory.getLogger(DiscordGuildJoinEventListener.class);
 | 
			
		||||
  
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private RepositoryService guildSongRepoService;
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public void onGuildJoin(GuildJoinEvent event) {
 | 
			
		||||
    try {
 | 
			
		||||
      long guildId = event.getGuild().getIdLong();
 | 
			
		||||
      DiscordGuild discordGuild =  guildSongRepoService.getGuildRepo().findByGuildId(guildId);
 | 
			
		||||
      if (discordGuild != null) {
 | 
			
		||||
        guildSongRepoService.logEntry(guildId, "Eight Track joined guild %s", discordGuild.getGuildName());
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      logger.debug("Joining Server: " + event.getGuild().getName());
 | 
			
		||||
 | 
			
		||||
      discordGuild = new DiscordGuild();
 | 
			
		||||
      discordGuild.setGuildId(guildId);
 | 
			
		||||
      discordGuild.setGuildName(event.getGuild().getName());
 | 
			
		||||
      discordGuild.setDateJoined(new Date());
 | 
			
		||||
 | 
			
		||||
      guildSongRepoService.getGuildRepo().save(discordGuild);
 | 
			
		||||
      
 | 
			
		||||
      guildSongRepoService.logEntry(guildId, "Eight Track joined guild %s", discordGuild.getGuildName());
 | 
			
		||||
      
 | 
			
		||||
    } catch (Exception ex) {
 | 
			
		||||
      logger.error("Unable to persist server information to database: " + ex.getMessage(), ex);
 | 
			
		||||
      guildSongRepoService.logEntry(event.getGuild().getIdLong(), ex, "Unable to persist guild server info to database: %s", ex.getMessage());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -30,14 +30,14 @@ package net.locusworks.discord.eighttrack.services;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.scheduling.annotation.Scheduled;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import net.dv8tion.jda.api.JDA;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.repos.GuildRepository;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class DatabaseCleanupService {
 | 
			
		||||
@@ -47,7 +47,7 @@ public class DatabaseCleanupService {
 | 
			
		||||
  private static final long HOUR = 60 * MINUTE;
 | 
			
		||||
  private static final long DAY = 24 * HOUR;
 | 
			
		||||
 | 
			
		||||
  private Logger logger = LoggerFactory.getLogger(DatabaseCleanupService.class);
 | 
			
		||||
  private ApplicationLogger logger = ApplicationLoggerFactory.getLogger(DatabaseCleanupService.class);
 | 
			
		||||
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private GuildRepository guildRepo;
 | 
			
		||||
 
 | 
			
		||||
@@ -27,42 +27,62 @@
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.services;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import javax.security.auth.login.LoginException;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.BeansException;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
 | 
			
		||||
import org.springframework.core.type.filter.TypeFilter;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import net.dv8tion.jda.api.AccountType;
 | 
			
		||||
import net.dv8tion.jda.api.JDA;
 | 
			
		||||
import net.dv8tion.jda.api.JDABuilder;
 | 
			
		||||
import net.locusworks.discord.eighttrack.handlers.DiscordEventHandler;
 | 
			
		||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
 | 
			
		||||
import net.locusworks.discord.eighttrack.annotations.DiscordEventListener;
 | 
			
		||||
import net.locusworks.discord.eighttrack.utils.BeanFinder;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class EightTrackService {
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  private ApplicationLogger logger = ApplicationLoggerFactory.getLogger(EightTrackService.class);
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  private static JDA client;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private ConfigurationService confService;
 | 
			
		||||
  
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private DiscordEventHandler eventListener;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  public void begin() throws LoginException {
 | 
			
		||||
    String token = confService.getDiscordToken();
 | 
			
		||||
    logger.debug("Logging in with token %s", token);
 | 
			
		||||
    
 | 
			
		||||
    client = new JDABuilder(AccountType.BOT).setToken(token).build();
 | 
			
		||||
    client.addEventListener(eventListener);
 | 
			
		||||
    try {
 | 
			
		||||
      
 | 
			
		||||
      final TypeFilter serviceFilter = new AnnotationTypeFilter(Service.class);
 | 
			
		||||
      final TypeFilter eventListenerFilter = new AnnotationTypeFilter(DiscordEventListener.class);
 | 
			
		||||
      
 | 
			
		||||
      List<ListenerAdapter> eventListeners = BeanFinder.autowireBeans(ListenerAdapter.class, "net.locusworks.discord.eighttrack", serviceFilter, eventListenerFilter);
 | 
			
		||||
      if (eventListeners == null ||eventListeners.size() < 1) {
 | 
			
		||||
        throw new LoginException("No event listeners found. Exiting");
 | 
			
		||||
      }
 | 
			
		||||
      
 | 
			
		||||
      String token = confService.getDiscordToken();
 | 
			
		||||
      JDABuilder builder = JDABuilder.createDefault(token);
 | 
			
		||||
      logger.debug("Logging in with token %s", token);
 | 
			
		||||
      
 | 
			
		||||
      logger.info(String.format("Loading %d listeners", eventListeners.size()));
 | 
			
		||||
      for (ListenerAdapter la : eventListeners) {
 | 
			
		||||
        logger.debug("Loading eventListener: %s", la.getClass().getName());
 | 
			
		||||
        builder.addEventListeners(la);
 | 
			
		||||
      }
 | 
			
		||||
      
 | 
			
		||||
      client = builder.build();
 | 
			
		||||
 | 
			
		||||
    } catch (BeansException e) {
 | 
			
		||||
      throw new LoginException(e.getMessage());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  public static JDA getClient() {
 | 
			
		||||
    return client;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,24 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.utils;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.BeansException;
 | 
			
		||||
import org.springframework.context.ApplicationContext;
 | 
			
		||||
import org.springframework.context.ApplicationContextAware;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
public class ApplicationContextHolder implements ApplicationContextAware {
 | 
			
		||||
  
 | 
			
		||||
 private static ApplicationContext context;
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public void setApplicationContext(ApplicationContext ctx) throws BeansException {
 | 
			
		||||
    if (context == null) {
 | 
			
		||||
      context = ctx;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public static ApplicationContext getContext() {
 | 
			
		||||
    return context;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,54 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.utils;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
import org.springframework.beans.BeansException;
 | 
			
		||||
import org.springframework.beans.factory.BeanInitializationException;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowire;
 | 
			
		||||
import org.springframework.beans.factory.config.BeanDefinition;
 | 
			
		||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
 | 
			
		||||
import org.springframework.core.type.classreading.MetadataReader;
 | 
			
		||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
 | 
			
		||||
import org.springframework.core.type.filter.TypeFilter;
 | 
			
		||||
 | 
			
		||||
public class BeanFinder {
 | 
			
		||||
  
 | 
			
		||||
  
 | 
			
		||||
  public static List<BeanDefinition> getBeans(String basePackage, TypeFilter... filters) {
 | 
			
		||||
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
 | 
			
		||||
    scanner.addIncludeFilter(new TypeFilter() {
 | 
			
		||||
      
 | 
			
		||||
      @Override
 | 
			
		||||
      public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
 | 
			
		||||
        boolean found = true;
 | 
			
		||||
        
 | 
			
		||||
        for (TypeFilter filter : filters) {
 | 
			
		||||
          found &= filter.match(metadataReader, metadataReaderFactory);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        return found;
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
    
 | 
			
		||||
    return scanner.findCandidateComponents(basePackage).stream().collect(Collectors.toList());
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  @SuppressWarnings("unchecked")
 | 
			
		||||
  public static <T> List<T> autowireBeans(Class<T> clazz, String basePackage, TypeFilter... filters) throws BeansException {
 | 
			
		||||
    try {
 | 
			
		||||
      List<T> list = new ArrayList<>();
 | 
			
		||||
      for (BeanDefinition bd : getBeans(basePackage, filters)) {
 | 
			
		||||
        Object obj = ApplicationContextHolder.getContext().getAutowireCapableBeanFactory().createBean(Class.forName(bd.getBeanClassName()), Autowire.BY_TYPE.value(), true);
 | 
			
		||||
        if (clazz.isInstance(obj)) {
 | 
			
		||||
          list.add((T) obj);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return list;
 | 
			
		||||
    } catch (Exception ex) {
 | 
			
		||||
      throw new BeanInitializationException(ex.getMessage(), ex);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user