package org.seasar.framework.container.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import org.seasar.framework.util.StringUtil;

public class S2ContainerServlet extends HttpServlet {

	public static final String CONFIG_PATH_KEY = "configPath";

	public void init() {
		String configPath = null;
		ServletConfig servletConfig = getServletConfig();
		if (servletConfig != null) {
			configPath = servletConfig.getInitParameter(CONFIG_PATH_KEY); 
		}
		if (!StringUtil.isEmpty(configPath)) {
			SingletonS2ContainerFactory.setConfigPath(configPath);
		}
		SingletonS2ContainerFactory.init();
	}

	public void destroy() {
		SingletonS2ContainerFactory.destroy();
	}

	public static S2Container getContainer() {
		return SingletonS2ContainerFactory.getContainer();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {

		response.getWriter().write("S2ContainerServlet is running.");
	}
}