How-to reload properties on-the-fly

This how-to article will demonstrate a way to get the on-the-fly update property values from a configuration file.

String fileURL = Bootstrap.getServerHome(Bootstrap.CONFIGHOME)+"/conf/[Application-Name]/Application.xml";
// Get server xml shortcut function
// String fileURL = Bootstrap.getServerXMLURL();
if (fileURL != null)
{
	try
	{
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
		Document doc = domBuilder.parse(fileURL);

		XPathFactory factory = XMLUtils.newXPathFactory();
		XPath xpath = factory.newXPath();

		if (doc != null)
		{
			Element root = doc.getDocumentElement();
			String version = XMLUtils.getVersion(root);  // unused currently

			String propertiesXPath = "/Root/Application/Properties/Property";

                        // For server properties 
                        // String propertiesXPath = "/Root/Server/Properties/Property";

			WMSProperties properties = new WMSProperties(); 
			XMLUtils.loadConfigProperies(root, propertiesXPath, properties);
		
                        // How to obtain new property value
                        String newPropertyValue = properties.getPropertyStr("YOUR-PROPERTY-NAME", null);
		        WMSLoggerFactory.getLogger(getClass()).error("newPropertyValue: "+newPropertyValue);
 		}
	}
	catch (Exception e)
	{
		WMSLoggerFactory.getLogger(getClass()).error("Error no file", e);
	}
}

Comments are closed.