在Tomcat 7下启动期间获取完整的Servlet URL

5

我有一个使用Tomcat 6编写的Web应用程序,现在想让它能够在Tomcat 7上运行。在启动应用程序时,除了其他事情之外,它还会将其Web服务组件注册到某个远程目录。为此,它需要提供自己的URL。下面是一个有点天真的方法,可以返回Web服务URL:

import org.apache.catalina.ServerFactory;
import org.apache.catalina.connector.Connector;
.
.
.
private String getWsUrl(ServletContext context)
            throws UnknownHostException, MalformedURLException {
    String host = java.net.InetAddress.getLocalHost().getCanonicalHostName();
    int port = -1;
    for (Connector c : ServerFactory.getServer().findServices()[0].findConnectors()) {
        if (c.getProtocol().contains("HTTP")) {
            port = c.getPort();
            break;
        }
    }
    URL wsURL = new URL("http", host, port, context.getContextPath()
                + C.WEB_SERVICE_PATH /* this is just a constant string */ );
    return wsURL.toString();
}

ServerFactory.getServer()这部分被证明存在问题:在Tomcat 7中没有org.apache.catalina.ServerFactory类。有没有关于如何重写这个问题的建议?我也很乐意拥有更具可移植性、非特定于Tomcat的代码。

2个回答

3
我遇到了同样的问题:我需要知道特定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;

// 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.
 * <ul>
 *   <li> httpPort - the HTTP port</li>
 *   <li> httpRedirectPort - the HTTP redirect port (which seems to be the SSL port) </li>
 * </ul>
 * @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<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>();

    // 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<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 { }
}

}


有趣的方法,但对于生产代码来说太不规范了。 因为原创性而加一分。 - Ilia K.
它能够工作,但是它将使用与Tomcat 7绑定。此外,不是针对此回复,而是针对原始帖子,.getLocalhost()在某些系统中的性能非常差,可能是由于Java 1.4.1查找IP6引起的。我想知道是否可以在启动后向servlet本身发出转储请求,通过HttpRequest获取必要的信息。 - Casper Ngo

0

我从未使用过它,而且它可能不会返回带有主机名和地址的URL,但是ServletContext.getResource("/")有没有可能做到你想要的?我知道它是用于通过servlet内部访问资源的,但你永远不知道。


它返回一个基于磁盘文件系统的URL,指向公共Web内容的根目录。所以不,这绝对不是OP想要的 :) - BalusC
好的。API文档没有说明它返回什么类型的URL,而且我也没有时间测试它。感谢您对此进行检查。 - Tim Yates

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接