3

I'm tryin to migrate some legacy (struts2-based) web application from Jboss to Open-Liberty server, and I'm wondering if there is a way to externalize the values of context-params (or filter init-params) from web.xml, like it is possible with the ${} syntax in server.xml or using mpConfig feature of eclipse microprofile. In the original project the param values were injected in web.xml at build time, using a placeholder substitution but, according to 12-factor 3rd recommendation, I would prefer to set this values outside software, in environment variables, for example. In my specific case I need to configure a servlet filter and a custom taglibrary with environment dependent param values.

I already tried to use the ${} syntax in web.xml, but no luck:

...
  <context-param>
    <param-name>remincl.resource.provider</param-name>
    <param-value>${remincl.resource.provider}</param-value>
  </context-param>
...

the runtime value of the context-param is: "${remincl.resource.provider}" instead of the actual value that is stored in an environment variable.

I think JEE specs doesn't allow this behavior, but I would like to know if open-liberty offers some extra feature to solve this problem. Otherwise I must keep injecting values at build time (or change the configuration strategy of both filter and taglib).

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61

1 Answers1

4

A JavaEE standard way to accomplish this would be using a javax.servlet.ServletContextListener.

For example:

@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Get the context value from wherever is most convenient:
        // System.getProperty(), System.getenv(), MP Config API, etc
        String value = System.getProperty("remincl.resource.provider");
        event.getServletContext().setInitParameter("remincl.resource.provider", value);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {}

}
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • 1
    I tried your solution: initially it seemed not working as intended (the value was not overwritten) but this was because you can't overwrite a context-param or a filter init-param (the method _setInitParameter()_ returns false if it was not set because this ServletContext already contains a context initialization parameter with a matching name) and, in my case, it was already defined in web.xml with the placeholder value. So I simply removed the declaration in web.xml and it worked. – Mauro Antonaci May 11 '19 at 07:39