Project

General

Profile

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