001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.webservices;
018    
019    import java.io.IOException;
020    import javax.servlet.Servlet;
021    import javax.servlet.ServletConfig;
022    import javax.servlet.ServletContext;
023    import javax.servlet.ServletException;
024    import javax.servlet.ServletRequest;
025    import javax.servlet.ServletResponse;
026    import javax.xml.rpc.server.ServiceLifecycle;
027    
028    /**
029     * Delegates requests to a WebServiceContainer which is presumably for a POJO WebService
030     * Nothing stopping us from using this for EJBs or other types of webservices other than
031     * it is more than we need.  EJB webservices use the JettyEJBWebServiceContext.
032     * <p/>
033     * From a 10,000 foot view the Jetty architecture has:
034     * Container -> Context -> Holder -> Servlet
035     * <p/>
036     * A Container has multiple Contexts, typically webapps
037     * A Context provides the JNDI, TX, and Security for the webapp and has many Holders
038     * A Holder simply wraps each Servlet
039     * <p/>
040     * The POJO Web Service architecture on Jetty looks like this:
041     * Container -> WebApp Context -> JettyPOJOWebServiceHolder -> POJOWebServiceServlet
042     * <p/>
043     * The EJB Web Service architecure, on the other hand, creates one Context for each EJB:
044     * Container -> JettyEJBWebServiceContext
045     *
046     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
047     */
048    public class POJOWebServiceServlet implements Servlet {
049        public static final String POJO_CLASS = POJOWebServiceServlet.class.getName()+"@pojoClassName";
050        private Servlet stack;
051    
052        public void init(ServletConfig config) throws ServletException {
053            ServletContext context = config.getServletContext();
054    
055            String pojoClassID = config.getInitParameter(POJO_CLASS);
056            Class pojoClass = (Class) context.getAttribute(pojoClassID);
057    
058            Object pojo;
059            try {
060                pojo = pojoClass.newInstance();
061            } catch (Exception e) {
062                throw new ServletException("Unable to instantiate POJO WebService class: " + pojoClass.getName(), e);
063            }
064    
065            stack = new WebServiceContainerInvoker(pojo);
066            if (pojo instanceof ServiceLifecycle) {
067                stack = new ServiceLifecycleManager(stack,(ServiceLifecycle)pojo);
068            }
069    
070    
071            stack.init(config);
072        }
073    
074        public ServletConfig getServletConfig() {
075            return stack.getServletConfig();
076        }
077    
078        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
079            stack.service(servletRequest, servletResponse);
080        }
081    
082        public String getServletInfo() {
083            return stack.getServletInfo();
084        }
085    
086        public void destroy() {
087            stack.destroy();
088        }
089    }