1
|
package eu.dnetlib.services;
|
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.Iterator;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
import java.util.TreeMap;
|
11
|
import java.util.stream.Collectors;
|
12
|
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.maven.model.Model;
|
16
|
import org.apache.maven.model.Parent;
|
17
|
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.context.ResourceLoaderAware;
|
20
|
import org.springframework.core.env.AbstractEnvironment;
|
21
|
import org.springframework.core.env.Environment;
|
22
|
import org.springframework.core.env.MapPropertySource;
|
23
|
import org.springframework.core.env.PropertySource;
|
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.web.bind.annotation.RequestMapping;
|
28
|
import org.springframework.web.bind.annotation.RestController;
|
29
|
|
30
|
import com.google.common.collect.Maps;
|
31
|
|
32
|
import eu.dnetlib.conf.DnetGenericApplicationProperties;
|
33
|
import eu.dnetlib.enabling.annotations.DnetService;
|
34
|
import eu.dnetlib.enabling.annotations.DnetServiceType;
|
35
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
36
|
import eu.dnetlib.miscutils.datetime.HumanTime;
|
37
|
import eu.dnetlib.miscutils.streams.DnetStreamSupport;
|
38
|
|
39
|
@RestController
|
40
|
@RequestMapping("/hcm")
|
41
|
@DnetService(DnetServiceType.hcm)
|
42
|
public class HCMController extends BaseService implements ResourceLoaderAware {
|
43
|
|
44
|
private static final Log log = LogFactory.getLog(HCMController.class);
|
45
|
|
46
|
@Autowired
|
47
|
private Environment env;
|
48
|
|
49
|
@Autowired
|
50
|
private DnetGenericApplicationProperties containerConfiguration;
|
51
|
|
52
|
private ResourceLoader resourceLoader;
|
53
|
|
54
|
@RequestMapping("properties")
|
55
|
public Map<String, Object> listProperties() {
|
56
|
|
57
|
final Iterator<PropertySource<?>> iter = ((AbstractEnvironment) env).getPropertySources().iterator();
|
58
|
return DnetStreamSupport.generateStreamFromIterator(iter)
|
59
|
.filter(ps -> ps instanceof MapPropertySource)
|
60
|
.map(ps -> (MapPropertySource) ps)
|
61
|
.collect(Collectors.toMap(MapPropertySource::getName, MapPropertySource::getSource));
|
62
|
|
63
|
}
|
64
|
|
65
|
@RequestMapping("info")
|
66
|
public Map<String, Object> info() {
|
67
|
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
|
68
|
final Map<String, Object> genInfo = Maps.newLinkedHashMap();
|
69
|
genInfo.put("Hostname", containerConfiguration.getHost());
|
70
|
genInfo.put("Port", containerConfiguration.getPort());
|
71
|
genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime()));
|
72
|
genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime()));
|
73
|
return genInfo;
|
74
|
}
|
75
|
|
76
|
@RequestMapping("jvm")
|
77
|
public Map<String, String> jvmInfo() {
|
78
|
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
|
79
|
final Map<String, String> jvmInfo = Maps.newLinkedHashMap();
|
80
|
jvmInfo.put("JVM Name", mxbean.getVmName());
|
81
|
jvmInfo.put("JVM Vendor", mxbean.getVmVendor());
|
82
|
jvmInfo.put("JVM Version", mxbean.getVmVersion());
|
83
|
jvmInfo.put("JVM Spec Name", mxbean.getSpecName());
|
84
|
jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor());
|
85
|
jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion());
|
86
|
jvmInfo.put("Running JVM Name", mxbean.getName());
|
87
|
jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion());
|
88
|
return jvmInfo;
|
89
|
}
|
90
|
|
91
|
@RequestMapping("libs")
|
92
|
public Map<String, String> libInfo() {
|
93
|
final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
|
94
|
final Map<String, String> libInfo = Maps.newLinkedHashMap();
|
95
|
libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : "));
|
96
|
libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : "));
|
97
|
libInfo.put("Input arguments", mxbean.getInputArguments().toString());
|
98
|
libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : "));
|
99
|
return libInfo;
|
100
|
}
|
101
|
|
102
|
@RequestMapping("maven")
|
103
|
private Map<String, Map<String, Map<String, List<String>>>> mavenModules() throws IOException {
|
104
|
final Map<String, Map<String, Map<String, List<String>>>> modules = new TreeMap<>();
|
105
|
|
106
|
final MavenXpp3Reader reader = new MavenXpp3Reader();
|
107
|
for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) {
|
108
|
try {
|
109
|
final Model model = reader.read(res.getInputStream());
|
110
|
|
111
|
final String name = model.getArtifactId();
|
112
|
|
113
|
String groupId = model.getGroupId();
|
114
|
for (Parent parent = model.getParent(); (groupId == null) && (model.getParent() != null); parent = model.getParent()) {
|
115
|
groupId = parent.getGroupId();
|
116
|
}
|
117
|
|
118
|
String version = model.getVersion();
|
119
|
for (Parent parent = model.getParent(); (version == null) && (model.getParent() != null); parent = model.getParent()) {
|
120
|
version = parent.getVersion();
|
121
|
}
|
122
|
|
123
|
if (!modules.containsKey(groupId)) {
|
124
|
modules.put(groupId, new TreeMap<>());
|
125
|
}
|
126
|
if (!modules.get(groupId).containsKey(name)) {
|
127
|
modules.get(groupId).put(name, new TreeMap<>());
|
128
|
}
|
129
|
if (!modules.get(groupId).get(name).containsKey(version)) {
|
130
|
modules.get(groupId).get(name).put(version, new ArrayList<>());
|
131
|
}
|
132
|
|
133
|
modules.get(groupId).get(name).get(version).add(res.getURI().toString());
|
134
|
} catch (final Exception e) {
|
135
|
log.warn("Error evaluating pom: " + res.getURI());
|
136
|
log.debug("-- ERROR --", e);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
return modules;
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
public void setResourceLoader(final ResourceLoader resourceLoader) {
|
145
|
this.resourceLoader = resourceLoader;
|
146
|
}
|
147
|
|
148
|
}
|