All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blade.web.DispatcherServlet Maven / Gradle / Ivy

/**
 * Copyright (c) 2015, biezhi 王爵 ([email protected])
 *
 * 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.blade.web;

import java.io.IOException;

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

import blade.kit.StringKit;
import blade.kit.log.Logger;

import com.blade.Aop;
import com.blade.Blade;
import com.blade.Bootstrap;
import com.blade.route.RouteBuilder;
import com.blade.route.RouteMatcher;

/**
 * Blade Core DispatcherServlet
 * 
 * @author	biezhi
 * @since	1.0
 */
public class DispatcherServlet extends HttpServlet {
	
	private static final long serialVersionUID = -2607425162473178733L;
	
	private static final Logger LOGGER = Logger.getLogger(DispatcherServlet.class);
	
	private Blade blade = Blade.me();
	
	private Bootstrap bootstrap; 
	
	private ServletContext servletContext;
	
	private SyncRequestHandler syncRequestHandler;
	
	public DispatcherServlet() {
	}
	
	public DispatcherServlet(Bootstrap bootstrap) {
		this.bootstrap = bootstrap;
		blade.setInit(true);
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		servletContext = config.getServletContext();
		if(!blade.isInit()){
		    blade.webRoot(servletContext.getRealPath("/"));
			this.bootstrap = blade.bootstrap();
			if(null == bootstrap){
				String bootStrapClassName = config.getInitParameter("bootstrap");
				if(StringKit.isNotBlank(bootStrapClassName)){
					bootstrap = getBootstrap(bootStrapClassName);
				} else {
					bootstrap = new Bootstrap() {
						@Override
						public void init(Blade blade) {
						}
					}; 
				}
				blade.app(bootstrap);
			}
			blade.bootstrap().init(blade);
			
		    // buiding route
			new RouteBuilder(blade).building();
			
			// initialization ioc
			blade.iocInit();
			
		    blade.bootstrap().contextInitialized(blade);
		    
		    syncRequestHandler = new SyncRequestHandler(servletContext, blade.routers());
		    AsynRequestHandler.routeMatcher = new RouteMatcher(blade.routers());
		    
		    blade.setInit(true);
		    LOGGER.info("blade init complete!");
		}
	}

	@Override
	protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException {
		httpRequest.setCharacterEncoding(blade.encoding());
		httpResponse.setCharacterEncoding(blade.encoding());
		
		boolean isAsync = httpRequest.isAsyncSupported();
		if (isAsync) {
			AsyncContext asyncCtx = httpRequest.startAsync();
			asyncCtx.addListener(new AppAsyncListener());
			asyncCtx.setTimeout(10000L);
			asyncCtx.start(new AsynRequestHandler(servletContext, asyncCtx));
		} else {
			syncRequestHandler.handle(httpRequest, httpResponse);
		}
	}
	
	/**
     * Get global initialization object, the application of the initialization
     * 
     * @param botstrapClassName 	botstrap class name
     * @return 						return bootstrap object
     * @throws ServletException
     */
    @SuppressWarnings("unchecked")
	private Bootstrap getBootstrap(String botstrapClassName) throws ServletException {
    	Bootstrap bootstrapClass = null;
        try {
        	if(null != botstrapClassName){
            	Class applicationClass = (Class) Class.forName(botstrapClassName);
                if(null != applicationClass){
                	bootstrapClass = Aop.createT(applicationClass);
                }
        	} else {
        		throw new ServletException("bootstrapClass is null !");
			}
        } catch (Exception e) {
            throw new ServletException(e);
        }
		return bootstrapClass;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy