views:

33

answers:

1

I have a web application written using Tomcat 6 and I'm trying to make it work with Tomcat 7. During its startup the application, besides other things, registers its web service component at some remote directory. For this it needs to supply its own URL. The following (a little bit naive) method should return webservice 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();
}

The ServerFactory.getServer() part is turned out to be problematic: there is no org.apache.catalina.ServerFactory class in Tomcat 7. Any suggestions on how to rewrite this for Tomcat 7? I would also be happy to have more portable, non tomcat-specific code.

A: 

I've never used it, and it probably doesn't return a URL with the hostname and address, but is there any chance ServletContext.getResource("/") will do what you want? I know it's intended for accessing a resource internally by the servlet, but you never know.

Tim Yates
It returns a **disk file system** based `URL` pointing to the root of the public webcontent. So no, this absolutely doesn't do what the OP wants :)
BalusC
Okay. The API docs don't specify what kind of URL it returns, and I didn't have time to test it. Thanks for checking on that.
Tim Yates