session自定义存储,如何更好地进行session共享;读tomcat7源码,org.apache.catalina.session.FileStore可知

session自定义存储,如何更好地进行session共享;


读tomcat源码,org.apache.catalina.session.FileStore可知


一、详见:
方法1 public void save(Session session)
try {
            ((StandardSession)session).writeObjectData(oos);
        } finally {
            oos.close();
        }
方法2 public Session load(String id)
    ois = new ObjectInputStream(bis);


    StandardSession session = (StandardSession) manager.createEmptySession();
    session.readObjectData(ois);
    session.setManager(manager);

    return (session);



二、在conf/context.xml文件设置:这个是针对所有的项目了,也可以针对具体项目来配置在server.xml中;

打开context.xml,在<Context>节点下添加如下<Manager>节点:
<Manager className="org.apache.catalina.session.PersistentManager" >
    debug=0
    saveOnRestart="true"
    maxActiveSession="-1"
    minIdleSwap="-1"
    maxIdleSwap="-1"
    maxIdleBackup="-1"
    <Store className="org.apache.catalina.session.FileStore" directory="../session" />

//这里代表的是文件持久化.也可以自己实现Store
</Manager>




三、源码见下文
 * Licensed to the Apache Software Foundation (ASF) under one or more




package org.apache.catalina.session;




import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


import javax.servlet.ServletContext;


import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Loader;
import org.apache.catalina.Session;
import org.apache.catalina.util.CustomObjectInputStream;




/**
 * Concrete implementation of the <b>Store</b> interface that utilizes
 * a file per saved Session in a configured directory.  Sessions that are
 * saved are still subject to being expired based on inactivity.
 *
 * @author Craig R. McClanahan
 */
public final class FileStore extends StoreBase {




    // ----------------------------------------------------- Constants




    /**
     * The extension to use for serialized session filenames.
     */
    private static final String FILE_EXT = ".session";




    // ----------------------------------------------------- Instance Variables




    /**
     * The pathname of the directory in which Sessions are stored.
     * This may be an absolute pathname, or a relative path that is
     * resolved against the temporary work directory for this application.
     */
    private String directory = ".";




    /**
     * A File representing the directory in which Sessions are stored.
     */
    private File directoryFile = null;




    /**
     * The descriptive information about this implementation.
     */
    private static final String info = "FileStore/1.0";


    /**
     * Name to register for this Store, used for logging.
     */
    private static final String storeName = "fileStore";


    /**
     * Name to register for the background thread.
     */
    private static final String threadName = "FileStore";




    // ------------------------------------------------------------- Properties




    /**
     * Return the directory path for this Store.
     */
    public String getDirectory() {


        return (directory);


    }




    /**
     * Set the directory path for this Store.
     *
     * @param path The new directory path
     */
    public void setDirectory(String path) {


        String oldDirectory = this.directory;
        this.directory = path;
        this.directoryFile = null;
        support.firePropertyChange("directory", oldDirectory,
                                   this.directory);


    }




    /**
     * Return descriptive information about this Store implementation and
     * the corresponding version number, in the format
     * <code>&lt;description&gt;/&lt;version&gt;</code>.
     */
    @Override
    public String getInfo() {


        return (info);


    }


    /**
     * Return the thread name for this Store.
     */
    public String getThreadName() {
        return(threadName);
    }


    /**
     * Return the name for this Store, used for logging.
     */
    @Override
    public String getStoreName() {
        return(storeName);
    }




    /**
     * Return the number of Sessions present in this Store.
     *
     * @exception IOException if an input/output error occurs
     */
    @Override
    public int getSize() throws IOException {


        // Acquire the list of files in our storage directory
        File file = directory();
        if (file == null) {
            return (0);
        }
        String files[] = file.list();


        // Figure out which files are sessions
        int keycount = 0;
        for (int i = 0; i < files.length; i++) {
            if (files[i].endsWith(FILE_EXT)) {
                keycount++;
            }
        }
        return (keycount);


    }




    // --------------------------------------------------------- Public Methods




    /**
     * Remove all of the Sessions in this Store.
     *
     * @exception IOException if an input/output error occurs
     */
    @Override
    public void clear()
        throws IOException {


        String[] keys = keys();
        for (int i = 0; i < keys.length; i++) {
            remove(keys[i]);
        }


    }




    /**
     * Return an array containing the session identifiers of all Sessions
     * currently saved in this Store.  If there are no such Sessions, a
     * zero-length array is returned.
     *
     * @exception IOException if an input/output error occurred
     */
    @Override
    public String[] keys() throws IOException {


        // Acquire the list of files in our storage directory
        File file = directory();
        if (file == null) {
            return (new String[0]);
        }


        String files[] = file.list();
        
        // Bugzilla 32130
        if((files == null) || (files.length < 1)) {
            return (new String[0]);
        }


        // Build and return the list of session identifiers
        ArrayList<String> list = new ArrayList<String>();
        int n = FILE_EXT.length();
        for (int i = 0; i < files.length; i++) {
            if (files[i].endsWith(FILE_EXT)) {
                list.add(files[i].substring(0, files[i].length() - n));
            }
        }
        return list.toArray(new String[list.size()]);


    }




    /**
     * Load and return the Session associated with the specified session
     * identifier from this Store, without removing it.  If there is no
     * such stored Session, return <code>null</code>.
     *
     * @param id Session identifier of the session to load
     *
     * @exception ClassNotFoundException if a deserialization error occurs
     * @exception IOException if an input/output error occurs
     */
    @Override
    public Session load(String id)
        throws ClassNotFoundException, IOException {


        // Open an input stream to the specified pathname, if any
        File file = file(id);
        if (file == null) {
            return (null);
        }


        if (! file.exists()) {
            return (null);
        }
        if (manager.getContainer().getLogger().isDebugEnabled()) {
            manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".loading",
                             id, file.getAbsolutePath()));
        }


        FileInputStream fis = null;
        BufferedInputStream bis = null;
        ObjectInputStream ois = null;
        Loader loader = null;
        ClassLoader classLoader = null;
        ClassLoader oldThreadContextCL = Thread.currentThread().getContextClassLoader();
        try {
            fis = new FileInputStream(file.getAbsolutePath());
            bis = new BufferedInputStream(fis);
            Container container = manager.getContainer();
            if (container != null)
                loader = container.getLoader();
            if (loader != null)
                classLoader = loader.getClassLoader();
            if (classLoader != null) {
                Thread.currentThread().setContextClassLoader(classLoader);
                ois = new CustomObjectInputStream(bis, classLoader);
            } else {
                ois = new ObjectInputStream(bis);
            }


            StandardSession session =
                    (StandardSession) manager.createEmptySession();
            session.readObjectData(ois);
            session.setManager(manager);
            return (session);
        } catch (FileNotFoundException e) {
            if (manager.getContainer().getLogger().isDebugEnabled())
                manager.getContainer().getLogger().debug("No persisted data file found");
            return (null);
        } catch (IOException e) {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException f) {
                    // Ignore
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException f) {
                    // Ignore
                }
            }
            throw e;
        } finally {
            if (ois != null) {
                // Close the input stream
                try {
                    ois.close();
                } catch (IOException f) {
                    // Ignore
                }
            }
            Thread.currentThread().setContextClassLoader(oldThreadContextCL);
        }
    }




    /**
     * Remove the Session with the specified session identifier from
     * this Store, if present.  If no such Session is present, this method
     * takes no action.
     *
     * @param id Session identifier of the Session to be removed
     *
     * @exception IOException if an input/output error occurs
     */
    @Override
    public void remove(String id) throws IOException {


        File file = file(id);
        if (file == null) {
            return;
        }
        if (manager.getContainer().getLogger().isDebugEnabled()) {
            manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".removing",
                             id, file.getAbsolutePath()));
        }
        file.delete();


    }




    /**
     * Save the specified Session into this Store.  Any previously saved
     * information for the associated session identifier is replaced.
     *
     * @param session Session to be saved
     *
     * @exception IOException if an input/output error occurs
     */
    @Override
    public void save(Session session) throws IOException {


        // Open an output stream to the specified pathname, if any
        File file = file(session.getIdInternal());
        if (file == null) {
            return;
        }
        if (manager.getContainer().getLogger().isDebugEnabled()) {
            manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".saving",
                             session.getIdInternal(), file.getAbsolutePath()));
        }
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(file.getAbsolutePath());
            oos = new ObjectOutputStream(new BufferedOutputStream(fos));
        } catch (IOException e) {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException f) {
                    // Ignore
                }
            }
            throw e;
        }


        try {
            ((StandardSession)session).writeObjectData(oos);
        } finally {
            oos.close();
        }


    }




    // -------------------------------------------------------- Private Methods




    /**
     * Return a File object representing the pathname to our
     * session persistence directory, if any.  The directory will be
     * created if it does not already exist.
     */
    private File directory() throws IOException {


        if (this.directory == null) {
            return (null);
        }
        if (this.directoryFile != null) {
            // NOTE:  Race condition is harmless, so do not synchronize
            return (this.directoryFile);
        }
        File file = new File(this.directory);
        if (!file.isAbsolute()) {
            Container container = manager.getContainer();
            if (container instanceof Context) {
                ServletContext servletContext =
                    ((Context) container).getServletContext();
                File work = (File)
                    servletContext.getAttribute(ServletContext.TEMPDIR);
                file = new File(work, this.directory);
            } else {
                throw new IllegalArgumentException
                    ("Parent Container is not a Context");
            }
        }
        if (!file.exists() || !file.isDirectory()) {
            if (!file.delete() && file.exists()) {
                throw new IOException(
                        sm.getString("fileStore.deleteFailed", file));
            }
            if (!file.mkdirs() && !file.isDirectory()) {
                throw new IOException(
                        sm.getString("fileStore.createFailed", file));
            }
        }
        this.directoryFile = file;
        return (file);


    }




    /**
     * Return a File object representing the pathname to our
     * session persistence file, if any.
     *
     * @param id The ID of the Session to be retrieved. This is
     *    used in the file naming.
     */
    private File file(String id) throws IOException {


        if (this.directory == null) {
            return (null);
        }
        String filename = id + FILE_EXT;
        File file = new File(directory(), filename);
        return (file);


    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供 PyCharm 的安装教程。以下是一些基本步骤: 1. 首先,您需要从 JetBrains 官方网站下载 PyCharm。您可以在 https://www.jetbrains.com/pycharm/ 上找`1catalina.org.apache.juli.FileHandler` 是 Tomcat 中的一个处理器,用于将日志记录写入到下载链接。 2. 打开下载的安装程序,并按照提示进行安装。在安装过程中,您到文件中。该处理器使用 Java Util Logging(JUL)框架进行日志记录。 具体配置 `1catal可以选择使用默认设置或自定义安装选项。如果您是新手,建议使用默认设置。 3. 安装ina.org.apache.juli.FileHandler` 的相关属性可以在 `logging.properties` 文件中进行设置。以下是一些常见的完成后,启动 PyCharm。您可能会被要求导入或创建新的项目。如果您是初学者属性及其说明: - `1catalina.org.apache.juli.FileHandler.level`:指定 `1catalina.org.apache,可以选择创建一个新的项目并选择合适的解释器。 4. 在 PyCharm 中,您可以选择使用.juli.FileHandler` 的日志级别。默认值是 `ALL`,表示记录所有级别的日志信息。 -已有的 Python 解释器或创建一个新的解释器。如果您没有安装 Python,可以选择安装 Python 并将其与 PyCharm 集成。 5. 设置完成后,您将进入 PyCharm 的主界面。您可以 `1catalina.org.apache.juli.FileHandler.directory`:指定日志文件的目录路径。默认值是 `${c在此界面上创建、编辑和运行 Python 代码。 这是一个基本的 PyCharm 安装教程。atalina.base}/logs`,表示将日志文件写入到 Tomcat 的日志目录中。 - `1catal希望对您有所帮助!如果您需要进一步的帮助或有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值