Reputation: 125
I have developed an API in Eclipse deployed on Apache Tomcat Server which modifies an XML file kept in the ROOT folder of the server.
Apache Tomcat is installed in the D drive in my system and hence I've hard-coded that path in the API like this
new File("D:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ROOT\\example\\example.xml")
Also I've configured the server to use port 8080 hence I've also hard-coded the URI path in the API like this
String uriPath = "http://localhost:8080/example/example2";
And it works fine.
But now I want to deploy the same API in the form of a WAR file on different systems having Apache Tomcat.
How do I obtain the Apache Tomcat ROOT folder path and the port number for those systems programmatically so that one API works for all systems? How do I integrate that into the API?
Upvotes: 1
Views: 2117
Reputation: 9767
There are a number of reasons within the JEE specification that state why you shouldn't do this but, that aside, you can get a platform agnostic way of determining the tomcat location.
The system defines two variables on startup, CATALINA_HOME and CATALINA_BASE. You should be able to get CATALINA_BASE from the system using System.getProperty("catalina.base")
From there you could hypothetically build up a path like this:
final String catalinaBase= System.getProperty("catalina.base");
final File catalinaBaseDir= new File(catalinaBase);
final File exampleXML= new File(catalinaBase, "webapps/root/example/example.xml");
As for the URI path, this article should provide you with enough reference to do what you want.
Upvotes: 3