Search code examples
javaxmljsfjbossweb.xml

Reading System Properties in Web.XML


in order to access global values stored in the file src/resources/settings.properties from web.xml on a JBoss EAP 7 Server, I implemented the following class from a similar Stack Overflow topic:

public class ConfigurationWebFilter implements ServletContextListener {
        protected static final Properties properties = new Properties();

   @Override
   public void contextInitialized(final ServletContextEvent event){
        try {
            try (InputStream stream = new FileInputStream("/settings.properties")) {
                properties.load(stream);
            }

             for (String prop : properties.stringPropertyNames())
                {
                 if (System.getProperty(prop) == null)
                 {
                     System.setProperty(prop, properties.getProperty(prop));
                 }
              }
        } catch (IOException ex) {
            logger.error("Failed loading settings from configuration file for web.xml", ex);
        }
    }

}

Then I added the according listener to web.xml:

  <listener>       
  <listener-class>
     com.product.util.ConfigurationWebFilter
  </listener-class>
  </listener>  

The code gets called properly and I can verify by debugging that the system variables get set correctly. However, the properties of my web.xml do not seem to be replaced/interpreted. The following parameter does still evaluate to ${serverName}, even after restarting the server and/or republishing:

<filter>
  <filter-name>CAS Authentication Filter</filter-name>
  <filter-class>(...)</filter-class>
  <init-param>
    <param-name>serverName</param-name>
    <param-value>${serverName}</param-value>
  </init-param>
</filter>

All the other topics on this issue were of no use because no solution worked for me. How can I replace web.xml parameters by values stored in a properties file?


Solution

  • Works now, I had to set a parameter related to the replacement of variables to true (was false) in the Wildfly standalone.xml.