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.