1
|
package eu.dnetlib.functionality.modular.ui;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Map.Entry;
|
8
|
import java.util.Properties;
|
9
|
|
10
|
import javax.servlet.http.HttpServletRequest;
|
11
|
import javax.servlet.http.HttpServletResponse;
|
12
|
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.core.io.Resource;
|
17
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
18
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
19
|
import org.springframework.ui.ModelMap;
|
20
|
|
21
|
import com.google.common.collect.Maps;
|
22
|
|
23
|
import eu.dnetlib.conf.PropertyFetcher;
|
24
|
import eu.dnetlib.conf.WebappContextPropertyLocationFactory;
|
25
|
|
26
|
/**
|
27
|
* Modular UI that displays collected properties and shows winning ones.
|
28
|
*
|
29
|
* @author Andrea Mannocci
|
30
|
*/
|
31
|
public class ContainerPropertiesController extends ModuleEntryPoint {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(ContainerPropertiesController.class);
|
34
|
|
35
|
private final ResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
|
36
|
|
37
|
@Autowired
|
38
|
private PropertyFetcher propertyFetcher;
|
39
|
|
40
|
private Properties propertyFetcherProps;
|
41
|
|
42
|
@Autowired
|
43
|
private WebappContextPropertyLocationFactory propertyLocations;
|
44
|
|
45
|
@Override
|
46
|
protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
|
47
|
this.propertyFetcherProps = this.propertyFetcher.getProps();
|
48
|
map.addAttribute("properties", fetchProperties());
|
49
|
}
|
50
|
|
51
|
private Map<String, List<PropertyInfo>> fetchProperties() throws IOException {
|
52
|
final Map<String, List<PropertyInfo>> propertiesMap = Maps.newTreeMap();
|
53
|
|
54
|
// cycle over all property locations know to dnet
|
55
|
for (final String location : this.propertyLocations.getLocations()) {
|
56
|
log.debug(String.format("Loading properties from %s", location));
|
57
|
// loop over all *.properties files matching the location
|
58
|
for (final Resource propertyFile : this.pathResolver.getResources(location)) {
|
59
|
final Properties properties = new Properties();
|
60
|
log.debug(String.format("URL: %s", propertyFile.getURL()));
|
61
|
properties.load(propertyFile.getInputStream());
|
62
|
|
63
|
// loop over all properties set within a property file file
|
64
|
for (final Entry<Object, Object> property : properties.entrySet()) {
|
65
|
List<PropertyInfo> propertyInfoList;
|
66
|
if (propertiesMap.containsKey(property.getKey())) {
|
67
|
propertyInfoList = propertiesMap.get(property.getKey());
|
68
|
} else {
|
69
|
propertyInfoList = new ArrayList<>();
|
70
|
}
|
71
|
|
72
|
propertyInfoList.add(new PropertyInfo((String) property.getValue(), propertyFile.getFilename(),
|
73
|
isWinning((String) property.getKey(), (String) property.getValue())));
|
74
|
propertiesMap.put((String) property.getKey(), propertyInfoList);
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
|
79
|
// Scans the Map just created for properties overridden externally (e.g. by Tomcat)
|
80
|
/*
|
81
|
* for (String property : propertiesMap.keySet()) { List<PropertyInfo> propertyInfoList = propertiesMap.get(property); boolean
|
82
|
* isOverridden = true; for (PropertyInfo propInfo : propertyInfoList) { // Checks for a checker if (propInfo.isChecked) {
|
83
|
* isOverridden = false; } }
|
84
|
*
|
85
|
* // If none of the pairs has been checked, then the property has been overridden externally. if (isOverridden &&
|
86
|
* propertyFetcherProps.getProperty(property) != null) { // Adds the current property value (which is the override) as the current
|
87
|
* one. propertyInfoList.add(new PropertyInfo(propertyFetcherProps.getProperty(property), "Overridden externally", true));
|
88
|
* propertiesMap.put(property, propertyInfoList); } }
|
89
|
*/
|
90
|
|
91
|
return propertiesMap;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Checks whether a given property-value is the one currently in use.
|
96
|
*
|
97
|
* @param property
|
98
|
* The property key
|
99
|
* @param value
|
100
|
* The property value
|
101
|
* @return true/false
|
102
|
*/
|
103
|
private Boolean isWinning(final String property, final String value) {
|
104
|
final String winningPropertyValue = this.propertyFetcherProps.getProperty(property);
|
105
|
return value.equals(winningPropertyValue);
|
106
|
}
|
107
|
|
108
|
private class PropertyInfo {
|
109
|
|
110
|
String value;
|
111
|
String sourcePropertyFile;
|
112
|
Boolean isChecked;
|
113
|
|
114
|
public PropertyInfo(final String value, final String sourcePropertyFile, final Boolean isChecked) {
|
115
|
this.value = value;
|
116
|
this.sourcePropertyFile = sourcePropertyFile;
|
117
|
this.isChecked = isChecked;
|
118
|
}
|
119
|
|
120
|
public String getValue() {
|
121
|
return this.value;
|
122
|
}
|
123
|
|
124
|
public String getSourcePropertyFile() {
|
125
|
return this.sourcePropertyFile;
|
126
|
}
|
127
|
|
128
|
public Boolean getIsChecked() {
|
129
|
return this.isChecked;
|
130
|
}
|
131
|
}
|
132
|
}
|