ContainerDetector utility class
(See attachments for current snapshot versions) Small utility class for detecting active Servlet Container without access to a ServletContext instance. Mostly meant for use inside logging / diagnostic code. Note there is no container-neutral way to obtain this information, so it has to be done on try-each-known-case basis. Currently it supports Tomcat and Glassfish. Note there are no dependencies on javax.servlet.* packages, so it doesn't fail when they're not present. There is no logging, as one of its intended uses is inside a logging framework, so introducing logging could require unwanted dependencies or circular nasties. Plans:- support Jetty
- add some basic System.out logging controlled by a system property
- support other containers
Example code
import net.lilianne_blaze.debugutils.*; ... // long, all-inclusive string suitable for logging / diagnostics String s = ContainerDetector.getContainerInfo().toString(); // "tomcat" or "sun-appserver"; likely to remain contant between releases, // especially useful combined with one of the following props to check for // versions with known bugs String type = ContainerDetector.getContainerInfo().getProperties().get(ContainerInfo.SERVLET_CONTAINER_SHORT_NAME); // works on Glassfish, Tomcat returns null String buildVersion = ContainerDetector.getContainerInfo().getProperties().get(ContainerInfo.SERVLET_CONTAINER_BUILD); // works on Tomcat, Glassfish returns null String buildDate = ContainerDetector.getContainerInfo().getProperties().get(ContainerInfo.SERVLET_CONTAINER_DATE);
Example output
Tomcat 6.0.16
ServletContext.getServerInfo() = Apache Tomcat/6.0.16 ContainerDetector.getContainerInfo() = Apache Tomcat/6.0.16.0 Jan 28 2008 11:35:29 Additional properties:- servlet.container.date.asString = Jan 28 2008 11:35:29
- servlet.container.longName = Apache Tomcat
- servlet.container.shortName = tomcat
- servlet.container.version.asString = 6.0.16.0
Glassfish
ServletContext.getServerInfo() = Sun Java System Application Server 9.1.1 ContainerDetector.getContainerInfo() = Sun Java System Application Server 9.1.1 (build b38-fcs)- servlet.container.build.asString = b38-fcs
- servlet.container.longName = Sun Java System Application Server
- servlet.container.shortName = sun-appserver
- servlet.container.version.asString = 9.1.1
Known issues
Too strict SecurityManagers are very likely to prevent detection.FAQ
Q: What is the project's main goal? A: To provide a viable alternative to ServletContext.getServerInfo() which can be accessed at any point, from any class and thread. Q: And why would such a thing be needed? A: I started this project to be able to access this data from inside a logging framework, which obviously must be able to initialize long before a ServletContext instance is available. Also, typical getServerInfo implementations tend to return only name and major/minor version, while ContainerDetector makes best effort to get full build version and date where available. Q: Why there's only basic System.out logging? A: See the above question. It must work reliably when called from inside a Log4J or Logback code. Anything other than System.out would introduce unwanted dependencies and/or nasty circulars. Q: Why user is able to set server description string ? I can set "I'n not a java server" :D or something :D Furby A: To make it more test-friendly. In general I don't believe in protecting users from themselves. Q: Why both NONE_OR_UNKNOWN and UNKNOWN? A: UNKNOWN means there is a servlet container. NONE_OR_UNKNOWN means servlet classes are available, but without any guarantees whether they are part of a container or a mock objects framework.Legalese
It is available under Apache Licence 2.0 and WTFPL, with the following exception: if you choose to use any non-final version (i.e. one that is marked as "snapshot", "rc" or "beta") please (i.e. it is a request, not a legal requirement) repack it to another package using JarJar or similar software. See JarJar homepage, JarJar: solves class loading conflicts
Version 1.1 last modified by Lilianne Blaze on 29/06/2008 at 21:15

Comments: 1
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/" From sources :) Why user is able to set server description string ? I can set "I'n not a java server" :D or something :D Furby