|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2017 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.config; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javax.annotation.PostConstruct; |
| 25 | + |
| 26 | +import org.apache.commons.lang3.StringUtils; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 32 | +import org.springframework.context.ApplicationEventPublisher; |
| 33 | +import org.springframework.context.ApplicationListener; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 37 | +import org.springframework.core.env.EnumerablePropertySource; |
| 38 | +import org.springframework.core.env.PropertySource; |
| 39 | + |
| 40 | +import lombok.RequiredArgsConstructor; |
| 41 | + |
| 42 | +// CheckStyle: ignore AvoidStarImportCheck for next 1 line |
| 43 | +import ru.mystamps.web.controller.event.*; // NOPMD: UnusedImports (false positive) |
| 44 | +import ru.mystamps.web.util.extractor.SiteParser; |
| 45 | + |
| 46 | +@Configuration |
| 47 | +@RequiredArgsConstructor |
| 48 | +public class EventsConfig { |
| 49 | + |
| 50 | + private static final Logger LOG = LoggerFactory.getLogger(EventsConfig.class); |
| 51 | + |
| 52 | + private final ServicesConfig servicesConfig; |
| 53 | + private final ApplicationEventPublisher eventPublisher; |
| 54 | + private final ConfigurableBeanFactory beanFactory; |
| 55 | + private final ConfigurableEnvironment env; |
| 56 | + |
| 57 | + @PostConstruct |
| 58 | + public void init() { |
| 59 | + Map<Integer, SiteParser> parsers = createSiteParsers(); |
| 60 | + for (Map.Entry<Integer, SiteParser> entry : parsers.entrySet()) { |
| 61 | + Integer num = entry.getKey(); |
| 62 | + SiteParser parser = entry.getValue(); |
| 63 | + if (!parser.isFullyInitialized()) { |
| 64 | + LOG.warn("Ignored non-fully initialized site parser (app.site-parser[{}])", num); |
| 65 | + continue; |
| 66 | + } |
| 67 | + LOG.trace("Registering site parser for '{}'", parser); |
| 68 | + beanFactory.registerSingleton("siteParser" + num, parser); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + public ApplicationListener<ImportRequestCreated> getImportRequestCreatedEventListener() { |
| 74 | + return new ImportRequestCreatedEventListener( |
| 75 | + LoggerFactory.getLogger(ImportRequestCreatedEventListener.class), |
| 76 | + servicesConfig.getSeriesDownloaderService(), |
| 77 | + servicesConfig.getSeriesImportService(), |
| 78 | + eventPublisher |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public ApplicationListener<DownloadingFailed> getDownloadingFailedEventListener() { |
| 84 | + return new DownloadingFailedEventListener( |
| 85 | + LoggerFactory.getLogger(DownloadingFailedEventListener.class), |
| 86 | + servicesConfig.getSeriesImportService() |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + public ApplicationListener<DownloadingSucceeded> getDownloadingSucceededEventListener( |
| 92 | + List<SiteParser> siteParsers) { |
| 93 | + |
| 94 | + return new DownloadingSucceededEventListener( |
| 95 | + LoggerFactory.getLogger(DownloadingSucceededEventListener.class), |
| 96 | + servicesConfig.getSeriesImportService(), |
| 97 | + siteParsers, |
| 98 | + eventPublisher |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + public ApplicationListener<ParsingFailed> getParsingFailedEventListener() { |
| 104 | + return new ParsingFailedEventListener( |
| 105 | + LoggerFactory.getLogger(ParsingFailedEventListener.class), |
| 106 | + servicesConfig.getSeriesImportService() |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + @SuppressWarnings("PMD.ModifiedCyclomaticComplexity") // TODO: deal with it someday |
| 111 | + private Map<Integer, SiteParser> createSiteParsers() { |
| 112 | + boolean foundSiteParserProps = false; |
| 113 | + Map<Integer, SiteParser> parsers = new HashMap<>(); |
| 114 | + |
| 115 | + for (PropertySource<?> source : env.getPropertySources()) { |
| 116 | + // while we expect that properties will be in PropertiesPropertySource, we use |
| 117 | + // EnumerablePropertySource to also handle systemProperties and systemEnvironment |
| 118 | + // that are MapPropertySource and SystemEnvironmentPropertySource respectively |
| 119 | + if (!(source instanceof EnumerablePropertySource<?>)) { |
| 120 | + LOG.trace("Ignored property source: {} ({})", source.getName(), source.getClass()); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + LOG.trace("Inspecting property source: {} ({})", source.getName(), source.getClass()); |
| 125 | + |
| 126 | + for (String name : ((EnumerablePropertySource<?>)source).getPropertyNames()) { |
| 127 | + if (!name.startsWith("app.site-parser")) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + String propertyValue = (String)source.getProperty(name); |
| 132 | + LOG.trace("Detected property '{}' with value {}", name, propertyValue); |
| 133 | + |
| 134 | + // extract parser number (app.site-parser[2].name -> 2) |
| 135 | + String strNum = StringUtils.substringBetween(name, "[", "]"); |
| 136 | + if (StringUtils.isBlank(strNum)) { |
| 137 | + LOG.warn("Ignored property '{}': could not extract index", name); |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + Integer num = Integer.valueOf(strNum); |
| 142 | + if (!parsers.containsKey(num)) { |
| 143 | + SiteParser parser = |
| 144 | + new SiteParser(); // NOPMD: AvoidInstantiatingObjectsInLoops |
| 145 | + parsers.put(num, parser); |
| 146 | + } |
| 147 | + |
| 148 | + // extract parser property (app.site-parser[2].name -> name) |
| 149 | + String fieldName = StringUtils.substringAfterLast(name, "."); |
| 150 | + if (StringUtils.isBlank(fieldName)) { |
| 151 | + LOG.warn("Ignored property '{}': could not extract property name", name); |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + SiteParser parser = parsers.get(num); |
| 156 | + boolean validProperty = parser.setField(fieldName, propertyValue); |
| 157 | + if (!validProperty) { |
| 158 | + LOG.warn("Ignored property '{}': unknown or unsupported", name); |
| 159 | + continue; |
| 160 | + } |
| 161 | + foundSiteParserProps = true; |
| 162 | + } |
| 163 | + // we shouldn't process others to be able to override a property |
| 164 | + if (foundSiteParserProps) { |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return parsers; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments