Is there a better way than this?

Specifically - detect running Tomcat, then if under Tomcat (today only 
interested in v7 and v9) obtain the version string as described [1] and shown 
on the Manager web application.

import org.apache.catalina.core.*;
//...
    public void init(ServletConfig config) throws ServletException
    {
        //java.util.logging...
        log.log(Level.INFO, "config={0}", config);
        ServletContext sc = config.getServletContext();
        log.log(Level.INFO, "ServletContext={0}", sc);
        if (sc instanceof ApplicationContextFacade)
        {
            try
            {
                ApplicationContextFacade acf = (ApplicationContextFacade) sc;
                Field applicationContextField = 
ApplicationContextFacade.class.getDeclaredField("context");
                applicationContextField.setAccessible(true);
                ApplicationContext applicationContext = (ApplicationContext) 
applicationContextField.get(acf);
                Field standardContextField = 
ApplicationContext.class.getDeclaredField("context");
                standardContextField.setAccessible(true);
                StandardContext standardContext = (StandardContext) 
standardContextField.get(applicationContext);
                log.log(Level.INFO, "version={0}", 
standardContext.getWebappVersion());
            }
            catch (RuntimeException | NoSuchFieldException | 
IllegalAccessException e)
            {
                log.log(Level.WARNING, "unable", e);
            }
        }
    }

1: https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming

--
Jason Pyeron  | Architect
PD Inc        |
10 w 24th St  |
Baltimore, MD |
 
.mil: jason.j.pyeron....@mail.mil
.com: jpye...@pdinc.us
tel : 202-741-9397




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to