Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui;
2

    
3
import java.io.IOException;
4
import java.lang.management.ManagementFactory;
5
import java.lang.management.RuntimeMXBean;
6
import java.util.ArrayList;
7
import java.util.Collections;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Map.Entry;
12

    
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpServletResponse;
15

    
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.apache.maven.model.Model;
19
import org.apache.maven.model.Parent;
20
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
21
import org.springframework.beans.factory.annotation.Required;
22
import org.springframework.context.ResourceLoaderAware;
23
import org.springframework.core.io.Resource;
24
import org.springframework.core.io.ResourceLoader;
25
import org.springframework.core.io.support.ResourcePatternUtils;
26
import org.springframework.ui.ModelMap;
27

    
28
import com.google.common.collect.Lists;
29
import com.google.common.collect.Maps;
30

    
31
import eu.dnetlib.miscutils.datetime.DateUtils;
32
import eu.dnetlib.miscutils.datetime.HumanTime;
33

    
34
public class InfoController extends ModuleEntryPoint implements ResourceLoaderAware {
35

    
36
	private String hostname;
37
	private String port;
38
	private String context;
39

    
40
	private ResourceLoader resourceLoader;
41

    
42
	private static final Log log = LogFactory.getLog(InfoController.class);
43

    
44
	@Override
45
	protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
46
		final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
47

    
48
		final Map<String, Map<String, String>> info = Maps.newLinkedHashMap();
49
		info.put("General", getGeneralInfo(mxbean));
50
		info.put("JVM", getJvmInfo(mxbean));
51
		info.put("Libraries and arguments", getLibInfo(mxbean));
52
		info.put("System properties", getSysInfo(mxbean));
53

    
54
		map.addAttribute("info", info);
55
		map.addAttribute("modules", getModules());
56

    
57
	}
58

    
59
	@SuppressWarnings("unchecked")
60
	private List<Map<String, Object>> getModules() throws IOException {
61
		final long start = System.currentTimeMillis();
62
		try {
63

    
64
			final Map<String, Map<String, Map<String, Object>>> modules = Maps.newLinkedHashMap();
65

    
66
			final MavenXpp3Reader reader = new MavenXpp3Reader();
67
			for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) {
68
				try {
69
					final Model model = reader.read(res.getInputStream());
70

    
71
					final String name = model.getArtifactId();
72

    
73
					String groupId = model.getGroupId();
74
					for (Parent parent = model.getParent(); (groupId == null) && (model.getParent() != null); parent = model.getParent()) {
75
						groupId = parent.getGroupId();
76
					}
77

    
78
					String version = model.getVersion();
79
					for (Parent parent = model.getParent(); (version == null) && (model.getParent() != null); parent = model.getParent()) {
80
						version = parent.getVersion();
81
					}
82

    
83
					if (!modules.containsKey(groupId)) {
84
						modules.put(groupId, new HashMap<String, Map<String, Object>>());
85
					}
86
					if (!modules.get(groupId).containsKey(name)) {
87
						final Map<String, Object> map = Maps.newHashMap();
88
						map.put("group", groupId);
89
						map.put("name", name);
90
						map.put("files", new ArrayList<String>());
91
						map.put("versions", new ArrayList<String>());
92
						modules.get(groupId).put(name, map);
93
					} else {
94
						// Artifact already found
95
						modules.get(groupId).get(name).put("warning", "1");
96
					}
97
					((List<String>) modules.get(groupId).get(name).get("versions")).add(version);
98
					((List<String>) modules.get(groupId).get(name).get("files")).add(res.getURI().toString());
99
				} catch (final Exception e) {
100
					log.error("Error evaluating pom: " + res.getURI());
101
					log.debug("-- ERROR --", e);
102
				}
103
			}
104

    
105
			final List<Map<String, Object>> list = Lists.newArrayList();
106
			for (final Entry<String, Map<String, Map<String, Object>>> e : modules.entrySet()) {
107
				for (final Entry<String, Map<String, Object>> e1 : e.getValue().entrySet()) {
108
					list.add(e1.getValue());
109
				}
110
			}
111

    
112
			Collections.sort(list, (o1, o2) -> {
113
				if (o1.get("group").equals(o2.get("group"))) {
114
					return o1.get("name").toString().compareTo(o2.get("name").toString());
115
				} else {
116
					return o1.get("group").toString().compareTo(o2.get("group").toString());
117
				}
118
			});
119

    
120
			return list;
121
		} finally {
122
			log.debug(" - getModules(): " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
123
		}
124
	}
125

    
126
	private Map<String, String> getSysInfo(final RuntimeMXBean mxbean) {
127
		final long start = System.currentTimeMillis();
128
		try {
129
			return mxbean.getSystemProperties();
130
		} finally {
131
			log.debug(" - getSysInfo(): " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
132
		}
133

    
134
	}
135

    
136
	private Map<String, String> getGeneralInfo(final RuntimeMXBean mxbean) {
137
		final long start = System.currentTimeMillis();
138
		try {
139
			final Map<String, String> genInfo = Maps.newLinkedHashMap();
140
			genInfo.put("Hostname", hostname);
141
			genInfo.put("Port", port);
142
			genInfo.put("Context", context);
143
			genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime()));
144
			genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime()));
145
			return genInfo;
146
		} finally {
147
			log.debug(" - getGeneralInfo(): " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
148
		}
149

    
150
	}
151

    
152
	private Map<String, String> getJvmInfo(final RuntimeMXBean mxbean) {
153
		final long start = System.currentTimeMillis();
154
		try {
155
			final Map<String, String> jvmInfo = Maps.newLinkedHashMap();
156
			jvmInfo.put("JVM Name", mxbean.getVmName());
157
			jvmInfo.put("JVM Vendor", mxbean.getVmVendor());
158
			jvmInfo.put("JVM Version", mxbean.getVmVersion());
159
			jvmInfo.put("JVM Spec Name", mxbean.getSpecName());
160
			jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor());
161
			jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion());
162
			jvmInfo.put("Running JVM Name", mxbean.getName());
163
			jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion());
164
			return jvmInfo;
165
		} finally {
166
			log.debug(" - getJvmInfo(): " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
167
		}
168

    
169
	}
170

    
171
	private Map<String, String> getLibInfo(final RuntimeMXBean mxbean) {
172
		final long start = System.currentTimeMillis();
173
		try {
174
			final Map<String, String> libInfo = Maps.newLinkedHashMap();
175
			libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : "));
176
			libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : "));
177
			libInfo.put("Input arguments", mxbean.getInputArguments().toString());
178
			libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : "));
179
			return libInfo;
180
		} finally {
181
			log.debug(" - getLibInfo(): " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
182
		}
183

    
184
	}
185

    
186
	public String getHostname() {
187
		return hostname;
188
	}
189

    
190
	@Required
191
	public void setHostname(final String hostname) {
192
		this.hostname = hostname;
193
	}
194

    
195
	public String getPort() {
196
		return port;
197
	}
198

    
199
	@Required
200
	public void setPort(final String port) {
201
		this.port = port;
202
	}
203

    
204
	public String getContext() {
205
		return context;
206
	}
207

    
208
	@Required
209
	public void setContext(final String context) {
210
		this.context = context;
211
	}
212

    
213
	@Override
214
	public void setResourceLoader(final ResourceLoader resourceLoader) {
215
		this.resourceLoader = resourceLoader;
216
	}
217
}
(8-8/17)