package com.wuyiccc.helloframework.mvc; import com.wuyiccc.helloframework.aop.AspectWeaver; import com.wuyiccc.helloframework.core.BeanContainer; import com.wuyiccc.helloframework.injection.DependencyInjector; import com.wuyiccc.helloframework.mvc.processor.RequestProcessor; import com.wuyiccc.helloframework.mvc.processor.impl.ControllerRequestProcessor; import com.wuyiccc.helloframework.mvc.processor.impl.JspRequestProcessor; import com.wuyiccc.helloframework.mvc.processor.impl.PreRequestProcessor; import com.wuyiccc.helloframework.mvc.processor.impl.StaticResourceRequestProcessor; import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Properties; /** * @author wuyiccc * @date 2020/7/14 21:42 * 岂曰无衣,与子同袍~ */ @WebServlet("/*") @Slf4j public class DispatcherServlet extends HttpServlet { private static final String HELLOFRAMEWORK_CONFIG_FILE = "config/helloframework-config.properties"; private List<RequestProcessor> PROCESSOR = new ArrayList<>(); @Override public void init() throws ServletException { // 1. 初始化容器 BeanContainer beanContainer = BeanContainer.getInstance(); beanContainer.loadBeans(getHelloFrameworkScanPackages()); new AspectWeaver().doAop(); new DependencyInjector().doIoc(); // 2. 初始化请求处理器责任链 PROCESSOR.add(new PreRequestProcessor()); PROCESSOR.add(new StaticResourceRequestProcessor(getServletContext())); PROCESSOR.add(new JspRequestProcessor(getServletContext())); PROCESSOR.add(new ControllerRequestProcessor()); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1. 创建责任链对象实例 RequestProcessorChain requestProcessorChain = new RequestProcessorChain(PROCESSOR.iterator(), req, resp); // 2. 通过责任链模式来依次调用请求处理器对请求进行处理 requestProcessorChain.doRequestProcessorChain(); // 3. 对处理结果进行渲染 requestProcessorChain.doRender(); } private String getHelloFrameworkScanPackages() { Properties properties = new Properties(); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(HELLOFRAMEWORK_CONFIG_FILE); try { properties.load(in); } catch (IOException e) { log.warn("The config/helloframework.properties can not load"); e.printStackTrace(); } String scanPackages = properties.getProperty("helloframework.scan.packages"); log.info("this is scanPackages: {}", scanPackages); return scanPackages; } }
package org.myframework.mvc; /** * @author wuyiccc * @date 2020/6/16 18:48 * 岂曰无衣,与子同袍~ */ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.myframework.mvc.processor.RequestProcessor; import org.myframework.mvc.render.DefaultResultRender; import org.myframework.mvc.render.InternalErrorResultRender; import org.myframework.mvc.render.ResultRender; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Iterator; /** * 1. 以责任链的模式执行注册的请求处理器 * 2. 委派给特定的Render实例对处理后的结果进行渲染 */ @Data @Slf4j public class RequestProcessorChain { // 请求处理器迭代器 private Iterator<RequestProcessor> requestProcessorIterator; private HttpServletRequest request; private HttpServletResponse response; // http请求方法 private String requestMethod; // http请求路径 private String requestPath; // http响应状态码 private int responseCode; // 请求结果渲染器 private ResultRender resultRender; public RequestProcessorChain(Iterator<RequestProcessor> requestProcessorIterator, HttpServletRequest request, HttpServletResponse response) { this.requestProcessorIterator = requestProcessorIterator; this.request = request; this.response = response; this.requestMethod = request.getMethod(); this.requestPath = request.getPathInfo(); this.responseCode = HttpServletResponse.SC_OK; } /** * 以责任链的模式执行请求链 */ public void doRequestProcessorChain() { // 1. 通过迭代器遍历注册的请求处理器实现类列表 try { while (requestProcessorIterator.hasNext()) { // 2. 知道某个请求处理器执行后返回为false为止 if (!requestProcessorIterator.next().process(this)) { break; } } } catch (Exception e) { // 3. 期间如果出现异常,则交给内部异常渲染器处理 this.resultRender = new InternalErrorResultRender(e.getMessage()); log.error("doRequestProcessorChain error: ", e); } } /** * 执行处理器 */ public void doRender() { // 1. 如果请求处理器实现类均未选择合适的渲染器,则使用默认的 if (this.resultRender == null) { this.resultRender = new DefaultResultRender(); } // 2. 调用渲染器的render方法对结果进行渲染 try { this.resultRender.render(this); } catch (Exception e) { log.error("doRender error: ", e); throw new RuntimeException(e); } } }
github地址: https://github.com/wuyiccc/he...