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

📄 interceptorservice.java

📁 java servlet著名论坛源代码
💻 JAVA
字号:
/*
 * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/interceptor/InterceptorService.java,v 1.3 2004/03/16 12:58:46 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.3 $
 * $Date: 2004/03/16 12:58:46 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2004 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding MyVietnam and MyVietnam CoreLib
 * MUST remain intact in the scripts and source code.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 */
package net.myvietnam.mvncore.interceptor;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.myvietnam.mvncore.configuration.DOM4JConfiguration;
import net.myvietnam.mvncore.exception.InterceptorException;
import net.myvietnam.mvncore.util.FileUtil;

public class InterceptorService {

    private static Log log = LogFactory.getLog(InterceptorService.class);

    private static final String OPTION_FILE_NAME = "mvncore.xml";

    /** Singleton instance of this class */
    private static InterceptorService instance = new InterceptorService();

    private MailInterceptor mailInterceptor = null;

    private ContentInterceptor contentInterceptor = null;

    private LoginIDInterceptor loginIDInterceptor = null;

    /**
     * Private constructor to prevent instantiation
     */
    private InterceptorService() {
        loadInterceptorInfo();
    }

    private void loadInterceptorInfo() {
        try {
            String strPathName = FileUtil.getServletClassesPath();
            String configFilename = strPathName + OPTION_FILE_NAME;
            DOM4JConfiguration conf = new DOM4JConfiguration(new File(configFilename));

            String mailInterceptorClassName = conf.getString("interceptor.mailinterceptor_implementation", "");
            loadMailInterceptor(mailInterceptorClassName);

            String contentInterceptorClassName = conf.getString("interceptor.contentinterceptor_implementation", "");
            loadContentInterceptor(contentInterceptorClassName);

            String loginIDInterceptorClassName = conf.getString("interceptor.loginidinterceptor_implementation", "");
            loadLoginIDInterceptor(loginIDInterceptorClassName);
        } catch (Exception e) {
            log.error("Error loading the interceptor properties", e);
        }

    }

    private void loadContentInterceptor(String contentInterceptorClassName) {

        try {
            if (contentInterceptorClassName.length() > 0) {
                Class contentInterceptorClass = Class.forName(contentInterceptorClassName);
                ContentInterceptor contentInterceptor = (ContentInterceptor)contentInterceptorClass.newInstance();
                setContentInterceptor(contentInterceptor);
            }
        } catch (Exception ex) {
            log.error("Cannot load ContentInterceptor", ex);
        }
    }

    private void loadMailInterceptor(String mailInterceptorClassName) {

        try {
            if (mailInterceptorClassName.length() > 0) {
                Class mailInterceptorClass = Class.forName(mailInterceptorClassName);
                MailInterceptor mailInterceptor = (MailInterceptor)mailInterceptorClass.newInstance();
                setMailInterceptor(mailInterceptor);
            }
        } catch (Exception ex) {
            log.error("Cannot load MailInterceptor", ex);
        }
    }

    private void loadLoginIDInterceptor(String loginIDInterceptorClassName) {

        try {
            if (loginIDInterceptorClassName.length() > 0) {
                Class loginIDInterceptorClass = Class.forName(loginIDInterceptorClassName);
                LoginIDInterceptor loginIDInterceptor = (LoginIDInterceptor)loginIDInterceptorClass.newInstance();
                setLoginIDInterceptor(loginIDInterceptor);
            }
        } catch (Exception ex) {
            log.error("Cannot load LoginIDInterceptor", ex);
        }
    }

    /**
     * Return singleton instance of this class
     *
     * @return InterceptorService the singleton instance of this class
     */
    public static InterceptorService getInstance() {
        return instance;
    }

    /**
     * Validate email if the MailInterceptor is present
     *
     * @param email String email to be validated
     * @throws InterceptorException if email is not valid for some reason
     */
    public void validateMail(String email) throws InterceptorException {
        if (mailInterceptor != null) {
            mailInterceptor.validateEmail(email);
        }
    }

    public MailInterceptor getMailInterceptor() {
        return mailInterceptor;
    }

    public void setMailInterceptor(MailInterceptor interceptor) {
        log.info("Use MailInterceptor = " + interceptor);
        this.mailInterceptor = interceptor;
    }

    /**
     * Validate content if the ContentInterceptor is present
     *
     * @param content String content to be validated
     * @throws InterceptorException if content is not valid for some reason
     */
    public void validateContent(String content) throws InterceptorException {
        if (contentInterceptor != null) {
            contentInterceptor.validateContent(content);
        }
    }

    public ContentInterceptor getContentInterceptor() {
        return contentInterceptor;
    }

    public void setContentInterceptor(ContentInterceptor interceptor) {
        log.info("Use ContentInterceptor = " + interceptor);
        this.contentInterceptor = interceptor;
    }

    /**
     * Validate loginID if the LoginIDInterceptor is present
     *
     * @param loginID String loginID to be validated
     * @throws InterceptorException if loginID is not valid for some reason
     */
    public void validateLoginID(String loginID) throws InterceptorException {
        if (loginIDInterceptor != null) {
            loginIDInterceptor.validateLoginID(loginID);
        }
    }

    public LoginIDInterceptor getLoginIDInterceptor() {
        return loginIDInterceptor;
    }

    public void setLoginIDInterceptor(LoginIDInterceptor interceptor) {
        log.info("Use LoginIDInterceptor = " + interceptor);
        this.loginIDInterceptor = interceptor;
    }

}

⌨️ 快捷键说明

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