I've got a library that I use all over the place. Sometimes in web apps (tomcat, glassfish, etc.) and sometimes stand alone.
When running under a container, I want to let the container handle the database connection pool. When running standalone, I have to provide the functionality in my
So I need to be able to tell if a servlet container is running this code.
I'd like a nice, clean way to do this.
I can think of some ugly ones, like iterating through all of the classes loaded by the ClassLoader, and using reflection to see if any "marker" classes are loaded.
I can probably think of other ugly ones as well. But clean would be better.
Examining the contents of new Exception().getStackTrace() amounts to the same as your ClassLoader idea, but with less coding effort on your part. The ugliness of that approach is in the eye of the beholder...