我遇到了同样的问题:我需要知道特定Tomcat实例的端口号以构建URL,端口号可能会有所变化(因为我运行多个实例进行测试),而自Tomcat 7起,ServerFactory已经不存在了。
我编写了以下代码来查找和解析Tomcat server.xml文件。它没有解析太多内容,只获取HTTP“port”和“redirectPort”值。它依赖于“catalina.home”或“catalina.base”系统属性,在任何运行的Tomcat实例中都应该存在。美妙的是,它不依赖于任何Tomcat类,并使用JVM的XML解析器。
希望这可以帮助你。
public final class TomcatConfigUtil {
private static final String CONFIG_FILE_PATH = "conf/server.xml";
private static Map<String, String> properties = null;
private TomcatConfigUtil() { }
public static synchronized Map<String, String> 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<String, String> tmpProperties = new HashMap<String, String>();
final ServerConfigContentHandler contentHandler = new ServerConfigContentHandler(tmpProperties);
final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(contentHandler);
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;
}
private static class ServerConfigContentHandler implements ContentHandler {
private final Map<String, String> map;
private boolean inServerElement;
private boolean inCatalinaServiceElement;
private ServerConfigContentHandler(final Map<String, String> 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 && "Server".equals(localName)) {
this.inServerElement = true;
}
else if (this.inServerElement && "Service".equals(localName) && "Catalina".equals(atts.getValue("name"))) {
this.inCatalinaServiceElement = true;
}
else if (this.inCatalinaServiceElement && "Connector".equals(localName) && "HTTP/1.1".equals(atts.getValue("protocol"))) {
if ((atts.getValue("SSLEnabled") == null || "false".equals(atts.getValue("SSLEnabled"))) &&
(atts.getValue("secure") == null || "false".equals(atts.getValue("secure"))) &&
(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 && "Service".equals(localName)) {
this.inCatalinaServiceElement = false;
}
else if (this.inServerElement && "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 { }
}
}