OFBiz:初期RequestHandler

15791 ワード

RequestHandlerは、ControlServiceletで要求プロセッサと呼ぶことができる.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()); }
}

ControlServiceletは、RequestHandlerの変数を定義するのではなく、ServiceletContextに配置します.1つのサーブレットContextにはRequestHandlerが1つしか許可されていません.
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は、ConfigXML Readerを介して処理されます.OFBiz処理コントローラxmlはキャッシュを使用しており、タイムアウト時に自動的にクリーンアップされます.したがって、OFBizクラスでControllerConfigが必要な場合、変数を定義してControllerConfigを保存するのではなく、controlllerを定義するだけである.ConfigXML ReaderはURLをキーとして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()メソッドが実行されます.具体的なVelocityViewHandlerを例にとると,そのinti()メソッドはVelocityEngineの初期化を完了する.
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);
        }
    }
}