/
AppConfig.java
108 lines (89 loc) · 3.86 KB
/
AppConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package au.org.ala.biocache.config;
import au.org.ala.biocache.service.RestartDataService;
import au.org.ala.biocache.service.SpeciesLookupIndexService;
import au.org.ala.biocache.service.SpeciesLookupRestService;
import au.org.ala.biocache.service.SpeciesLookupService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.web.client.RestOperations;
import javax.inject.Inject;
/**
* This class handles the switching between implementations of interfaces based on
* external configuration.
*/
@Configuration
public class AppConfig {
private final static Logger logger = Logger.getLogger(AppConfig.class);
@Inject
private AbstractMessageSource messageSource; // use for i18n of the headers
@Value("${name.index.dir:/data/lucene/namematching}")
protected String nameIndexLocation;
@Inject
@Qualifier("restTemplate")
private RestOperations restTemplate; // NB MappingJacksonHttpMessageConverter() injected by Spring
/** URI prefix for bie-service - may be overridden in properties file */
@Value("${service.bie.ws.url:http://bie.ala.org.au/ws}")
protected String bieUriPrefix;
//NC 20131018: Allow service to be disabled via config (enabled by default)
@Value("${service.bie.enabled:false}")
protected Boolean enabled;
//Disable the default that autocomplete uses the local names index. For use when there are no local names index files.
@Value("${service.autocomplete.local.enabled:true}")
protected Boolean autocompleteLocalEnabled;
// Configuration for facets
@Value("${facet.config:/data/biocache/config/facets.json}")
protected String facetConfig;
@Value("${facets.max:4}")
protected Integer facetsMax;
@Value("${facet.default:true}")
protected Boolean facetDefault;
@Value("${restart.data.dir:/tmp}")
public void setDatabase(String dir) {
RestartDataService.dir = dir;
}
protected SpeciesLookupService getSpeciesLookupRestService() {
logger.info("Initialising rest-based species lookup services.");
SpeciesLookupRestService service = new SpeciesLookupRestService();
service.setBieUriPrefix(bieUriPrefix);
service.setEnabled(enabled);
service.setRestTemplate(restTemplate);
service.setMessageSource(messageSource);
return service;
}
protected SpeciesLookupService getSpeciesLookupIndexService() {
logger.info("Initialising local index-based species lookup services.");
SpeciesLookupIndexService service = new SpeciesLookupIndexService();
service.setNameIndexLocation(nameIndexLocation);
service.setMessageSource(messageSource);
return service;
}
public @Bean(name = "speciesLookupService")
SpeciesLookupService speciesLookupServiceBean() {
logger.info("Initialising species lookup services.");
if(enabled){
return getSpeciesLookupRestService();
} else {
return getSpeciesLookupIndexService();
}
}
public @Bean(name = "speciesLookupIndexService")
SpeciesLookupService speciesLookupIndexService() {
logger.info("Initialising species lookup services.");
try {
if (autocompleteLocalEnabled) {
return getSpeciesLookupIndexService();
}
} catch (Exception e) {
logger.error("Failed to initialise local species lookup service for use with the species autocomplete ws. Attempting to use BIE instead.");
}
if (enabled) {
return getSpeciesLookupRestService();
} else {
return null;
}
}
}