Is it possible to dynamically configure Tomcat?
Created May 4, 2012
For example, this is the source I used to obtain all the current active web applications running in Tomcat 3.2.1.
I hope this is useful for you.
import java.util.Enumeration;
import java.util.ArrayList;
import javax.servlet.http.*;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.FacadeManager;
import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.ContextManager;
import org.apache.tomcat.util.RequestUtil;
public class ContextTree {
private ContextManager cm;
public ContextTree(HttpServletRequest request) {
FacadeManager facadeM = (FacadeManager)request.getAttribute( FacadeManager.FACADE_ATTRIBUTE);
if (facadeM == null) {
cm = null;
return;
}
this.cm = facadeM.getRealRequest(request).getContext().getContextManager();
}
public WebLink[] getContextNames() {
ArrayList list = new ArrayList();
Enumeration e = cm.getContexts();
while(e.hasMoreElements()) {
Context c = (Context)e.nextElement();
if(c.getDocBase().indexOf("ROOT") == -1) {
WebLink wl = new WebLink(c.getDescription(), c.getDocBase());
list.add(wl);
}
}
WebLink[] temp = new WebLink[list.size()];
list.toArray(temp);
return temp;
}
}
[Please see the original forum thread for some advice and warnings about this approach. -Alex]