systemcontext.java

来自「一个自己做的公司网站和办公职员管理系统。」· Java 代码 · 共 326 行

JAVA
326
字号
package ws.woa.core;

import java.sql.Connection;
import java.sql.SQLException;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ws.woa.logger.LogManager;
import ws.woa.logger.LogWriter;
import ws.woa.util.MailSender;

/**
 * 僔僗僥儉偺忣曬傪曐帩偟丄儌僕儏乕儖偵採嫙偡傞偨傔偺僆僽僕僃僋僩偱偡丅
 *
 * @author Wang
 */
public class SystemContext {

    private String systemName;
    private String systemVersion;

    private ServletContext     servletCtx;
    private Request            request;
    private Response           response;
    private SystemConfig       config;
    private ModulePluginManager      modulePluginMgr;
    private DataSourceManager  dsMgr;
    private LogManager         logMgr;

    private String mgrDS;
    private String mgrLog;

    /**
     * 僐儞僗僩儔僋僞
     *
     * @param servletContext
     * @param request
     * @param response
     * @param pluginMgr
     */
    public SystemContext(Request request,Response response) throws Exception {

        this.systemName    = Constants.SYS_NAME;
        this.systemVersion = Constants.SYS_VERSION;

        this.mgrDS  = Constants.MGR_DS;
        this.mgrLog = Constants.MGR_LOG;

        this.servletCtx = ControllerServlet._context;
        this.request    = request;
        this.response   = response;
        this.config     = SystemConfig.getInstance();
        this.modulePluginMgr  = ModulePluginManager.getInstance();
        this.dsMgr      = DataSourceManager.getInstance();
        this.logMgr     = LogManager.getInstance();
    }

    /**
     * 僔僗僥儉柤徧傪庢摼偟傑偡丅
     *
     * @return 僔僗僥儉柤徧
     */
    public String getSystemName() {
        return systemName;
    }

    /**
     * 僔僗僥儉偺僶乕僕儑儞傪庢摼偟傑偡丅
     *
     * @return 僔僗僥儉偺僶乕僕儑儞
     */
    public String getSystemVersion() {
        return systemVersion;
    }

    /**
     * ModuleInfo傪庢摼偟傑偡丅
     *
     * @param name 儌僕儏乕儖柤
     * @return ModuleInfo
     */
    public ModuleInfo getModuleInfo(String name){
        return modulePluginMgr.getModuleInfo(name);
    }

    /**
     * 慡偰偺ModuleInfo傪攝楍偱庢摼偟傑偡丅
     *
     * @return ModuleInfo[]
     */
    public ModuleInfo[] getModuleInfoArray(){
        return modulePluginMgr.getModuleInfoArray();
    }

    /**
     * ServletContext傪庢摼偟傑偡丅
     *
     * @return ServletContext
     */
    public ServletContext getServletContext(){
        return this.servletCtx;
    }

    /**
     * 僨乕僞僜乕僗偲偟偰搊榐偟偨Connection傪庢摼偟傑偡丅
     *
     * @param name 僨乕僞僜乕僗柤
     * @return Connection
     */
    public Connection getConnection(String name)
            throws Exception, ApplicationException {
        return this.dsMgr.getConnection(name);
    }

    /**
     * 僨乕僞僜乕僗偲偟偰搊榐偟偨Connection傪庢摼偟傑偡丅
     *
     * @return Connection
     */
    public Connection getConnection()
            throws Exception, ApplicationException {
        return this.dsMgr.getConnection(this.mgrDS);
    }

    /**
     * 僨乕僞僜乕僗偲偟偰搊榐偟偨Connection傪庢摼偟傑偡丅
     *
     * Added by Wang for JDBC Pool.
     *
     * @param name 僨乕僞僜乕僗柤
     * @param Connection
     */
    public void releaseConnection(String name, Connection conn) {
        this.dsMgr.releaseConnection(name, conn);
    }

    /**
     * 僨乕僞僜乕僗偲偟偰搊榐偟偨Connection傪庢摼偟傑偡丅
     *
     * Added by Wang for JDBC Pool.
     *
     * @param Connection
     */
    public void releaseConnection(Connection conn) {
        this.dsMgr.releaseConnection(this.mgrDS, conn);
    }

    /**
     * LogWriter傪庢摼偟傑偡丅
     *
     * @param name 儘僌儔僀僞柤
     * @return LogWriter
     */
    public LogWriter getLogger(String name){
        return this.logMgr.getLogWriter(name);
    }

    /**
     * LogWriter傪庢摼偟傑偡丅
     *
     * @return LogWriter
     */
    public LogWriter getLogger(){
        return this.logMgr.getLogWriter(this.mgrLog);
    }

    /**
     * 儐乕僓偑儘僌僀儞拞偐偳偆偐傪挷傋傑偡丅
     *
     * @return true=儘僌僀儞拞丄false=枹儘僌僀儞
     */
    public boolean isLogin(){
        if(getUserInfo()==null){
            return false;
        } else {
            return true;
        }
    }

    /**
     * 儘僌僀儞偟偰偄傞儐乕僓偺忣曬傪庢摼偟傑偡丅
     * 儘僌僀儞拞偱側偄応崌偼null傪曉偟傑偡丅
     */
    public UserInfo getUserInfo(){
        HttpSession session = this.request.getSession();
        return (UserInfo)session.getAttribute(Constants.REQ_ATTR_USERINFO);
    }

    /**
     * 堷悢偱巜掕偟偨傾僇僂儞僩偺儐乕僓忣曬傪庢摼偟傑偡丅
     *
     * @param account 傾僇僂儞僩
     * @return UserInfo
     */
    public UserInfo getUserInfo(String account)
            throws Exception, ApplicationException {
        Connection conn = this.getConnection();
        try {
            return UserDBAccessor.getUserInfo(conn,account);
        } finally {
            //conn.close();
            this.releaseConnection(conn);
        }
    }

    /**
     * 堷悢偱巜掕偟偨儐乕僓ID偺儐乕僓忣曬傪庢摼偟傑偡丅
     *
     * @param userID 儐乕僓ID
     * @return UserInfo
     */
    public UserInfo getUserInfo(long userID)
            throws Exception, ApplicationException {
        Connection conn = this.getConnection();
        try {
            return UserDBAccessor.getUserInfo(conn,userID);
        } finally {
            //conn.close();
            this.releaseConnection(conn);
        }
    }

    /**
     * 慡偰偺儐乕僓忣曬傪庢摼偟傑偡丅
     *
     * @return UserInfo[]
     */
    public UserInfo[] getAllUserInfo() throws Exception, ApplicationException {
        Connection conn = this.getConnection();
        try {
            return UserDBAccessor.getAllUserInfo(conn);
        } finally {
            //conn.close();
            this.releaseConnection(conn);
        }
    }

    /**
     * 堷悢偱巜掕偟偨僌儖乕僾ID偺僌儖乕僾忣曬傪庢摼偟傑偡丅
     *
     * @param groupID 僌儖乕僾ID
     * @return GroupInfo
     */
    public GroupInfo getGroupInfo(long groupID)
            throws Exception, ApplicationException{
        Connection conn = this.getConnection();
        try {
            return GroupDBAccessor.getGroupInfo(conn,groupID);
        } finally {
            //conn.close();
            this.releaseConnection(conn);
        }
    }

    /**
     * 慡偰偺僌儖乕僾忣曬傪庢摼偟傑偡丅
     *
     * @return GroupInfo[]
     */
    public GroupInfo[] getAllGroupInfo()
            throws Exception, ApplicationException {
        Connection conn = this.getConnection();
        try {
            return GroupDBAccessor.getAllGroupInfo(conn);
        } finally {
            //conn.close();
            this.releaseConnection(conn);
        }
    }

    public HttpServletResponse getResponse(){
        return this.response;
    }

    /**
     * 堎側傞儌僕儏乕儖偵儕僟僀儗僋僩偟傑偡丅
     *
     * @param moduleName 儌僕儏乕儖柤 or 儌僕儏乕儖URL
     */
    public void redirect(String moduleName) throws Exception {

        // Begin - Added by Wang on 2004/07/12
        String sModuleName = moduleName;
        if (moduleName.indexOf(".") != -1) {
            sModuleName = moduleName.substring(0, moduleName.indexOf("."));
        }
        // End - - Added by Wang on 2004/07/12

        //ModuleHandler handler = modulePluginMgr.getModuleHandler(moduleName);
        ModuleHandler handler = modulePluginMgr.getModuleHandler(sModuleName);
        handler.init(this);
        handler.doProcess(request,response);
    }

    /**
     * 儊乕儖傪憲怣偟傑偡丅
     *
     * @param from    憲怣幰偺傾僪儗僗
     * @param to      庴怣幰偺傾僪儗僗
     * @param subject 審柤
     * @param content 杮暥
     */
    public void sendMail(String from,String to,String subject,String content)
            throws NoSuchProviderException, MessagingException, AddressException{

        MailSender sender = new MailSender();

        String host = this.config.getSmtpHost();
        String port = this.config.getSmtpPort();

        if(port==null){
            sender.connect(host);
        } else {
            sender.connect(host,Integer.parseInt(port));
        }
        sender.send(to,from,subject,content);
        sender.disconnect();
    }

}

⌨️ 快捷键说明

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