Project

General

Profile

1 41837 michele.ar
package eu.dnetlib.functionality.modular.ui;
2
3 41838 michele.ar
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 41837 michele.ar
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 41838 michele.ar
import com.google.common.collect.Maps;
22 41837 michele.ar
23 41838 michele.ar
import eu.dnetlib.conf.PropertyFetcher;
24
import eu.dnetlib.conf.WebappContextPropertyLocationFactory;
25
26 41837 michele.ar
/**
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 41838 michele.ar
	private static final Log log = LogFactory.getLog(ContainerPropertiesController.class);
34 41837 michele.ar
35 41838 michele.ar
	private final ResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
36 41837 michele.ar
37 41838 michele.ar
	@Autowired
38
	private PropertyFetcher propertyFetcher;
39 41837 michele.ar
40 41838 michele.ar
	private Properties propertyFetcherProps;
41 41837 michele.ar
42 41838 michele.ar
	@Autowired
43
	private WebappContextPropertyLocationFactory propertyLocations;
44 41837 michele.ar
45 41838 michele.ar
	@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 41837 michele.ar
51 41838 michele.ar
	private Map<String, List<PropertyInfo>> fetchProperties() throws IOException {
52
		final Map<String, List<PropertyInfo>> propertiesMap = Maps.newTreeMap();
53 41837 michele.ar
54 41838 michele.ar
		// 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 41837 michele.ar
63 41838 michele.ar
				// 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 41837 michele.ar
72 41838 michele.ar
					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 41837 michele.ar
79 41838 michele.ar
		// 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 41837 michele.ar
91 41838 michele.ar
		return propertiesMap;
92
	}
93 41837 michele.ar
94 41838 michele.ar
	/**
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 41837 michele.ar
108 41838 michele.ar
	private class PropertyInfo {
109 41837 michele.ar
110 41838 michele.ar
		String value;
111
		String sourcePropertyFile;
112
		Boolean isChecked;
113 41837 michele.ar
114 41838 michele.ar
		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 41837 michele.ar
120 41838 michele.ar
		public String getValue() {
121
			return this.value;
122
		}
123 41837 michele.ar
124 41838 michele.ar
		public String getSourcePropertyFile() {
125
			return this.sourcePropertyFile;
126
		}
127 41837 michele.ar
128 41838 michele.ar
		public Boolean getIsChecked() {
129
			return this.isChecked;
130
		}
131
	}
132 41837 michele.ar
}