1 |
41837
|
michele.ar
|
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.Comparator;
|
9 |
|
|
import java.util.HashMap;
|
10 |
|
|
import java.util.List;
|
11 |
|
|
import java.util.Map;
|
12 |
|
|
import java.util.Map.Entry;
|
13 |
|
|
|
14 |
|
|
import javax.servlet.http.HttpServletRequest;
|
15 |
|
|
import javax.servlet.http.HttpServletResponse;
|
16 |
|
|
|
17 |
|
|
import org.apache.commons.logging.Log;
|
18 |
|
|
import org.apache.commons.logging.LogFactory;
|
19 |
|
|
import org.apache.maven.model.Model;
|
20 |
|
|
import org.apache.maven.model.Parent;
|
21 |
|
|
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
22 |
|
|
import org.springframework.beans.factory.annotation.Required;
|
23 |
|
|
import org.springframework.context.ResourceLoaderAware;
|
24 |
|
|
import org.springframework.core.io.Resource;
|
25 |
|
|
import org.springframework.core.io.ResourceLoader;
|
26 |
|
|
import org.springframework.core.io.support.ResourcePatternUtils;
|
27 |
|
|
import org.springframework.ui.ModelMap;
|
28 |
|
|
|
29 |
|
|
import com.google.common.collect.Lists;
|
30 |
|
|
import com.google.common.collect.Maps;
|
31 |
|
|
|
32 |
|
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
33 |
|
|
import eu.dnetlib.miscutils.datetime.HumanTime;
|
34 |
|
|
|
35 |
|
|
public class InfoController extends ModuleEntryPoint implements ResourceLoaderAware {
|
36 |
|
|
|
37 |
|
|
private String hostname;
|
38 |
|
|
private String port;
|
39 |
|
|
private String context;
|
40 |
|
|
|
41 |
|
|
private ResourceLoader resourceLoader;
|
42 |
|
|
|
43 |
|
|
private static final Log log = LogFactory.getLog(InfoController.class);
|
44 |
|
|
|
45 |
|
|
@Override
|
46 |
|
|
protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
|
47 |
|
|
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
|
48 |
|
|
|
49 |
|
|
final Map<String, Map<String, String>> info = Maps.newLinkedHashMap();
|
50 |
|
|
info.put("General", getGeneralInfo(mxbean));
|
51 |
|
|
info.put("JVM", getJvmInfo(mxbean));
|
52 |
|
|
info.put("Libraries and arguments", getLibInfo(mxbean));
|
53 |
|
|
info.put("System properties", getSysInfo(mxbean));
|
54 |
|
|
|
55 |
|
|
map.addAttribute("info", info);
|
56 |
|
|
map.addAttribute("modules", getModules());
|
57 |
|
|
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
@SuppressWarnings("unchecked")
|
61 |
|
|
private List<Map<String, Object>> getModules() throws IOException {
|
62 |
|
|
final Map<String, Map<String, Map<String, Object>>> modules = Maps.newLinkedHashMap();
|
63 |
|
|
|
64 |
|
|
final MavenXpp3Reader reader = new MavenXpp3Reader();
|
65 |
|
|
for (Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) {
|
66 |
|
|
try {
|
67 |
|
|
final Model model = reader.read(res.getInputStream());
|
68 |
|
|
|
69 |
|
|
final String name = model.getArtifactId();
|
70 |
|
|
|
71 |
|
|
String groupId = model.getGroupId();
|
72 |
|
|
for (Parent parent = model.getParent(); groupId == null && model.getParent() != null; parent = model.getParent()) {
|
73 |
|
|
groupId = parent.getGroupId();
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
String version = model.getVersion();
|
77 |
|
|
for (Parent parent = model.getParent(); version == null && model.getParent() != null; parent = model.getParent()) {
|
78 |
|
|
version = parent.getVersion();
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
if (!modules.containsKey(groupId)) {
|
82 |
|
|
modules.put(groupId, new HashMap<String, Map<String, Object>>());
|
83 |
|
|
}
|
84 |
|
|
if (!modules.get(groupId).containsKey(name)) {
|
85 |
|
|
final Map<String, Object> map = Maps.newHashMap();
|
86 |
|
|
map.put("group", groupId);
|
87 |
|
|
map.put("name", name);
|
88 |
|
|
map.put("files", new ArrayList<String>());
|
89 |
|
|
map.put("versions", new ArrayList<String>());
|
90 |
|
|
modules.get(groupId).put(name, map);
|
91 |
|
|
} else {
|
92 |
|
|
// Artifact already found
|
93 |
|
|
modules.get(groupId).get(name).put("warning", "1");
|
94 |
|
|
}
|
95 |
|
|
((List<String>) modules.get(groupId).get(name).get("versions")).add(version);
|
96 |
|
|
((List<String>) modules.get(groupId).get(name).get("files")).add(res.getURI().toString());
|
97 |
|
|
} catch (Exception e) {
|
98 |
|
|
log.error("Error evaluating pom: " + res.getURI());
|
99 |
|
|
log.debug("-- ERROR --", e);
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
final List<Map<String, Object>> list = Lists.newArrayList();
|
104 |
|
|
for (Entry<String, Map<String, Map<String, Object>>> e : modules.entrySet()) {
|
105 |
|
|
for (Entry<String, Map<String, Object>> e1 : e.getValue().entrySet()) {
|
106 |
|
|
list.add(e1.getValue());
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
Collections.sort(list, new Comparator<Map<String, Object>>() {
|
111 |
|
|
|
112 |
|
|
@Override
|
113 |
|
|
public int compare(final Map<String, Object> o1, final Map<String, Object> o2) {
|
114 |
|
|
if (o1.get("group").equals(o2.get("group"))) {
|
115 |
|
|
return o1.get("name").toString().compareTo(o2.get("name").toString());
|
116 |
|
|
} else {
|
117 |
|
|
return o1.get("group").toString().compareTo(o2.get("group").toString());
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
});
|
121 |
|
|
|
122 |
|
|
return list;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
private Map<String, String> getSysInfo(final RuntimeMXBean mxbean) {
|
126 |
|
|
return mxbean.getSystemProperties();
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
private Map<String, String> getGeneralInfo(final RuntimeMXBean mxbean) {
|
130 |
|
|
final Map<String, String> genInfo = Maps.newLinkedHashMap();
|
131 |
|
|
genInfo.put("Hostname", hostname);
|
132 |
|
|
genInfo.put("Port", port);
|
133 |
|
|
genInfo.put("Context", context);
|
134 |
|
|
genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime()));
|
135 |
|
|
genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime()));
|
136 |
|
|
return genInfo;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
private Map<String, String> getJvmInfo(final RuntimeMXBean mxbean) {
|
140 |
|
|
final Map<String, String> jvmInfo = Maps.newLinkedHashMap();
|
141 |
|
|
jvmInfo.put("JVM Name", mxbean.getVmName());
|
142 |
|
|
jvmInfo.put("JVM Vendor", mxbean.getVmVendor());
|
143 |
|
|
jvmInfo.put("JVM Version", mxbean.getVmVersion());
|
144 |
|
|
jvmInfo.put("JVM Spec Name", mxbean.getSpecName());
|
145 |
|
|
jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor());
|
146 |
|
|
jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion());
|
147 |
|
|
jvmInfo.put("Running JVM Name", mxbean.getName());
|
148 |
|
|
jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion());
|
149 |
|
|
return jvmInfo;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
private Map<String, String> getLibInfo(final RuntimeMXBean mxbean) {
|
153 |
|
|
final Map<String, String> libInfo = Maps.newLinkedHashMap();
|
154 |
|
|
libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : "));
|
155 |
|
|
libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : "));
|
156 |
|
|
libInfo.put("Input arguments", mxbean.getInputArguments().toString());
|
157 |
|
|
libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : "));
|
158 |
|
|
return libInfo;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
public String getHostname() {
|
162 |
|
|
return hostname;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
@Required
|
166 |
|
|
public void setHostname(final String hostname) {
|
167 |
|
|
this.hostname = hostname;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
public String getPort() {
|
171 |
|
|
return port;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
@Required
|
175 |
|
|
public void setPort(final String port) {
|
176 |
|
|
this.port = port;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
public String getContext() {
|
180 |
|
|
return context;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
@Required
|
184 |
|
|
public void setContext(final String context) {
|
185 |
|
|
this.context = context;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
@Override
|
189 |
|
|
public void setResourceLoader(final ResourceLoader resourceLoader) {
|
190 |
|
|
this.resourceLoader = resourceLoader;
|
191 |
|
|
}
|
192 |
|
|
}
|