RESTServlet.java :  » REST » dyuproject » com » dyuproject » web » rest » Java Open Source

Java Open Source » REST » dyuproject 
dyuproject » com » dyuproject » web » rest » RESTServlet.java
//========================================================================
//Copyright 2007-2008 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.dyuproject.web.rest;

import java.io.IOException;

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

import com.dyuproject.util.ClassLoaderUtil;


/**
 * The REST servlet to handle all the requests dispatched to the webapp.
 * 
 * @author David Yu
 * @created May 10, 2008
 */

public final class RESTServlet extends HttpServlet
{
    
    private static final long serialVersionUID = 2009100680L;
    
    private WebContext _webContext;
    
    public void init() throws ServletException
    {        
        _webContext = (WebContext)getServletContext().getAttribute(WebContext.class.getName());
        if(_webContext==null)
        {
            String webContextClass = getServletConfig().getInitParameter("webContext");            
            if(webContextClass==null)
                throw new ServletException("*webContext* is missing from the servlet context's attributes/init-parameter");
            try
            {
                _webContext = ClassLoaderUtil.newInstance(webContextClass, RESTServlet.class);                
            }
            catch(Exception e)
            {
                throw new ServletException(e);
            }
        }
        try
        {            
            _webContext.preConfigure(getServletConfig());
            _webContext.init(getServletContext());
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }        
    }
    
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        _webContext.service(request, response);
    }
    
    public void destroy()
    {
        if(_webContext!=null)
            _webContext.destroy(getServletContext());
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.