Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I faced the same problem: I needed to know the port number to build a URL for a particular Tomcat instance, the port number might vary (because I run multiple instances for testing), and as of Tomcat 7 ServerFactory has gone away.</p> <p>I wrote the following code to find and parse the Tomcat server.xml file. It doesn't parse much, just getting the HTTP "port" and "redirectPort" values. It depends on the "catalina.home" or "catalina.base" System properties, which should exist in any running Tomcat instance. The beauty is that it doesn't depend on any Tomcat classes, and uses the JVM's XML parser.</p> <p>I hope this helps.</p> <pre><code>public final class TomcatConfigUtil { private static final String CONFIG_FILE_PATH = "conf/server.xml"; private static Map&lt;String, String&gt; properties = null; // No instances, please. private TomcatConfigUtil() { } /** * Get the configuration as a map of name/value pairs, or throw an exception if it wasn't found. * All values are returned as Strings. * &lt;ul&gt; * &lt;li&gt; httpPort - the HTTP port&lt;/li&gt; * &lt;li&gt; httpRedirectPort - the HTTP redirect port (which seems to be the SSL port) &lt;/li&gt; * &lt;/ul&gt; * @exception FileNotFoundException if the configuration file wasn't found * @exception IOException if there was a problem reading the configuration file * @exception SAXException if there was a problem parsing the configuration file */ public static synchronized Map&lt;String, String&gt; getConfig() throws FileNotFoundException, IOException, SAXException { if (properties != null) { return properties; } final File serverConfigFile = findServerConfigFile(); if (serverConfigFile == null) { throw new FileNotFoundException("Couldn't find the configuration file."); } final Map&lt;String, String&gt; tmpProperties = new HashMap&lt;String, String&gt;(); // Content-handler does the actual parsing. final ServerConfigContentHandler contentHandler = new ServerConfigContentHandler(tmpProperties); final XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setContentHandler(contentHandler); // Pass the config file as the input source for parsing. final FileReader fileReader = new FileReader(serverConfigFile); xmlReader.parse(new InputSource(fileReader)); fileReader.close(); return (properties = Collections.unmodifiableMap(tmpProperties)); } private static File findServerConfigFile() { if (System.getProperty("catalina.home") != null) { final File file = new File(System.getProperty("catalina.home"), CONFIG_FILE_PATH); if (file.isFile()) { return file; } } if (System.getProperty("catalina.base") != null) { final File file = new File(System.getProperty("catalina.base"), CONFIG_FILE_PATH); if (file.isFile()) { return file; } } return null; } /** * ContentHandler implementation for the XML parser. */ private static class ServerConfigContentHandler implements ContentHandler { private final Map&lt;String, String&gt; map; private boolean inServerElement; private boolean inCatalinaServiceElement; private ServerConfigContentHandler(final Map&lt;String, String&gt; map) { this.map = map; } @Override public void startDocument() throws SAXException { this.inServerElement = false; this.inCatalinaServiceElement = false; } @Override public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException { if (!this.inServerElement &amp;&amp; "Server".equals(localName)) { this.inServerElement = true; } else if (this.inServerElement &amp;&amp; "Service".equals(localName) &amp;&amp; "Catalina".equals(atts.getValue("name"))) { this.inCatalinaServiceElement = true; } else if (this.inCatalinaServiceElement &amp;&amp; "Connector".equals(localName) &amp;&amp; "HTTP/1.1".equals(atts.getValue("protocol"))) { if ((atts.getValue("SSLEnabled") == null || "false".equals(atts.getValue("SSLEnabled"))) &amp;&amp; (atts.getValue("secure") == null || "false".equals(atts.getValue("secure"))) &amp;&amp; (atts.getValue("scheme") == null || "http".equals(atts.getValue("scheme")))) { final String portStr = atts.getValue("port"); if (portStr != null) { this.map.put("httpPort", portStr); } final String redirectPortStr = atts.getValue("redirectPort"); if (redirectPortStr != null) { this.map.put("httpRedirectPort", redirectPortStr); } } } } @Override public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (this.inCatalinaServiceElement &amp;&amp; "Service".equals(localName)) { this.inCatalinaServiceElement = false; } else if (this.inServerElement &amp;&amp; "Server".equals(localName)) { this.inServerElement = false; } } @Override public void endDocument() throws SAXException { this.inServerElement = false; this.inCatalinaServiceElement = false; } @Override public void characters(final char[] ch, final int start, final int length) throws SAXException { } @Override public void endPrefixMapping(final String prefix) throws SAXException { } @Override public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException { } @Override public void processingInstruction(final String target, final String data) throws SAXException { } @Override public void setDocumentLocator(final Locator locator) { } @Override public void skippedEntity(final String name) throws SAXException { } @Override public void startPrefixMapping(final String prefix, final String uri) throws SAXException { } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload