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

📄 sessionmanager.java

📁 intra_mart是日本NDD公司开发的著名企业应用解决方案中间件。集成了J2ee服务器
💻 JAVA
字号:
/*
 * SessionManager.java
 *
 * Created on 2001/12/17, 18:28
 */

package jp.co.intra_mart.framework.base.session;

import java.util.ResourceBundle;
import javax.servlet.ServletRequest;

import jp.co.intra_mart.framework.system.log.LogConstant;
import jp.co.intra_mart.framework.system.log.LogManager;
import jp.co.intra_mart.framework.system.property.PropertyManager;

import java.util.MissingResourceException;

import jp.co.intra_mart.framework.system.property.PropertyHandlerException;
import jp.co.intra_mart.framework.system.property.PropertyManagerException;

/**
 * 僙僢僔儑儞儅僱乕僕儍偱偡丅
 *
 * @author INTRAMART
 * @version 1.0
 */
public class SessionManager {

    /**
     * 僙僢僔儑儞僼儗乕儉儚乕僋偺儘僌偺僾儗僼傿僢僋僗
     */
    static String LOG_HEAD = "[J2EE][Session]";

    /**
     * 僙僢僔儑儞僾儘僷僥傿僴儞僪儔偺僉乕
     */
    public static final String SESSION_PROPERTY_HANDLER_KEY = "session";

    /**
     * 僙僢僔儑儞儅僱乕僕儍庢摼僼儔僌
     */
    private static Boolean managerFlag = new Boolean(false);

    /**
     * 僙僢僔儑儞儅僱乕僕儍
     */
    private static SessionManager manager;

    /**
     * 僙僢僔儑儞僾儘僷僥傿僴儞僪儔
     */
    private SessionPropertyHandler sessionPropertyHandler;

    /**
     * 僙僢僔儑儞僼傽僋僩儕
     */
    private SessionFactory sessionFactory;

    /**
     * 僙僢僔儑儞儅僱乕僕儍傪庢摼偟傑偡丅
     *
     * @return 僙僢僔儑儞儅僱乕僕儍
     * @throws SessionManagerException 僙僢僔儑儞儅僱乕僕儍偺惗惉偵幐攕偟偨
     */
    public static SessionManager getSessionManager()
        throws SessionManagerException {
        if (!managerFlag.booleanValue()) {
            synchronized (managerFlag) {
                if (!managerFlag.booleanValue()) {
                    try {
                        manager = new SessionManager();
                    } catch (SessionManagerException e) {
                        String message = null;
                        try {
                            message =
                                ResourceBundle
                                    .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                                    .getString("SessionManager.FailedToCreateManager");
                        } catch (MissingResourceException ex) {
                        }
                        LogManager.getLogManager().getLogAgent().sendMessage(
                            SessionManager.class.getName(),
                            LogConstant.LEVEL_ERROR,
                            LOG_HEAD + message,
                            e);
                        throw e;
                    }
                    managerFlag = new Boolean(true);
                    String message = null;
                    try {
                        message =
                            ResourceBundle
                                .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                                .getString("SessionManager.SuccessedToCreateManager");
                    } catch (MissingResourceException ex) {
                    }
                    LogManager.getLogManager().getLogAgent().sendMessage(
                        SessionManager.class.getName(),
                        LogConstant.LEVEL_INFO,
                        LOG_HEAD + message);
                }
            }
        }

        return manager;
    }

    /**
     * SessionManager傪惗惉偡傞僐儞僗僩儔僋僞偱偡丅
     * 偙偺僐儞僗僩儔僋僞偼柧帵揑偵屇傃弌偡偙偲偼偱偒傑偣傫丅
     *
     * @throws SessionManagerException 僙僢僔儑儞儅僱乕僕儍偺惗惉偵幐攕偟偨
     */
    private SessionManager() throws SessionManagerException {
        PropertyManager propertyManager = null;
        Object factoryObject = null;
        String className = null;

        // 僾儘僷僥傿儅僱乕僕儍偺庢摼
        try {
            propertyManager = PropertyManager.getPropertyManager();
        } catch (PropertyManagerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                        .getString("SessionManager.FailedToGetPropertyManager");
            } catch (MissingResourceException ex) {
            }
            throw new SessionManagerException(message, e);
        }

        // 僙僢僔儑儞僾儘僷僥傿僴儞僪儔偺庢摼
        try {
            this.sessionPropertyHandler =
                (SessionPropertyHandler)propertyManager.getPropertyHandler(
                    SESSION_PROPERTY_HANDLER_KEY);
        } catch (PropertyHandlerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                        .getString("SessionManager.FailedToGetSessionPropertyHandler");
            } catch (MissingResourceException ex) {
            }
            throw new SessionManagerException(
                message + " : " + SESSION_PROPERTY_HANDLER_KEY,
                e);
        }

        // 僙僢僔儑儞僼傽僋僩儕偺庢摼
        try {
            className = this.sessionPropertyHandler.getSessionFactoryName();
        } catch (SessionPropertyException e) {
            throw new SessionManagerException(e.getMessage(), e);
        }
        if (className != null) {
            try {
                factoryObject = Class.forName(className).newInstance();
            } catch (Exception e) {
                String message = null;
                try {
                    message =
                        ResourceBundle
                            .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                            .getString("SessionManager.FailedToCreateFactory");
                } catch (MissingResourceException ex) {
                }
                throw new SessionManagerException(
                    message + " : factory class = " + className,
                    e);
            }
            if (factoryObject instanceof SessionFactory) {
                this.sessionFactory = (SessionFactory)factoryObject;
            } else {
                String message = null;
                try {
                    message =
                        ResourceBundle
                            .getBundle("jp.co.intra_mart.framework.base.session.i18n")
                            .getString("SessionManager.FactoryImplemented");
                } catch (MissingResourceException ex) {
                }
                throw new SessionManagerException(
                    message + " : factory class = " + className);
            }
        }
    }

    /**
     * 僙僢僔儑儞僾儘僷僥傿僴儞僪儔傪庢摼偟傑偡丅
     *
     * @return 僙僢僔儑儞僾儘僷僥傿僴儞僪儔
     */
    public SessionPropertyHandler getSessionPropertyHandler() {
        return this.sessionPropertyHandler;
    }

    /**
     * 僙僢僔儑儞僼傽僋僩儕傪庢摼偟傑偡丅
     * 僙僢僔儑儞僾儘僷僥傿偵SessionFactory偑巜掕偝傟偰偄側偄応崌丄null偑曉傝傑偡丅
     *
     * @return 僙僢僔儑儞僼傽僋僩儕乮僾儘僷僥傿偑愝掕偝傟偰偄側偄応崌null乯
     */
    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    /**
     * 僙僢僔儑儞僆僽僕僃僋僩傪庢摼偟傑偡丅
     * 巜掕偝傟偨儕僋僄僗僩撪偵偁傞僙僢僔儑儞僆僽僕僃僋僩傪庢摼偟傑偡丅
     * 僙僢僔儑儞偑懚嵼偟側偄応崌丄怴婯偵僙僢僔儑儞傪惗惉偟傑偡丅
     * 偙偺儊僜僢僪偼僙僢僔儑儞僾儘僷僥傿偵SessionFactory傪巜掕偟偨偲偒偵桳岠偵側傝傑偡丅
     *
     * @param request 儕僋僄僗僩
     * @return 僙僢僔儑儞僆僽僕僃僋僩
     * @throws SessionPropertyException 僙僢僔儑儞僾儘僷僥傿偺庢摼偵幐攕
     * @throws SessionFactoryException 僙僢僔儑儞僆僽僕僃僋僩偺惗惉偵幐攕
     */
    public SessionObject getSessionObject(ServletRequest request)
        throws SessionPropertyException, SessionFactoryException {
        SessionObject session = null;
        String key = this.sessionPropertyHandler.getSessionObjectKey();

        if (key != null) {
            session = (SessionObject)request.getAttribute(key);
            if (session == null) {
                // 僙僢僔儑儞偑懚嵼偟側偄応崌丄怴婯偵僙僢僔儑儞傪惗惉偡傞
                session = this.sessionFactory.createSessionObject(request);
                request.setAttribute(key, session);
            }
        }

        return session;
    }
}

⌨️ 快捷键说明

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