views:

176

answers:

4

I would like to be able to get a list of all Spring application contexts from the web applications running in my servlet container. This so so that I can re-initialise the contexts without restarting or reloading my container or webapps.

Is this possible? If so, how? Is there anyway to get a list of all the servlet contexts running on a servlet container instance through the Servlet API?

A: 

There used to be and it was called getServlets; however, it is now deprecated with no replacement meaning that having such a method probably made implementing the spec overly burdensome. From the JavaDoc of the spec:

getServlets

public java.util.Enumeration getServlets()

Deprecated. As of Java Servlet API 2.0, with no replacement.

This method was originally defined to return an Enumeration of all the servlets known to this servlet context. In this version, this method always returns an empty enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

Your container may implement a similar method, but it will be non-standard and you'll have to dig into it's documentation or tell us which container you're using.

Aaron Maenpaa
+2  A: 

No - to both questions.

Access to all servlet contexts would be a security problem. You would be able to inspect and/or manipulate other webapps. The only one knowing all servlet contexts is the container itself. But this is internal implementation, there is no api.

Spring context access is similar, but if you mean all spring contexts in one webapp, they will create a hierarchy - those for controllers for example. Implementing an org.springframework.context.ApplicationListener as bean in the root spring context (as initialized by the org.springframework.web.context.ContextLoaderListener configured in the web.xml) can notify you about contexts started, stopped, closed or refreshed.

Arne Burmeister
The manager web application in Tomcat lists and controls other web applications/servlet contexts using the custom Catalina API. I assume other applications could use this API to similarly circumvent security, but I'd like a solution that doesn't use the catalina API.
Ricardo Gladwell
You cannot use the catalina API to implement a webapp to be used by other application servers like websphere, trifork, oracle or jetty.
Arne Burmeister
+2  A: 

[Edit: This doesn't really help, as each web.xml would have to have the same listener]

You could have a ServletContextAttributeListener listen out for insertions of the standard WebApplicationContext attribute, and then notify some centralised monitor?

Not tested:

public class SpringWACListener implements ServletContextAttributeListener {
    public void attributeAdded(ServletContextAttributeEvent scab) {
        if (WebApplicationContext.
            ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.
            equals(scab.getName()) {
            myCentralisedMonitor.notify("added");
        }
    }
    // same for others
}
toolkit
Thanks for the tip, it should be possible to include the same listener in all my webapps.
Ricardo Gladwell
+1  A: 

You could try the Spring Application Management Suite

http://www.springsource.com/products/ams

not sure what the costs involved are though, can't imagine it being cheap.

mlo55