Skip to content

Instantly share code, notes, and snippets.

@iambigd
Created January 22, 2014 06:28
Show Gist options
  • Save iambigd/8554290 to your computer and use it in GitHub Desktop.
Save iambigd/8554290 to your computer and use it in GitHub Desktop.
How to get TOMCAT properties from server.xml configuration file
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.NonWritableChannelException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class TomcatConfig {
// private static Log LOGGER = LogFactory.getLog(TomcatConfig.class);
// String serverXmlPath = "C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 6.0\\conf\\server.xml";
public static String SERVER_XML =
"C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 6.0\\conf\\server.xml";
// "/usr/local/TOMCAT/conf/server.xml";
public static Integer PORT_NUMBER = null;
public static Integer getTomcatPortFromConfigXml() {
// FileLock lock = null;
FileInputStream fis = null;
if(PORT_NUMBER == null){
// LOGGER.debug("Port number is null");
try {
fis = new FileInputStream(SERVER_XML);
// lock = fis.getChannel().tryLock(0L, Long.MAX_VALUE, true);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(fis);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/Server/Service[@name='Catalina']/Connector[count(@scheme)=0]/@port[1]");
String result = (String) expr.evaluate(doc, XPathConstants.STRING);
PORT_NUMBER = result != null && result.length() > 0 ? Integer.valueOf(result) : null;
System.out.println("PORT_NUMBER:" + PORT_NUMBER);
fis.close();
// lock.release();
} catch (FileNotFoundException e) {
PORT_NUMBER = null;
// e.printStackTrace();
// LOGGER.error(e.getMessage());
} catch (XPathExpressionException e) {
PORT_NUMBER = null;
// e.printStackTrace();
// LOGGER.error(e.getMessage());
} catch (SAXException e) {
PORT_NUMBER = null;
// e.printStackTrace();
// LOGGER.error(e.getMessage());
} catch (ParserConfigurationException e) {
PORT_NUMBER = null;
e.printStackTrace();
// LOGGER.error(e.getMessage());
} catch (IOException e) {
PORT_NUMBER = null;
e.printStackTrace();
// LOGGER.error(e.getMessage());
} catch (NonWritableChannelException e){
// LOGGER.error(e.getMessage());
e.printStackTrace();
}
}
return PORT_NUMBER;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment