2

Given a servlet-container (JBoss, Tomcat, Jetty, etc) is it possible to find all servletcontexts (or servletconfigs) currently instantiated by the JVM or more specifically the servlet-container? If so, how?

dsutherland
  • 792
  • 6
  • 15

1 Answers1

0

I'm not sure about other containers, but Tomcat allows you to access a wealth of information via JMX. Attach to jconsole, VisualVM or another JMX browser to a Tomcat process and look under Catalina,type=WebModule and you can see a list of loaded contexts. Each JMX bean you can find in that sub-tree has a wealth of information about the state of the server.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Thanks but I was aware of that and I'm trying to find an implementation agnostic approach, not to mention the JMX console does not meet all the requirements of what I need. Really what would be great is if there was a way to lookup all instances of a given interface loaded by the JVM currently. – dsutherland Jun 06 '13 at 14:40
  • You will not find an implementation-agnostic approach, because the servlet specification does not have anything like this defined. – Christopher Schultz Jun 06 '13 at 14:49
  • If you were desperate, you could write some code to interact with the profiling interface to the JVM (not sure what the current state-of-the-art is, but t used to be JVMPI and friends) and simply inspect every class or object in the JVM. Collect objects that implement `ServletContext` and you are done. Mostly. There may be objects whose classes implement that interface but are dummy objects, or previously-terminated webapps that haven't been GC'd yet, etc. You will also have to scan periodically, since things can change at any moment. – Christopher Schultz Jun 06 '13 at 14:51
  • I actually might have found a way to do this using a reflection library called org.reflections. [link](https://code.google.com/p/reflections/) I will post on how to use it if this works. – dsutherland Jun 06 '13 at 15:03
  • Sadly, the reflections library only gives a reference to the classes used and not the actually class instances themselves. – dsutherland Jun 06 '13 at 18:37