0

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?

Knight
  • 53
  • 1
  • 5
  • Hi Knight there are many ways you can parse the xml like JaxB,DocumentBuilders,Jsoup etc out that you can use DocumentBuilder to read the xml and set the value from java – Pradeep Oct 13 '17 at 13:27
  • https://www.mkyong.com/java/how-to-modify-xml-file-in-java-dom-parser/ – Pradeep Oct 13 '17 at 13:27
  • Please go through this link – Pradeep Oct 13 '17 at 13:28
  • Hi Pradeep, thank you, but I don't think that this website is of any help; I don't want to parse arbitrary XML code and replace values. The web.xml is the configuration file of the web application, which should be sort of "parametrized" by vales found in a separate .properties file. – Knight Oct 15 '17 at 19:48

1 Answers1

1

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

Knight
  • 53
  • 1
  • 5