17
17
*/
18
18
package ru .mystamps .web .feature .series .importing .event ;
19
19
20
- import java .util .HashMap ;
21
- import java .util .Map ;
22
-
23
- import javax .annotation .PostConstruct ;
24
-
25
- import org .apache .commons .lang3 .StringUtils ;
26
-
27
20
import org .slf4j .Logger ;
28
21
import org .slf4j .LoggerFactory ;
29
22
30
- import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
31
23
import org .springframework .context .ApplicationEventPublisher ;
32
24
import org .springframework .context .ApplicationListener ;
33
25
import org .springframework .context .annotation .Bean ;
34
26
import org .springframework .context .annotation .Configuration ;
35
- import org .springframework .core .env .ConfigurableEnvironment ;
36
- import org .springframework .core .env .EnumerablePropertySource ;
37
- import org .springframework .core .env .PropertySource ;
38
27
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
39
28
40
29
import lombok .RequiredArgsConstructor ;
41
30
42
31
import ru .mystamps .web .config .ServicesConfig ;
43
32
import ru .mystamps .web .feature .series .importing .SeriesImportService ;
44
33
import ru .mystamps .web .feature .series .importing .extractor .JdbcSiteParserDao ;
45
- import ru .mystamps .web .feature .series .importing .extractor .JsoupSiteParser ;
46
- import ru .mystamps .web .feature .series .importing .extractor .SiteParser ;
47
34
import ru .mystamps .web .feature .series .importing .extractor .SiteParserDao ;
48
35
import ru .mystamps .web .feature .series .importing .extractor .SiteParserService ;
49
36
import ru .mystamps .web .feature .series .importing .extractor .SiteParserServiceImpl ;
50
- import ru .mystamps .web .feature .series .importing .extractor .TimedSiteParser ;
51
37
52
38
@ Configuration
53
39
@ RequiredArgsConstructor
@@ -58,25 +44,8 @@ public class EventsConfig {
58
44
private final SeriesImportService seriesImportService ;
59
45
private final ServicesConfig servicesConfig ;
60
46
private final ApplicationEventPublisher eventPublisher ;
61
- private final ConfigurableBeanFactory beanFactory ;
62
- private final ConfigurableEnvironment env ;
63
47
private final NamedParameterJdbcTemplate jdbcTemplate ;
64
48
65
- @ PostConstruct
66
- public void init () {
67
- Map <Integer , SiteParser > parsers = createSiteParsers ();
68
- for (Map .Entry <Integer , SiteParser > entry : parsers .entrySet ()) {
69
- Integer num = entry .getKey ();
70
- SiteParser parser = entry .getValue ();
71
- if (!parser .isFullyInitialized ()) {
72
- LOG .warn ("Ignored non-fully initialized site parser (app.site-parser[{}])" , num );
73
- continue ;
74
- }
75
- LOG .trace ("Registering site parser for '{}'" , parser );
76
- beanFactory .registerSingleton ("siteParser" + num , parser );
77
- }
78
- }
79
-
80
49
@ Bean
81
50
public SiteParserService siteParserService (SiteParserDao siteParserDao ) {
82
51
return new SiteParserServiceImpl (
@@ -121,74 +90,4 @@ public ApplicationListener<ParsingFailed> getParsingFailedEventListener() {
121
90
);
122
91
}
123
92
124
- @ SuppressWarnings ({
125
- "PMD.AvoidInstantiatingObjectsInLoops" ,
126
- "PMD.ModifiedCyclomaticComplexity" // TODO: deal with it someday
127
- })
128
- private Map <Integer , SiteParser > createSiteParsers () {
129
- boolean foundSiteParserProps = false ;
130
- Map <Integer , SiteParser > parsers = new HashMap <>();
131
-
132
- for (PropertySource <?> source : env .getPropertySources ()) {
133
- // while we expect that properties will be in PropertiesPropertySource, we use
134
- // EnumerablePropertySource to also handle systemProperties and systemEnvironment
135
- // that are MapPropertySource and SystemEnvironmentPropertySource respectively
136
- if (!(source instanceof EnumerablePropertySource <?>)) {
137
- LOG .trace ("Ignored property source: {} ({})" , source .getName (), source .getClass ());
138
- continue ;
139
- }
140
-
141
- LOG .trace ("Inspecting property source: {} ({})" , source .getName (), source .getClass ());
142
-
143
- for (String name : ((EnumerablePropertySource <?>)source ).getPropertyNames ()) {
144
- if (!name .startsWith ("app.site-parser" )) {
145
- continue ;
146
- }
147
-
148
- String propertyValue = (String )source .getProperty (name );
149
- LOG .trace ("Detected property '{}' with value {}" , name , propertyValue );
150
-
151
- // extract parser number (app.site-parser[2].name -> 2)
152
- String strNum = StringUtils .substringBetween (name , "[" , "]" );
153
- if (StringUtils .isBlank (strNum )) {
154
- LOG .warn ("Ignored property '{}': could not extract index" , name );
155
- continue ;
156
- }
157
-
158
- Integer num = Integer .valueOf (strNum );
159
- if (!parsers .containsKey (num )) {
160
- // @todo #801 EventsConfig.createSiteParsers(): split the logic for properties
161
- // parsing and the object instantiation
162
- SiteParser parser =
163
- new TimedSiteParser (
164
- LoggerFactory .getLogger (TimedSiteParser .class ),
165
- new JsoupSiteParser ()
166
- );
167
- parsers .put (num , parser );
168
- }
169
-
170
- // extract parser property (app.site-parser[2].name -> name)
171
- String fieldName = StringUtils .substringAfterLast (name , "." );
172
- if (StringUtils .isBlank (fieldName )) {
173
- LOG .warn ("Ignored property '{}': could not extract property name" , name );
174
- continue ;
175
- }
176
-
177
- SiteParser parser = parsers .get (num );
178
- boolean validProperty = parser .setField (fieldName , propertyValue );
179
- if (!validProperty ) {
180
- LOG .warn ("Ignored property '{}': unknown or unsupported" , name );
181
- continue ;
182
- }
183
- foundSiteParserProps = true ;
184
- }
185
- // we shouldn't process others to be able to override a property
186
- if (foundSiteParserProps ) {
187
- break ;
188
- }
189
- }
190
-
191
- return parsers ;
192
- }
193
-
194
93
}
0 commit comments