Main.java (2482B)
1 package fr.kevincorvisier.mtg.dd; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.net.MalformedURLException; 6 import java.net.URISyntaxException; 7 import java.net.URL; 8 import java.util.Collection; 9 10 import org.springframework.beans.BeansException; 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 13 import org.springframework.core.io.support.ResourcePropertySource; 14 import org.springframework.stereotype.Service; 15 16 import fr.kevincorvisier.mtg.dd.consumers.DecklistConsumersService; 17 import fr.kevincorvisier.mtg.dd.downloaders.DecklistDownloader; 18 import lombok.RequiredArgsConstructor; 19 import lombok.extern.slf4j.Slf4j; 20 21 @Slf4j 22 @Service 23 @RequiredArgsConstructor 24 public class Main 25 { 26 private final DecklistConsumersService consumers; 27 private final Collection<DecklistDownloader> downloaders; 28 29 @Value("#{'${sources}'.split('\\|')}") 30 private final Collection<URL> sources; 31 32 public void run() throws MalformedURLException, URISyntaxException 33 { 34 try 35 { 36 for (final URL url : sources) 37 { 38 final DecklistDownloader downloader = downloaders.stream().filter(d -> d.accept(url)).findAny().orElse(null); 39 if (downloader == null) 40 { 41 log.warn("No downloader for URL: {}", url); 42 continue; 43 } 44 45 downloader.download(url); 46 } 47 } 48 finally 49 { 50 consumers.saveToFolder(); 51 } 52 } 53 54 public static void main(final String[] args) throws IOException, BeansException, IllegalStateException, URISyntaxException 55 { 56 for (final File file : getConfigurations()) 57 { 58 final File realFile = file.toPath().toRealPath().toFile(); 59 60 try (final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) 61 { 62 context.getEnvironment().getPropertySources().addLast(new ResourcePropertySource(realFile.toURI().toString())); 63 context.getEnvironment().getPropertySources().addLast(new ResourcePropertySource("application.properties")); 64 context.scan("fr.kevincorvisier.mtg.dd"); 65 context.refresh(); 66 67 context.getBean(Main.class).run(); 68 } 69 catch (final Exception e) 70 { 71 log.error("main: realFile={}", realFile, e); 72 } 73 } 74 } 75 76 private static File[] getConfigurations() throws URISyntaxException 77 { 78 final File configEnabled = new File(Main.class.getClassLoader().getResource("config-enabled").toURI()); 79 80 return configEnabled.listFiles(pathname -> pathname.isFile() && pathname.getName().endsWith(".properties")); 81 } 82 }