package org.ow2.jonas.web.tomcat6.ws;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.catalina.core.StandardWrapper;
import org.ow2.jonas.ws.jaxws.IWebServiceEndpoint;
import org.ow2.jonas.ws.jaxws.http.servlet.JAXWSServlet;


public class WebServiceEndpointStandardWrapper extends StandardWrapper {

    /**
     * Value for the <code>servlet-name</code> element.
     */
    public static final String JAX_WS_GENERIC_SERVLET = "JAX-WS 2.x Generic Servlet";

    /**
     * Serrial Version UID.
     */
    private static final long serialVersionUID = -8186120043063306566L;

    /**
     * Endpoint to be injected in the servlet instance.
     */
    private IWebServiceEndpoint endpoint;

    public WebServiceEndpointStandardWrapper(final IWebServiceEndpoint endpoint) {
        super();
        this.endpoint = endpoint;

        // Hardcoded JAXWSServlet name ?
        setServletClass(JAXWSServlet.class.getName());
        setServletName(JAX_WS_GENERIC_SERVLET);
    }

    /* (non-Javadoc)
     * @see org.apache.catalina.core.StandardWrapper#loadServlet()
     */
    @Override
    public synchronized Servlet loadServlet() throws ServletException {

        // Delegate Servlet instanciation
        JAXWSServlet servlet = (JAXWSServlet) super.loadServlet();

        // Inject the Endpoint
        servlet.setWebServiceEndpoint(endpoint);

        return servlet;
    }

}
