목차

Tomcat Context Reloader

Tomcat의 컨텍스트를 reloadable=“false”인 상태에서, Tomcat Manager를 설치하지 않은 상태에서 수동 Reload 할 수 있는 Valve를 만들어 보았다. Reload Tomcat Context manually(without manager or reloadable=“true” option).

설정

실행

소스

package kr.pe.kwonnam.tomcat.reloader;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
 
/**
 * Reload Tomcat Context by requesting URL
 *
 * Context의 reloadable="false"인 상태에서도 /reloadContext URL을 호출하면 해당 컨텍스트가 Reloading 된다.
 *
 * @author 손권남 kwon37xi@gmail.com
 *
 */
public class TomcatReloadValve extends ValveBase {
 
	private static final String RELOAD_CONTEXT_URI = "/reloadContext";
 
	@Override
	public void invoke(Request request, Response response) throws IOException,
			ServletException {
 
		Container container = getContainer();
 
		String requestUri = request.getRequestURI();
 
		String reloadUri = request.getContextPath() + RELOAD_CONTEXT_URI;
 
		if (requestUri.startsWith(reloadUri)
				&& container instanceof Context) {
			reloadContext(response, container);
			return;
		}
 
		getNext().invoke(request, response);
	}
 
	private void reloadContext(Response response, Container container)
			throws IOException {
		((Context) container).reload();
		HttpServletResponse httpResponse = response.getResponse();
 
		httpResponse.setContentType("text/plain;charset=utf-8");
		httpResponse.getWriter().write("Context Reloaded!!");
		httpResponse.getWriter().close();
 
		return;
	}
 
}