I have a simple problem: I want to configure an object differently based on whether the object is created in the servlet container or whether it is created in a stand-alone application.
The object is a database connection, and I take care of setting the request timeout.
The first solution I can come up with is:
if (insideServletContainer(this.getClass().getClassLoader()) { /// do some servlet specific config } else { /// do some standalone config }
The question, of course, is that I can write a reliable method for determining whether a class has been loaded into a servlet container. At best, it looks like a hack.
The second option is to assume that the default case is an offline instance creation, sets default values based on an offline configuration, and overrides them in the servlet context.
So, to summarize my question: Do you know about a good / reliable mechanism if the class was loaded from a servlet container? If not, I will have to go the second route.
Nick
source share