Search in sources :

Example 1 with UrlMapping

use of org.nutz.mvc.UrlMapping in project nutz by nutzam.

the class NutLoading method evalUrlMapping.

protected UrlMapping evalUrlMapping(NutConfig config, Class<?> mainModule, Ioc ioc) throws Exception {
    /*
         * @ TODO 个人建议可以将这个方法所涉及的内容转换到Loadings类或相应的组装类中,
         * 以便将本类加以隔离,使本的职责仅限于MVC整体的初使化,而不再负责UrlMapping的加载
         */
    /*
         * 准备 UrlMapping
         */
    UrlMapping mapping = createUrlMapping(config);
    if (log.isInfoEnabled())
        log.infof("Build URL mapping by %s ...", mapping.getClass().getName());
    /*
         * 创建视图工厂
         */
    ViewMaker[] makers = createViewMakers(mainModule, ioc);
    /*
         * 创建动作链工厂
         */
    ActionChainMaker maker = createChainMaker(config, mainModule);
    /*
         * 创建主模块的配置信息
         */
    ActionInfo mainInfo = Loadings.createInfo(mainModule);
    /*
         * 准备要加载的模块列表
         */
    // TODO 为什么用Set呢? 用List不是更快吗?
    Set<Class<?>> modules = Loadings.scanModules(ioc, mainModule);
    if (modules.isEmpty()) {
        if (log.isWarnEnabled())
            log.warn("None module classes found!!!");
    }
    int atMethods = 0;
    /*
         * 分析所有的子模块
         */
    for (Class<?> module : modules) {
        ActionInfo moduleInfo = Loadings.createInfo(module).mergeWith(mainInfo);
        for (Method method : module.getMethods()) {
            if (!Modifier.isPublic(method.getModifiers()) || method.isBridge())
                continue;
            if (Mirror.getAnnotationDeep(method, At.class) == null)
                continue;
            // 增加到映射中
            ActionInfo info = Loadings.createInfo(method).mergeWith(moduleInfo);
            info.setViewMakers(makers);
            mapping.add(maker, info, config);
            atMethods++;
        }
        // 记录pathMap
        if (null != moduleInfo.getPathMap()) {
            for (Entry<String, String> en : moduleInfo.getPathMap().entrySet()) {
                config.getAtMap().add(en.getKey(), en.getValue());
            }
        }
    }
    if (atMethods == 0) {
        if (log.isWarnEnabled())
            log.warn("None @At found in any modules class!!");
    } else {
        log.infof("Found %d module methods", atMethods);
    }
    config.setUrlMapping(mapping);
    config.setActionChainMaker(maker);
    config.setViewMakers(makers);
    return mapping;
}
Also used : UrlMapping(org.nutz.mvc.UrlMapping) ActionChainMaker(org.nutz.mvc.ActionChainMaker) ActionInfo(org.nutz.mvc.ActionInfo) Method(java.lang.reflect.Method) At(org.nutz.mvc.annotation.At) DefaultViewMaker(org.nutz.mvc.view.DefaultViewMaker) ViewMaker(org.nutz.mvc.ViewMaker)

Example 2 with UrlMapping

use of org.nutz.mvc.UrlMapping in project nutz by nutzam.

the class NutLoading method load.

public UrlMapping load(NutConfig config) {
    if (log.isInfoEnabled()) {
        log.infof("Nutz Version : %s ", Nutz.version());
        log.infof("Nutz.Mvc[%s] is initializing ...", config.getAppName());
    }
    if (log.isDebugEnabled()) {
        Properties sys = System.getProperties();
        log.debug("Web Container Information:");
        log.debugf(" - Default Charset : %s", Encoding.defaultEncoding());
        log.debugf(" - Current . path  : %s", new File(".").getAbsolutePath());
        log.debugf(" - Java Version    : %s", sys.get("java.version"));
        log.debugf(" - File separator  : %s", sys.get("file.separator"));
        log.debugf(" - Timezone        : %s", sys.get("user.timezone"));
        log.debugf(" - OS              : %s %s", sys.get("os.name"), sys.get("os.arch"));
        log.debugf(" - ServerInfo      : %s", config.getServletContext().getServerInfo());
        log.debugf(" - Servlet API     : %d.%d", config.getServletContext().getMajorVersion(), config.getServletContext().getMinorVersion());
        if (config.getServletContext().getMajorVersion() > 2 || config.getServletContext().getMinorVersion() > 4)
            log.debugf(" - ContextPath     : %s", config.getServletContext().getContextPath());
        log.debugf(" - context.tempdir : %s", config.getAttribute("javax.servlet.context.tempdir"));
        log.debugf(" - MainModule      : %s", config.getMainModule().getName());
    }
    /*
         * 准备返回值
         */
    UrlMapping mapping;
    Ioc ioc = null;
    /*
         * 准备计时
         */
    Stopwatch sw = Stopwatch.begin();
    try {
        /*
             * 检查主模块,调用本函数前,已经确保过有声明 MainModule 了
             */
        Class<?> mainModule = config.getMainModule();
        /*
             * 创建上下文
             */
        createContext(config);
        /*
             * 检查 Ioc 容器并创建和保存它
             */
        ioc = createIoc(config, mainModule);
        /*
             * 组装UrlMapping
             */
        mapping = evalUrlMapping(config, mainModule, ioc);
        /*
             * 分析本地化字符串
             */
        evalLocalization(config, mainModule);
        // 初始化SessionProvider
        createSessionProvider(config, mainModule);
        /*
             * 执行用户自定义 Setup
             */
        evalSetup(config, mainModule);
    } catch (Exception e) {
        if (log.isErrorEnabled())
            log.error("Error happend during start serivce!", e);
        if (ioc != null) {
            log.error("try to depose ioc");
            try {
                ioc.depose();
            } catch (Throwable e2) {
                log.error("error when depose ioc", e);
            }
        }
        throw Lang.wrapThrow(e, LoadingException.class);
    }
    // ~ Done ^_^
    sw.stop();
    if (log.isInfoEnabled())
        log.infof("Nutz.Mvc[%s] is up in %sms", config.getAppName(), sw.getDuration());
    return mapping;
}
Also used : UrlMapping(org.nutz.mvc.UrlMapping) Stopwatch(org.nutz.lang.Stopwatch) LoadingException(org.nutz.mvc.LoadingException) Properties(java.util.Properties) Ioc(org.nutz.ioc.Ioc) File(java.io.File) LoadingException(org.nutz.mvc.LoadingException)

Aggregations

UrlMapping (org.nutz.mvc.UrlMapping)2 File (java.io.File)1 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1 Ioc (org.nutz.ioc.Ioc)1 Stopwatch (org.nutz.lang.Stopwatch)1 ActionChainMaker (org.nutz.mvc.ActionChainMaker)1 ActionInfo (org.nutz.mvc.ActionInfo)1 LoadingException (org.nutz.mvc.LoadingException)1 ViewMaker (org.nutz.mvc.ViewMaker)1 At (org.nutz.mvc.annotation.At)1 DefaultViewMaker (org.nutz.mvc.view.DefaultViewMaker)1