Project

General

Profile

1
package eu.dnetlib.services.hcm;
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.Arrays;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.TreeMap;
12
import java.util.UUID;
13
import java.util.stream.Collectors;
14

    
15
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
import org.dom4j.DocumentHelper;
21
import org.dom4j.Element;
22
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
import org.springframework.core.io.Resource;
28
import org.springframework.core.io.ResourceLoader;
29
import org.springframework.core.io.support.ResourcePatternUtils;
30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
32
import org.springframework.web.bind.annotation.RestController;
33

    
34
import com.google.common.collect.Maps;
35

    
36
import eu.dnetlib.conf.DnetGenericApplicationProperties;
37
import eu.dnetlib.enabling.annotations.DnetService;
38
import eu.dnetlib.enabling.annotations.DnetServiceType;
39
import eu.dnetlib.miscutils.datetime.DateUtils;
40
import eu.dnetlib.miscutils.datetime.HumanTime;
41
import eu.dnetlib.miscutils.streams.DnetStreamSupport;
42
import eu.dnetlib.services.BaseService;
43

    
44
@RestController
45
@RequestMapping(value = "/hcm", method = RequestMethod.GET)
46
@DnetService(DnetServiceType.hcm)
47
public class HCM extends BaseService {
48

    
49
	private static final Log log = LogFactory.getLog(HCM.class);
50

    
51
	@Autowired
52
	private Environment env;
53

    
54
	@Autowired
55
	private DnetGenericApplicationProperties containerConfiguration;
56

    
57
	@Autowired
58
	private ResourceLoader resourceLoader;
59

    
60
	@RequestMapping(value = "properties", method = RequestMethod.GET)
61
	public Map<String, Object> listProperties() {
62

    
63
		final Iterator<PropertySource<?>> iter = ((AbstractEnvironment) env).getPropertySources().iterator();
64
		return DnetStreamSupport.generateStreamFromIterator(iter)
65
				.filter(ps -> ps instanceof MapPropertySource)
66
				.map(ps -> (MapPropertySource) ps)
67
				.collect(Collectors.toMap(MapPropertySource::getName, MapPropertySource::getSource));
68

    
69
	}
70

    
71
	@RequestMapping(value = "info", method = RequestMethod.GET)
72
	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
	@RequestMapping(value = "jvm", method = RequestMethod.GET)
83
	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
	@RequestMapping(value = "libs", method = RequestMethod.GET)
98
	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
	@RequestMapping(value = "maven", method = RequestMethod.GET)
109
	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

    
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
					modules.get(groupId).put(name, new TreeMap<>());
134
				}
135
				if (!modules.get(groupId).get(name).containsKey(version)) {
136
					modules.get(groupId).get(name).put(version, new ArrayList<>());
137
				}
138

    
139
				modules.get(groupId).get(name).get(version).add(res.getURI().toString());
140
			} 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
	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
}
    (1-1/1)