⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
// Copyright 2005-2007 onetsoft.com
//
// 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.onetsoft.fastjsp;

import com.onetsoft.fastjsp.request.multipart.UploadConfig;
import com.onetsoft.fastjsp.util.Constants;
import com.onetsoft.fastjsp.util.ResourceResolver;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.valid.ValidationMessageProvider;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

/**
 * FastJsp 模块
 * 模块保存于{@link ServletContext}
 *
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 * @see AbstractServicer
 * @see FastJspServlet
 * @since 2.0
 */
public class Module {

    private final static String CONFIG_NAME = "config";

    final LayoutManagerFactory layoutManagerFactory = new LayoutManagerFactory();

    /*当前模块名:servlet*/
    String name = null;


    /*缺省访问jsp文件名,e.g:"index"  严重警告:此值必须是 index ,否则会存在首页定位问题。tomcat的缺省就是"index.html"*/
    final String welcomeFileName = "index";

    /*url扩展名,一般为 html */
    String urlExt = null;

    /*Action parameter name,"action" applied if empty or null config*/
    String actionName = null;

    /*页面所在包,如:example.pages*/
    String pagePackageBase;

    /*模块页面模板Context相对位置:如:template/skin1  若为根路径,则为 ""  */
    String pageTemplateBase;

    /*系统缺省Locale*/
    Locale locale;

    /*系统缺省字符集*/
    String encoding;
    Charset charset = null;


    ServicerFactory servicerFactory = null;
    MessagesFactory messagesFactory = null;
    PageServiceFactory pageServiceFactory = null;
    ValidationMessageProvider validationMessageProvider = null;
    LocaleStateManagerFactory localeStateManagerFactory = null;
    ValidationMessageDecoratorFactory validationMessageDecoratorFactory = null;
    UploadConfig uploadConfig = null;
    DataSqueezerFactory dataSqueezerFactory = null;


    String moduleServletPath = null; //模块的servlet path ,"/users" 要么为null,要么是非空值

    /**
     * 构造当前模块
     *
     * @param servletConfig
     */
    public Module(ServletConfig servletConfig) {
        this.name = servletConfig.getServletName();
        init(servletConfig);
    }

    private final static String EXT = ".html";

    void init(ServletConfig servletConfig) {

        String cl = StringUtils.trimToEmpty(servletConfig.getInitParameter(CONFIG_NAME));
        if (cl.length() == 0)
            throw new ApplicationRuntimeException("No FastJsp configuration class found in web.xml!");

        Configuration config = null;
        try {
            config = (Configuration) ResourceResolver.forName(cl).getConstructor(new Class[]{String.class}).newInstance(new Object[]{name});
        } catch (Exception e) {
            throw new ApplicationRuntimeException("Fail to initialize FastJsp Configuration class \"" + name + "\".", e);
        }

        parseModuleServletPath(servletConfig);

        String ext = config.getUrlExt();
        if (ext != null && ext.trim().length() != 0 && ext.indexOf('.') != -1)
            urlExt = ext.trim();
        else
            urlExt = EXT;

        actionName = config.getActionName().trim();
        if (actionName.length() == 0)
            actionName = "action";
        pagePackageBase = config.getPagePackageBase();
        locale = config.getLocale();
        pageTemplateBase = config.getPageTemplateBase();
        if (pageTemplateBase.equals(StringUtils.SPLASH))
            pageTemplateBase = StringUtils.EMPTY;
        if (pageTemplateBase.endsWith(StringUtils.SPLASH))
            pageTemplateBase = pageTemplateBase.substring(0, pageTemplateBase.length() - 1);
        if (pageTemplateBase.startsWith(StringUtils.SPLASH))
            pageTemplateBase = pageTemplateBase.substring(1, pageTemplateBase.length());

        encoding = Constants.UTF_8;
        try {
            charset = Charset.forName(config.getEncoding());
            encoding = charset.name();
        } catch (Exception e) {
            throw new ApplicationRuntimeException(e);
        }

        servicerFactory = config.getServicerFactory();
        messagesFactory = config.getMessagesFactory();
        pageServiceFactory = config.getPageServiceFactory();
        validationMessageProvider = config.getValidationMessageProvider();
        localeStateManagerFactory = config.getLocaleStateManagerFactory();
        validationMessageDecoratorFactory = config.getValidationMessageDecoratorFactory();
        uploadConfig = config.getUploadConfig();
        dataSqueezerFactory = config.getDataSqueezerFactory();
    }

    private void parseModuleServletPath(ServletConfig servletConfig) {
        String s = StringUtils.EMPTY;
        try {
            SAXReader sax = new SAXReader();
            sax.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource() {
                        public InputStream getByteStream() {
                            return new InputStream() {
                                public int read() throws IOException {
                                    return -1;
                                }
                            };
                        }
                    };
                }
            });
            Document doc = sax.read(new File(servletConfig.getServletContext().getRealPath("/"), "WEB-INF/web.xml"));
            for (Iterator iter = doc.getRootElement().elementIterator("servlet-mapping"); iter.hasNext();) {
                Element el = (Element) iter.next();
                if (el.element("servlet-name").getTextTrim().equals(servletConfig.getServletName())) {
                    s = el.element("url-pattern").getTextTrim();
                    break;
                }
            }
            doc.clearContent();
        } catch (Exception e) {
            throw new ApplicationRuntimeException(e);
        }
        if (s.length() > 0 && !s.equals(StringUtils.SPLASH)) {
            StringBuffer buf = new StringBuffer(s.length());
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '/' && i > 0) break;
                buf.append(s.charAt(i));
            }
            s = buf.toString();
        }
        if (s.length() > 1)
            moduleServletPath = s;
    }


    /**
     * 当前模块名称
     *
     * @return
     */
    public String getName() {
        return name;
    }


    /**
     * 模式locale
     *
     * @return
     */
    public Locale getLocale() {
        return locale;
    }

    /**
     * 默认字符集
     *
     * @return
     */
    public String getEncoding() {
        return encoding;
    }

    /**
     * 页面模板位置
     * 此位置相对于Web Context Path, 如:""(表示根路径)   ,  "/templates"
     *
     * @return
     */
    public String getPageTemplateBase() {
        return pageTemplateBase;
    }

    /**
     * 取得18n资源
     *
     * @return
     */
    public MessagesFactory getMessagesFactory() {
        return messagesFactory;
    }

    /**
     * 取得自定义page service factory
     *
     * @return
     */
    public PageServiceFactory getPageServiceFactory() {
        return pageServiceFactory;
    }

    /**
     * 取得验证消息提供类
     *
     * @return
     */

    public ValidationMessageProvider getValidationMessageProvider() {
        return validationMessageProvider;
    }

    /**
     * 取得locale状态管理
     *
     * @return
     */
    public LocaleStateManagerFactory getLocaleStateManagerFactory() {
        return localeStateManagerFactory;
    }

    /**
     * 取得验证消息修饰器
     *
     * @return
     */
    public ValidationMessageDecoratorFactory getValidationMessageDecoratorFactory() {
        return validationMessageDecoratorFactory;
    }

    /**
     * 取得上载配置
     *
     * @return
     */
    public UploadConfig getUploadConfig() {
        return uploadConfig;
    }

    /**
     * 取得Page Data中获取/保存数据(类)映射工具
     *
     * @return
     */
    public DataSqueezerFactory getDataSqueezerFactory() {
        return dataSqueezerFactory;
    }


    final class LayoutManagerFactory {
        private Map pool = new HashMap(1);      //key:layout , value:LayoutManager

        final LayoutManager getLayoutManager(PageParamsImpl pageParams) {
            Object o = pool.get(pageParams.layoutId);
            if (o == null) {
                synchronized (pool) {
                    o = pool.get(pageParams.layoutId);
                    if (o == null) {
                        o = new LayoutManager();
                        pool.put(pageParams.layoutId, o);
                    }
                }
            }
            return (LayoutManager) o;
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -