OFBiz: 초기 RequestHandler

15798 단어 handler
RequestHandler, 요청 프로세서라고 할 수 있으며, ControlServlet에서.init()에서 초기화:
public class ControlServlet extends HttpServlet {
        public void init(ServletConfig config) throws ServletException {
        super.init(config);

        // configure custom BSF engines
        configureBsf();
        
        // initialize the request handler
 getRequestHandler();
    }
    
    protected RequestHandler getRequestHandler() {
        return RequestHandler.getRequestHandler(getServletContext()); }
}

ControlServlet은 RequestHandler에 대한 변수를 정의하지 않고 ServletContext에 배치합니다.서브렛Context 하나에는 RequestHandler 하나만 허용됩니다.
public class RequestHandler {
    public static RequestHandler getRequestHandler(ServletContext servletContext) {
        RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
        if (rh == null) {
            rh = new RequestHandler();
            servletContext.setAttribute("_REQUEST_HANDLER_", rh); rh.init(servletContext);
        }
        return rh;
    }
}

RequestHandler의 구조기는 비어 있으며, 초기화는 init () 에 있습니다.
public class RequestHandler {
    public void init(ServletContext context) {
        this.context = context;

        // init the ControllerConfig, but don't save it anywhere
        //    ControllerConfig,        
        this.controllerConfigURL = ConfigXMLReader.getControllerConfigURL(context);
        ConfigXMLReader.getControllerConfig(this.controllerConfigURL);
        
        this.viewFactory = new ViewFactory(this);
        this.eventFactory = new EventFactory(this);
    }
}

 
RequestHandler에 대응하는 설정 파일은 WEB-INF/controller입니다.xml, ConfigXMLReader 처리.OFBiz 처리 controller.xml이 캐시를 사용해서 초과할 때 자동으로 정리됩니다.따라서 OFBiz 클래스에 Controller Config가 필요할 때 변수를 정의하지 않고 controller Config를 저장합니다.ConfigXMLReader가 URL을 Key 캐시 ControllerConfig로 하기 때문에 xml에 대응하는 URL 변수입니다.RequestHandler의 DoRequest() 가져오기 ControllerConfig는 다음과 같이 처리됩니다.
public class RequestHandler {
    public void doRequest(HttpServletRequest request, HttpServletResponse response, String chain,
            GenericValue userLogin, Delegator delegator) throws RequestHandlerException {
        ... ...

        // get the controllerConfig once for this method so we don't have to get it over and over inside the method
        ConfigXMLReader.ControllerConfig controllerConfig = this.getControllerConfig();
        Map<String, ConfigXMLReader.RequestMap> requestMapMap = controllerConfig.getRequestMapMap();
        
        ... ...
    }
    
    public ConfigXMLReader.ControllerConfig getControllerConfig() {
        return ConfigXMLReader.getControllerConfig(this.controllerConfigURL);
    }
}

RequestHandler에는 ViewFactory 객체와 EventFactory 객체가 포함되어 있습니다.ViewFactory 객체의 초기화는 자체 작성기에 의해 수행됩니다.
public class ViewFactory {
    public ViewFactory(RequestHandler requestHandler) {
        this.handlers = FastMap.newInstance();
        this.requestHandler = requestHandler;
        this.context = requestHandler.getServletContext();

        // pre-load all the view handlers
        try {
            this.preLoadAll();
        } catch (ViewHandlerException e) {
            Debug.logError(e, module);
            throw new GeneralRuntimeException(e);
        }
    }
}

ViewFactory가 시작되었을 때, preLoadAll () 를 호출하여 모든 controller에 불러옵니다.xml에 정의된 ViewHandler입니다.
public class ViewFactory {
    private void preLoadAll() throws ViewHandlerException {
        Set<String> handlers = this.requestHandler.getControllerConfig().getViewHandlerMap().keySet();
        if (handlers != null) {
            for (String type: handlers) {
                this.handlers.put(type, this.loadViewHandler(type));
            }
        }

        // load the "default" type
        if (!this.handlers.containsKey("default")) {
            try {
                ViewHandler h = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
                h.init(context);
                this. handlers.put("default", h);
            } catch (Exception e) {
                throw new ViewHandlerException(e);
            }
        }
    }
}

loadViewHandler () 는 구체적인 ViewHandler 초기화를 실행합니다.
public class ViewFactory {
    private ViewHandler loadViewHandler(String type) throws ViewHandlerException {
        ViewHandler handler = null;
        String handlerClass = this.requestHandler.getControllerConfig().getViewHandlerMap().get(type);
        if (handlerClass == null) {
            throw new ViewHandlerException("Unknown handler type: " + type);
        }

        try {
            handler = (ViewHandler) ObjectType.getInstance(handlerClass); handler.setName(type); handler.init(context);
        } catch (ClassNotFoundException cnf) {
            //throw new ViewHandlerException("Cannot load handler class", cnf);
            Debug.logWarning("Warning: could not load view handler class because it was not found; note that some views may not work: " + cnf.toString(), module);
        } catch (InstantiationException ie) {
            throw new ViewHandlerException("Cannot get instance of the handler", ie);
        } catch (IllegalAccessException iae) {
            throw new ViewHandlerException(iae.getMessage(), iae);
        }

        return handler;
    }
}

ViewHandler 객체가 만들어진 다음 init() 메서드를 실행합니다.구체적인 Velocity ViewHandler의 경우 그 inti() 방법이 Velocity Engine의 초기화를 완성했다.
public class VelocityViewHandler extends AbstractViewHandler {
    public void init(ServletContext context) throws ViewHandlerException {
        try {
            Debug.logInfo("[VelocityViewHandler.init] : Loading...", module);
            ve = new VelocityEngine();
            // set the properties
            // use log4j for logging
            // use classpath template loading (file loading will not work in WAR)
            ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
                "org.apache.velocity.runtime.log.Log4JLogSystem");
            ve.setProperty("runtime.log.logsystem.log4j.category", module);

            Properties props = null;
            try {
                props = UtilProperties.getProperties(context.getResource("/WEB-INF/velocity.properties"));
                Debug.logInfo("[VelocityViewHandler.init] : Loaded /WEB-INF/velocity.properties", module);
            } catch (MalformedURLException e) {
                Debug.logError(e, module);
            }
            if (props == null) {
                props = new Properties();
                Debug.logWarning("[VelocityViewHandler.init] : Cannot load /WEB-INF/velocity.properties. " +
                    "Using default properties.", module);
            }

            // set the file loader path -- used to mount the webapp
            if (context.getRealPath("/") != null) {
                props.setProperty("file.resource.loader.path", context.getRealPath("/"));
                Debug.logInfo("[VelocityViewHandler.init] : Got true webapp path, mounting as template path.", module);
            }

            ve.init(props);
        } catch (Exception e) {
            throw new ViewHandlerException(e.getMessage(), e);
        }
    }
}

좋은 웹페이지 즐겨찾기