Accessing cloudbees-web.xml <context-param> values

189 Views Asked by At

I have a project that I am deploying to CloudBees and I have defined some param-names inside my cloudbees-web.xml file. I would like to access these from my java application but have tried System.getProperty(),(String)env.lookup("email.user.name") all with no luck.

How can I access these from within Java?

Below is my cloudbees-web.xml file which is located under WEB-INF:

            <?xml version="1.0"?>
            <cloudbees-web-app xmlns="http://www.cloudbees.com/xml/webapp/1">
              <!-- Changed for Privacy -->
              <appid>CB_ACCOUNT/APPNAME</appid>

              <!-- Extra context parameters -->
              <context-param>
                <param-name>email.user.name</param-name>
                <param-value>[email protected]</param-value>
              </context-param>
            </cloudbees-web-app>
1

There are 1 best solutions below

0
On BEST ANSWER

You can access those context parameters just like you can access any context parameter defined in your WEB-INF/web.xml, in other words:

getServletContext().getInitParameter("email.user.name")

Note that you need to be in a class instance that has access to the ServletContext, or else you need to pass the ServletContext from a class that has access to it.

Typically you will have requests served by a Servlet or JSP page... or the framework you are using will provide a means to access the context (e.g. Java Server Faces provides the external context concept - which is either a ServletContext or a PortletContext because JSF supports both containers) so in those cases it is just a question of accessing the parameter from your servlet.

If you are starting background threads to do work (which is strictly against the Servlet specification... or at least out of scope) you should be starting (and stopping - don't forget to stop them) those threads via a ServletContextListener... which also is fed the ServletContext... if you are a bold person and starting background threads from class static initializers... well your only solution is to have a ServletContextListener pull the config and hand it over to the background thread... at which point you are better off starting the tread from the ServletContextListener (also solves the loading multiple contexts from the same .war file issue... not that you'll have that on RUN@cloud)