MBeans... Tomcat also provides some insights. More information Tomcat MBean Names.
This sample snippets detects the port of a Tomcat running in the same VM:
private void probeTomcatPorts() throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
for (ObjectName obj : objs) {
String scheme = mbs.getAttribute(obj, "scheme").toString();
String port = obj.getKeyProperty("port");
LOG.info("Detected Tomcat port for scheme {}: {}", scheme, port);
defaultPorts.put(scheme, port);
}
}
Why...?! Maybe to detect the random port during integration testing.